-
if..else problem
Yea i know this is lame but i'm new to this particular attempt, the code is self explanatory and i commented as to where the problem lies, thnx for the help.
Code:
/* Steven Billington
Simple bot
*/
#include <iostream.h>
int main ()
{
const max = 50; // Set limit for input. Also tried with just
//input[5] with same problems
char input[max];
cout<<"Hello\n"; //Prompt user for input
cout<<"Response: ";
cin.getline(input,max); //Store input into input variable
cout<<"\n"; // Start a new line
// Check to see what to do, program is skipping right to
//else option
if (input == "a")
{
cout<<"Its a nice day out."<<"\n";
}
else
cout<<"What?\n";
return 0;
}
-
(input[max]) is an array; therefore (input) is a pointer to the beggining of the array; i.e. in this case a pointer to a char.
by writing you are asking if a char pointer (the pointer itself, not the char it points to) is equal to a string literal.
A better way to do it would be
or even to use strcmp
Code:
if(!strcmp(input, "a"))
something like that..
actually, if you only need to check one char for now, it would be easier to understand if
Code:
if(input[0] == 'a')
since this way you are checking if a char equals another char(same as the first example but easier to notice).
correct me if I'm wrong.. :p
(PS. there's no such thing as a dumb question :) )
-
Ride-or-die, I've seen this question at least 20 times on this board. And you have three times my post-count...
Woot, I'm in a sig :D
-
This should be in the FAQ.
-
I just couldn't resist adding that one to my sig drax ^.^
-
edit: spoke too soon, new code w/ errors. * didn't work.
Code:
/* Steven Billington
Simple bot
*/
#include <iostream.h>
#include <string.h>
int main ()
{
const max = 50; // Set limit for input. Also tried with just
//input[5] with same problems
char input[max];
cout<<"Hello\n"; //Prompt user for input
cout<<"Response: ";
cin.getline(input,max); //Store input into input variable
cout<<"\n"; // Start a new line
// Check to see what to do, program is skipping right to
//else option
if (!strcmp (input == "Hello"))
{
cout<<"Its a nice day out."<<"\n";
}
else
cout<<"What?\n";
return 0;
}
PHP Code:
--------------------Configuration: simplebot - Win32 Debug--------------------
Compiling...
simplebot.cpp
D:\MyProjects\Mine - experimental or unfinished\Marvin version one\simplebot.cpp(24) : error C2660: 'strcmp' : function does not take 1 parameters
Error executing cl.exe.
simplebot.obj - 1 error(s), 0 warning(s)
-
look up strcmp somewhere, the msdn is always a good place to start.
There are a few error messages that you'll see time and time again, try to understand what they mean and how to fix the problem. Dont just fix this one and go away not knowing why it never worked before...
-
I'll do that endo, thnx guys.