If String = "something" [HELP]

Dec 17, 2011 at 11:50pm
I receive an error message when creating an if statement asking for a specific string of text. Can someone please help me out. Sorry for the stupid question.

Error Messages:
In function `int main()':
ISO C++ forbids comparison between pointer and integer


1
2
3
4
5
6
7
8
9
10
11
12
13
int Random();
char want;

int main(){
cout << "Input \"Y\" to roll!" << endl;
cin >> want;
if(want=="y"); 
Random();
    
    
  system("PAUSE");   
  return 0;
}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random();
char want;

int main(){
cout << "Input \"Y\" to roll!" << endl;
cin >> want;
if(want=="y"); 
Random();
    
    
  system("PAUSE");   
  return 0;
}


int Random(){
    srand(time(0));
  for(int x =1; x<25;x++){
          cout << 1+(rand()%6) << endl;  
          }
          
  }
Dec 17, 2011 at 11:59pm
"y" is an array of 2 const char. Use single quotes if you want a char 'y'.
Dec 18, 2011 at 12:16am
When I change "y" to 'y' , I am able too enter anything and the if will still run.
Can you make your revisions too what I have already. In case there's something I missed.

Thanks Alot!
Dec 18, 2011 at 12:17am
Just a tip unrelated to your post, change that system("PAUSE") function to cin.ignore(). Looks much cleaner. System functions are frowned upon because I believe they pause every function of the computer, might be mistaken though.
Dec 18, 2011 at 12:27am
Remove the semicolon on line 12.

change that system("PAUSE") function to cin.ignore().

cin.ignore() is not enough here because the new line char is still in the input buffer. Adding two calls to ignore will work better.
Dec 18, 2011 at 6:12pm
Ok, I see what you mean.
Dec 18, 2011 at 7:45pm
I believe a call to cin.clear() will clear out the buffer. So a call to that then a call to cin.ignore() would do it. System("PAUSE") is just inefficient and flawed security wise.
Dec 18, 2011 at 8:26pm
ResidentBiscuit, cin.clear() just clears the error flags. It doesn't change the input buffer. I understand that people continue to use system("pause"). It just works and is not affected by the state of the input buffer. As a beginner efficiency and security is not top priority.

I find it amusing that it's only windows users that have this problem :P
Topic archived. No new replies allowed.