Jump to content
  • Advertisement
Sign in to follow this  
veghta

help me plesss any one

This topic is 4021 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
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.

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
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 "==".

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!