What is wrong with this code?...

I JUST started using C++.
I was making this questioning program where it asks you for your name and favorite food. It seemed to all work out fine without the ending, (asking for a yes or a no.) What did I do wrong? I'm a beginner, so please explain what you are saying. It may be something very simple.
(Also, please explain if any of my comments are wrong.)

Thanks,

-Benjamin
________________________________________________________________________________________________________________________________________________________________

#include <iostream>

using namespace std; // This sets up the language.

int main() // This is the heading/intro to the function.
{


char name[20]; //This explains it will be using something as a name.
char food[20]; //This explains it will be using something as a name for a certain food.
cout << "Hello, what is your name?"; // This asks for the name.
cin >> name; // This takes what the person enters as a name.
cout<< "Hello there, "<< name << "!"; // This says "Hello there," the name entered, and then puts an exclamation point after it.
cout << "What is your favorite food?"; //Asks for food name.
cin >> food; //Uses input to get food name.
cout<< "So your name is " << name << " and your favorite food is " << food << "?"; //Restates the name and food name given, puts them in a sentence.


if(
cin>> "yes") // If the person types in "yes"...
{cout<< "Okay. This is done now.";} //... Then this happens.
else;
{cout<< "I'll take that as a no. Let's try again.";} //Anything other then yes results in the statement.

system("pause");

}
 
if(cin >> "yes")


You can't do this. The >> operator takes string data out of cin and puts it in a variable. "yes" is a literal, not a variable.

You'd need to do something like this:

1
2
3
4
5
6
string answer;
cin >> answer;
if(answer == "yes")
{
  // they entered "yes"
}



Also, you should generally prefer string to char arrays for this purpose for many reasons:

1
2
//char name[20];  // blech!
string name;  // much better! 


Of course to use string you must also #include <string>
Thank you!
Why are double equal signs used?
It's the equality operator.
+1 filipe

= is assignment
== is equality / comparison.

1
2
3
int x;
x = 1;  // assigns the value 1 to x
if(x == 1)  // check to see if x contains the value 1 
to do what disch said you also have to #include <string> //this includes the library to use strings
what you can also do is just make another array (to stay consistent), change the other arrays to strings (assuming you're not expecting spaces) or change the size of the array to name[]
and try using std::cin.get instead of system("pause")

(note that i'm a beginner too, so if there's anything wrong with my suggestions please tell me :)

cheers!
Last edited on
or change the size of the array to name[]


This doesn't work, as you cannot create an array with no size unless the initializer implicitly sets the size (which wouldn't be the case here because there's no initializer).

These are exactly the kind of misconceptions which are common with char arrays, which is part of the reason you should avoid using them.

Save yourself the headache and use strings.
haha thanks :)

see with forums like these i become bettetr and better every time!
remember to use <code>your code</code>.guy
like this
cout<<"Hi,ben";
this "[" not "< "sorry
Topic archived. No new replies allowed.