LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2002, 10:32 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
why is not the following working?


Hi. What am I doing wrong?

#include <stdio.h>

int main(void)
{

char choice[20];

puts("Would you like to continue?\n");
puts("Enter \'yes\' or \'no\'");
scanf("%s", &choice);

if (choice == "YES" || choice == "yes")
printf("We are continuing!\n");

else if (choice == "NO" || choice == "no"){
printf("We are ending the program!\n");
}
else{
printf("You did not respond correctly!\n");
}

return 0;

}

I enter 'yes' ...
and it just prints >
You did not respond correctly!
I thought I was supposed to enclose the tests in double quotes.


thanks
 
Old 09-15-2002, 03:43 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well like we have already told you, you can't compare strings using mathematical operators like ==. like we already said, use strcmp() or whatever
 
Old 09-15-2002, 10:31 PM   #3
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks .... sorry. If I wanted to write my own strcmp function just to see how it works I think I could search thru the array and compare each character with the user entered array to see if it's a match and give an error if it isn't.

Is my idea close at all to how strcmp works? and where could I get a book or reference to how each of the various C functions like strcmp work and their function definitions? I think learning these would open up alot about C to me.

thanks again
 
Old 09-16-2002, 11:13 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
That's pretty much how strcmp works, although it doesn't just return a boolean, it returns the number of characters by which the two string differ. Therefore when two strings match exactly, strcmp returns 0.

Alex
 
Old 09-16-2002, 12:15 PM   #5
verigoth
Member
 
Registered: May 2002
Posts: 179

Rep: Reputation: Disabled
what you might want to do instead would be:

if((choice[0]=='Y') || (choice[0]=='y'))

that way lazy people like me can just hit one char

verigoth
 
Old 09-18-2002, 08:58 AM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
I would also suggest that if you insist on using scanf, please include a length operator on the %s, so that buffer overruns are not possible, as:

Code:
scanf("%20s", &choice);
A better option is to use fgets, IMHO, since it requires a length. Something like this works really nicely:

Code:
void getInput(char *myInput, const short int len)
{
  int dummy, sLen;
  fgets(myInput, len, stdin);
  sLen = strlen(myInput) - 1;
  if(myInput[sLen] =='\n') myInput[sLen] = '\0';
  else while((dummy = fgetc(stdin)) != '\n');
}
Since fgets leaves the newline on the input, we check to see if it exists.. if it does, we trim it off, if not, we know that there is still some input left in the input buffer, so we slurp it out into a dummy variable and poof, we're set.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Intel sound not working, usb modem not working. siriusb Linux - Hardware 8 03-04-2006 10:16 PM
After Online update: Hard drive working and working dacosta SUSE / openSUSE 6 11-06-2004 12:14 AM
F1 - F10 not working when F13 - F20 working in Linux haivinay Linux - General 0 09-20-2004 07:31 AM
Link/Applet not working in all Linux browsers, but working in IE MasterPatricko Linux - Software 6 09-04-2004 03:39 AM
cannot download files in mozilla -save dialog not working, galeon not working cmisip Linux - General 0 08-03-2003 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration