This is my first post joining this site so bare with me
Task:
pretty simple, just trying to get some concepts down and grow from there. I've got 'if statements' using integers and floats down pretty well, however, now I'm trying to ask the user to enter one of the options <<
printf("pick one of the following:\n name\n age");
>>, then if the user types "name" I would ask for their name and print it, the same goes for if they type "age" (just for simplicity's sake).
Problem:
I've searched the tutorials for solutions and maybe I'm not searching what I need to be but I've come across the functions:
-
strcmp
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
-
strncmp
http://www.cplusplus.com/reference/clibrary/cstring/strncmp/
and have tried messing around with those plus a few of the suggestions they advise at the bottom of the page with not much luck. i've tested their examples and they work fine. The strcmp was my closest bet however I'm trying to allow the user to pick between different options versus being stuck with one.
Code I'm trying to work with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main()
{
char namefunc[] = "name";
char agefunc[] = "age"
char input[10];
printf("Pick one of the following:\n name\n age");
gets(input);
if(strcmp(namefunc,input) !=0)//Here's where I'm lost, the tutorial put lines 6 and 7 in a <do, while> loop,
//however I'm trying to do the same in an if statement.
{
char username[20];
printf("enter your name");
scanf("%s", username);
printf("%s", username);
}
}
|
I get no error messages, the code runs, asks me to enter name or age, I'll enter a word, and then the following line is blank. I've tried a for loops as well.
thanks,
-stefan