Computing.Net > Forums > Programming > C string input and strcmp

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

C string input and strcmp

Reply to Message Icon

Name: tImmaY
Date: January 7, 2005 at 11:33:45 Pacific
OS: Windows XP Home SP2
CPU/Ram: AMD Athlon XP 2400+ / 512
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: January 7, 2005 at 12:18:53 Pacific
Reply:

#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!


0

Response Number 2
Name: tImmaY
Date: January 7, 2005 at 23:42:58 Pacific
Reply:

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]; ?


0

Response Number 3
Name: tImmaY
Date: January 10, 2005 at 01:28:22 Pacific
Reply:

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 :)


0

Response Number 4
Name: Don Arnett
Date: January 10, 2005 at 11:46:26 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


text file manipulation in... Simple question



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C string input and strcmp

c string to ascii valuse www.computing.net/answers/programming/c-string-to-ascii-valuse/7561.html

scanning a word and strcmp in C www.computing.net/answers/programming/scanning-a-word-and-strcmp-in-c/13333.html

Check pattern of C++ string www.computing.net/answers/programming/check-pattern-of-c-string-/13540.html