help me plesss any one

Started by
5 comments, last by veghta 16 years, 9 months ago
some one if you can give me a hand with this code all i am doinf is makeing a if with a char but will not work right.using dev c++ email is black_ops58@hotmail.com #include <iostream> #include <stdlib.h> #include <string> using namespace std; int main(int argc, char *argv[]) { char input; cout <<"(;-]>"; cin >> input; if (input = 'help') { cout <<"------------\n"; cout <<"- comands -\n"; cout <<"- quit -\n"; cout <<"- help -\n"; cout <<"- -\n"; cout <<"------------\n"; } if (input = 'quit') { cout<<" good bye\n"; } else { cout << "bad comand or bad file name :-)"; } system("pause"); return 0; }
Advertisement
char is only a character it cant be something like "help".You need to use a string instead.

string command;

cout<<"enter command : ";
cin>>command;

if(command == "help"){
printHelp();
}

Hope that helps.
You need ot use a function called strcmp. It will return zero if it is equal.

if( !strcmp(input, "help" ) )
{
cout <<"------------\n";
cout <<"- comands -\n";
cout <<"- quit -\n";
cout <<"- help -\n";
cout <<"- -\n";
cout <<"------------\n";
}


Oh BTW this is a common mistake and hard to find sometimes.
if(input="blahblah"){

}
Notice the single "=".That is the assignment operator comparison is done with "==".
Black Knight is right, your code should look like this ( I commented it to show the changes ):

#include <iostream>#include <string>using namespace std;int main( int argc, char *argv[] ){    string input;    cout << "(;-]> ";    cin >> input;    // Instead of two 'if' statements, use if, else if, and else. This way, always    // one of these branches will be taken.    if ( input == "help" ) // == instead of =, a common mistake    {        cout << "------------" << endl;  // endl is the C++ standard library way of denoting a new line. Use it.        cout << "- comands -" << endl;        cout << "- quit -" << endl;        cout << "- help -" << endl;        cout << "- -" << endl;        cout << "------------" << endl;    }    else if ( input == "quit" )    {        cout << "good bye" << endl;    }    else     {        cout << "bad comand or bad file name :-)" << endl;    }    // I omitted system("pause") here. If you want to make the window stay up    // you should run the program from the console instead of artificially    // pausing it.    return 0;}
thx for the help i will test it out later have to do some stuff
to day .
thx for the help dude

This topic is closed to new replies.

Advertisement