Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tImmaY
hey, i'm having problems taking in a string of characters and telling the program what to do once this is done. basically i just want a yes / no response and for the program to go to a function given each response. so how could i accomplish this? thanks

#define ANSWER_SIZE 10
/* define the size of the answer string */
char answer[ANSWER_SIZE];/* prompt and get answer */
printf("yes or no?\n");
fgets(answer,ANSWER_SIZE,stdin);/* remove the newline from the end of the response */
answer[strlen(answer)-1] = '\0';
if (strcmp(answer,"yes") == 0) {
.
.
do yes stuff
.
.
} else if (strcmp(answer,"no") == 0) {
.
.
do no stuff
.
.
} else {
.
.
do not yes/not no stuff
.
.
}Be sure to come back and let us know if our suggestions helped!

for some reason Dev-C++ isn't liking me and it skips straight through the input portion right to analyzing whatevers in the variable. did i do something wrong?
printf("Would you like to load this level: %s\n", level);
printf("Enter yes or no: \n");
fgets(loadlevel,ANSWER_SIZE,stdin);
/*Remove new line from end of input*/
loadlevel[strlen(loadlevel)-1] = '\0';
if (strcmp(loadlevel, "no") == 0)
{
nope();
}
else if(strcmp(loadlevel, "yes") == 0)
{
lvlchk(lvl);
}
else { printf("I'm sorry, %s is not a valid response.\n", loadlevel); start(menu, stage); }also, why did you define ANSWER_SIZE? is there an advantage to doing it this way over say: char answer[10]; ?

no response..so i'll take that as a yes.
actually i'm just quoting myself from a completely dif. situation.. but yea. still curious as to what i did wrong.... lol :)

Assuming that loadlevel and ANSWER_SIZE are defined/declared appropriately, I don't see a problem.
The only thing that I can think of is to check the return value from fgets().
If it returns NULL, then it's not working, tho I don't see a problem. If it works, is should return the address of the array loadlevel.
Be sure to come back and let us know if our suggestions helped!

![]() |
text file manipulation in...
|
Simple question
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |