C++ Learning Community Forum
August 01, 2010, 02:37:53 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: why isnt this working?  (Read 1533 times)
spiroth10
N00b!!1
*
Posts: 2


View Profile
« on: December 24, 2007, 05:34:57 AM »

I haven't bothered with c/c++ in a long time. After going to revisit it, I ended up with this not working.

I havent the slightest idea why, but the if statements arent evaluating the variable as a character, although if I try to printf(); out the variable it clearly gives me a y or n.

#include <stdio.h>

char ans;
   
int main(){
   
   printf("are you cool? y/n\n");
   scanf ("%s", &ans); 
   if (ans == "y") {
      printf("yes\n");
   }
   else if (ans == "n") {
      printf("no\n");
   }
   else {
      printf("your choice was not an answer, either y or n are\n");
   }
   getchar();
   return 0;
}
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #1 on: December 24, 2007, 06:07:14 AM »

What was happening, was you were trying to compare a char value with a memory location (what is in the double quotes) you can either replace what you have in your if with ans=='y' and ans=='n' (which checks the literal character value) or you could use C strings
Code
GeSHi (c):
#include <stdio.h>
#include <string.h>
 
char ans;
 
int main(){
 
  printf("are you cool? y/n\n");
  scanf ("%s", &ans);  
  if (strcmp(&ans,"y")==0) {
     printf("yes\n");
  }
  else if (strcmp(&ans,"n")==0) {
     printf("no\n");
  }
  else {
     printf("your choice was not an answer, either y or n are\n");
  }
  getchar();
  getchar();//extra one so it halts program long enough to see the output
  return 0;
}
 
Created by GeSHI 1.0.7.18

Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #2 on: December 24, 2007, 04:59:40 PM »

Code
GeSHi (c):
char ans;
...
scanf ("%s", &ans);
...
if (strcmp(&ans,"y")==0) {
...
else if (strcmp(&ans,"n")==0) {
 
Created by GeSHI 1.0.7.18

Noooo, you're as bad as each other Smiley You should use %c with scanf to read a single char, %s adds the NUL terminator to the buffer which would overflow the single char. As for strcmp, yes it takes a char*, but remember C-strings have to be NUL-terminated which a single char obviously isn't (well, unless you enter NUL in the scanf). It's much easier to just replace it with a simple if(ans == 'y')
Logged

FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #3 on: December 24, 2007, 08:52:21 PM »

adeyblue the method h8 mentioned is fine for a single char comparison. in fact I've used it for comparing up to 4 chars (on 32 bit processors). and I've seen variants of this method taught in just about every C book I've seen.
Logged


Imagine the impossible, then make it happen.
C-Man
Does anyone even read this ?
Global Moderator
Dr. of C++ology
*****
Posts: 988



View Profile WWW
« Reply #4 on: December 25, 2007, 10:16:12 AM »

it's still overflowing tho , you shouldn't do that , use %c as adeyblue said
Logged

adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #5 on: December 25, 2007, 10:54:16 PM »

If you use:

Code
GeSHi (c):
char ans;
int ret;
 
scanf ("%s", &ans);  
ret = strcmp(&ans,"y");
 
Created by GeSHI 1.0.7.18

then it'll technically work since the NUL will be stored in the byte after ans, however it's also very incorrect as fitting two chars in the space of one is a buffer overflow. If you correctly read the char with %c then the strcmp will no longer do what is expected as it goes on to take into account whatever trash is in memory after ans.
Logged

FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #6 on: December 26, 2007, 08:46:35 AM »

Code
GeSHi (c):
char ans;
int ret;
 
scanf ("%c", &ans);  
ret = ans - 'y'
Created by GeSHI 1.0.7.18
does exactly the same thing, without overflow.
but
Code
GeSHi (c):
if (ans == 'y')
Created by GeSHI 1.0.7.18
is considered more correct.
or
Code
GeSHi (c):
switch ans
{
 case 'y':
   /*code here*/
   break;
}
Created by GeSHI 1.0.7.18

the only overflow is going to be in the scanf statement, which still is in your code adeyblue. checking a char against a char or array will not result in overflow. just possible unpredictable results.
Logged


Imagine the impossible, then make it happen.
spiroth10
N00b!!1
*
Posts: 2


View Profile
« Reply #7 on: December 27, 2007, 06:55:13 PM »

Thanks for your help guys, its been like 2 years since I've bothered much with C/C++ and now I'm getting back into it again. You've helped me a great deal


Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!