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.