Need help with *argv and while loops

hey guys, im writing a little text based RPG game in C++, just to work on my programming skills, and to learn the language, since some many other languages are based on it. What im trying to do is have the user input a command, and have a while loop do something if it matches a certain word, and do something else if they dont match. But i cant get it to work right, any help would be greatly appricatied. Here is a snippet from the code im working on.#include<iostream>
using namespace std;


int main(int argc, char *argv[])
{
cout << "enter a word.\n";
cin >> *argv;
cout << *argv;

while (*argv[1] != "word")
{
cout << "*argv does not equal word\n";

}

cout << "argv equals word\n";
return 0;
}


First off this doesnt compile, keep getting an error saying that C++ forbids a comparsion between a pointer and an integar. I know its probably something small and im just overlooking it, but hey im still learning.
Firstly, please post any code inside Code Tags, "[ code ] your code here [ /code ]" (without spaces). It makes it easier to read your code and to help you. :)

Now on to your code, there are a lot of elements in your code that do not make too much sense, such as:

-why you are storing user input to the location of your command line parameters cin >> *argv;? Or why you are even including command line parameters at all?

-you can not compare a character arrays with the equality operator, you have to use the strcmp(...) function.

-if your user enters anything but "word" (if your comparison operation worked, which it does not), then it will cause an infinite loop.

I will try to help you, but I would HIGHLY recommend that you complete some C++ tutorials, before you continue on your project. There are some good ones on this website, you can find them here: http://www.cplusplus.com/doc/tutorial/

O.k. now on to your code.

Firstly, you do not need any command line parameters, so you can remove any reference to them:
this code becomes:

int main(int argc, char *argv[])

this:

int main()

I believe this is also the cause of your "C++ forbids a comparsion between a pointer and an integer".

Next you will need to store the user input in a variable, you can use a char array, but using the String Class (#include <string> ), is a better approach. You can find information on the String Class here: http://www.cplusplus.com/reference/string/string/ and tutorials on them here: http://www.cplusplus.com/doc/tutorial/basic_io/

The String Class contains a comparison function string::compare, you can find information on it here: http://www.cplusplus.com/reference/string/string/compare/

Finally, think about the placing of cin>>. Where could you put it, so that an infinite loop can be avoided? Also think about the type of loop your using. Is a while loop appropriate or is there another form of iteration that suits your purpose better?

Look over the links I've given you and try again, if you get stuck just post here and I'm sure someone will help you out.

Seriously consider doing some tutorials.

I hope this helps and good luck!
Last edited on
If you're going to use std::string (not String) you can use the == operator.
If you're going to use std::string (not String) you can use the == operator.

I stand corrected :) Thanks for the information filipe.
Topic archived. No new replies allowed.