Beginner Programmer

Hi,


I am a starting out 12 year old C++ programmer and I had come into a problem. Basically, It asks the user for input on what the computer will do. Instead of a simple bool where the user inputs 1 or 0, I'd like it to be more "user friendly" and execute this section of code if the user inputs this word, and this one if the user inputs this word. Also, when finished, there will be more like 5-10 different words the user would be able to input. I am on Mac OS X 10.6.8 Snow Leopard with 2 GB RAM and 2 GHz Processor. Here is my snippet of code:
#include <iostream>
using namespace std;
int main()
{
cout << "my text";
char begin;
cin >> begin;
char a;
if (begin=="a")
{
cout << "hi";
else
cout << "bye";
}
return 0;
}


So as you can see, i didn't give you the exact code to protect my code from people who rip peoples code off for a living, but what it does is it asks the user for input, and if the text the user inputs is "a", it executes this code. If the user inputs something else, it executes this other code snippet. Please help me out. Thanks!!!


edit: After i added == i got an error saying "ISO C++ forbids comparison between pointer and integer." What does this mean??????
Last edited on
A single = is assignment, == tests for equality (which is what you want.
You also might want to look into the switch statement. It does the same thing as a series of if statements, but it's a lot more readable, particularly if you have more than 3 or 4 else options.

http://cplusplus.com/doc/tutorial/control/

Good luck!
Last edited on
Also, your issue now is begin is a char (one character), but you are trying to compare it to "a" which is a string. Use ' ' for characters, not " ".
what is the purpose of the line
char a;?

You should probably assign it to something and then use it as a comparison. Or just omit it completely.

1
2
3
4
5
6
7
char a;
if (begin==a)
etc.

OR

if (begin == 'a')
So as you can see, i didn't give you the exact code to protect my code from people who rip peoples code off for a living,


really ?
hi

As you have declared"begin" as char you must use the 'a' in the declaration of if statement.
the code must be
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
cout << "my text";
char begin;
cin>>begin;
if (begin=='a')
cout << "hi";

else
cout << "bye";

return 0;
}


can u pls send the reply its working or not
Last edited on
So as you can see, i didn't give you the exact code to protect my code from people who rip peoples code off for a living,


You may not realize this, but when people 'steal' code from others it's usually not from 12 year old beginner programmers. I sincerely doubt you have anything in your code that's worth the trouble of protecting it, so you can just go right ahead and show us.
Last edited on
Topic archived. No new replies allowed.