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.)
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.
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 :)
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.