NO

Author Topic: Could someone help me, please?  (Read 8921 times)

jakeb211

  • Guest
Could someone help me, please?
« on: January 17, 2010, 12:18:46 AM »
Hello, I'm new to programming (student).  And I was wondering how to pass a string back from a function.
My project for school has us writing code for a pedometer that can store the information for ten users (weight and initials).  I am trying to write individual functions for most of my operations. I understand that this code might look pretty silly to other programmers(and there's things in there i haven't started to use yet).
I had it working(with char, not a string) while my cases contained the printf's and scanf's. Then I tried to change it up, and the problems began.
Any help would be appreciated.
Even pointing me toward something to read about it would help.
Here's what I've done so far...thanx.

/* The main menu for the pedometer */

/* Including these libraries to use their functions */
#include <stdio.h>   /* To use printf and scanf */
#include <stdlib.h>   /* To be able to clear the screen between menus*/

/* Define a constant used in the calories burned equation */
#define k 3.5

char choice;

/* Prototyping custom functions */
char Get_choice(void);   /* Display the menu */
char usr_nm(void);      /* User's Initials as a string*/
double usr_wght(void);   /* User's Weight */

int main(void)
{
   /* Defining variables */
   char choice, usr_nam[2];   /* trying out the string */
   
   double weight;

   choice = Get_choice();
   
   switch(choice)
   {
      case 'c':
      case 'C':
            usr_nam = usr_nm();
            weight = usr_wght();
            break;

      case 'e':
      case 'E':
         system("cls");
            printf("Select a User");
            printf("");
            break;
      default:
            printf("invalid data\n");
            break;
   }
   
   printf(" %s", usr_nam);

   return(0);
}

char Get_choice(void)
{
   char choice;

   printf("C - Create a user\n");
   printf("E - Edit a user\n");
   printf("S - Start recording\n");
   printf("T - Stop recording\n");
   printf("R - Reports\n\n");
   printf("Please make a selection...>\n");
   scanf(" %c", &choice);

   return(" %c", choice);
}

char usr_nm(void)
{
char initials[2];
   system("cls");
      printf("Enter user initials (xxx):\n");
      scanf("%s", initials);
   return("%s", initials);
}

double usr_wght(void)
{
double kg;
   system("cls");
      printf("\nEnter user weight (kg):\n");
      scanf("%lf", &kg);
   return(kg);
}

Syntax errors:

Building Main Menu.obj.
I:\School\Third Quarter\Intro To C Programming\Project\Pedometer Main Menu\Main Menu.c(30): error #2168: Operands of '=' have incompatible types 'char [2]' and 'char'.
I:\School\Third Quarter\Intro To C Programming\Project\Pedometer Main Menu\Main Menu.c(30): error #2088: Lvalue required.
I:\School\Third Quarter\Intro To C Programming\Project\Pedometer Main Menu\Main Menu.c(71): error #2060: Illegal return type; expected 'char' but found 'char *'.
*** Error code: 1 ***
Done.

Thanx again.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Could someone help me, please?
« Reply #1 on: January 17, 2010, 10:00:56 AM »
I think you need to use the following:
Code: [Select]
char * usr_nm(void)
{
char initials[2];
   system("cls");
      printf("Enter user initials (xxx):\n");
      scanf("%s", initials);
   return("%s", initials);
}
---
Stefan

Proud member of the UltraDefrag Development Team

jakeb211

  • Guest
Re: Could someone help me, please?
« Reply #2 on: January 17, 2010, 03:41:12 PM »
Thanks for suggestion.  I'll try it.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Could someone help me, please?
« Reply #3 on: January 17, 2010, 07:32:27 PM »
I think you need to use the following:
Code: [Select]
char * usr_nm(void)
{
char initials[2];
   system("cls");
      printf("Enter user initials (xxx):\n");
      scanf("%s", initials);
   return("%s", initials);
}

Sorry, but I think this would not work. initials is a locale variable and it's content is never valid after leaving this function. return("%s", initals) will only return a pointer to "%s".

Code: [Select]
char * usr_nm(void)
{
  char *initials;          // a pointer to a string
  initials = malloc(3);   // allocate memory for two chars and a zero ('\0') as the end of the string
  system("cls");
  printf("Enter user initials (xxx):\n");
  scanf("%s", initials);
  return(initials);
}

and if you not more need the string you should give it back with free(...).
best regards
 Alex ;)

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Could someone help me, please?
« Reply #4 on: January 18, 2010, 02:58:52 PM »
On the other hand one can still supply the variable to be filled as an argument:

Code: [Select]
// ...
            usr_nm(&usr_nam);
//...

void usr_nm(char * initials)
{
   system("cls");
      printf("Enter user initials (xxx):\n");
      scanf("%s", initials);
}
---
Stefan

Proud member of the UltraDefrag Development Team

Gary Willoughby

  • Guest
Re: Could someone help me, please?
« Reply #5 on: January 18, 2010, 07:01:05 PM »
Another way to avoid the free() call.

Code: [Select]
char* returnstring(void)
{
static char a[4] = {'\0'};
printf("Please enter your initials: ");
scanf("%3s", a);
return a;
}

int main(void)
{
char *b = returnstring();
printf("Your initials are %s\n", b);

return 0;
}

Using the static keyword keeps the local variable around after the function has returned so it's safe to return a pointer to it. :)
« Last Edit: January 18, 2010, 07:03:35 PM by Gary Willoughby »

JohnF

  • Guest
Re: Could someone help me, please?
« Reply #6 on: January 19, 2010, 08:29:10 AM »
removed.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Could someone help me, please?
« Reply #7 on: January 19, 2010, 12:54:12 PM »
Using the static keyword keeps the local variable around after the function has returned so it's safe to return a pointer to it. :)

Nice idea, but if you use static the memory is allocated once on the heap. So you can use this function once or you will overwrite all the input you have done.
best regards
 Alex ;)

JohnF

  • Guest
Re: Could someone help me, please?
« Reply #8 on: January 19, 2010, 09:07:32 PM »
Using the static keyword keeps the local variable around after the function has returned so it's safe to return a pointer to it. :)

Nice idea, but if you use static the memory is allocated once on the heap. So you can use this function once or you will overwrite all the input you have done.

This is quite normal in C - one should copy result into a buffer so that subsequent calls can be made without loss of data.

John

henrin

  • Guest
Re: Could someone help me, please?
« Reply #9 on: January 20, 2010, 04:48:33 PM »
John says: "Its quite normal inC.."

In ancient time, I used Fortarn, without reentrancy..
Modern technique prefer to write re-entrant functions.
It means that static variables are (almost) forbidden in any function.

That is a storage problem.
What unit (program / thread / function) is in charge of storing the data?
The called function or the calling one?
The response is generally right when one can affirm 'the data belongs to ..'
And then, declare the variable in that unit.

In fact, I use statics in function:
- for the purpose you described:
    - knowing it is not the best practice,
    - when it is compatible with the project,
    - when it is nor reusable code,
    - and because simpler is safer
- when the data is unique (overwrite can't happen)
- for error messages


JohnF

  • Guest
Re: Could someone help me, please?
« Reply #10 on: January 20, 2010, 05:33:33 PM »
Henrin,

I guess it's a style issue. But provided one knows the the function uses a static var and one deals with it, it's ok.

John

jakeb211

  • Guest
Re: Could someone help me, please?
« Reply #11 on: January 21, 2010, 01:55:08 AM »
Thank you very much for everyone's input I learned quite a bit from reading the replies.  It seems I still have alot to learn.  I got it working, and hope someday I'll be able to help with questions!  Again, thank you.