Sign in to follow this  

help me plesss any one

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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";
}


Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this