Hello guys & girls, my name is Robert.
I have done some c++ back in highschool and now I'm trying to do it again. So, I've already written my hello world program and now i'm at my second, where you enter your name and your age and it displays a message.
Program that does work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
using namespace std;
int main()
{
char name [20];
cout << "Hello My name is Adriana, I will teach you how to use this program." << endl << endl;
cout << "Please enter your name: ";
cin >> name;
int age;
cout << "Please enter your age: ";
cin >> age;
cout << "Your name is " << name << " and your age is " << age << endl << endl;
cout << "Thank you for using this program." << endl << endl;
system("Pause");
return 0;
}
|
Program that doesn't work all the way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
using namespace std;
int main()
{
char name [20];
int age;
cout << "Hello My name is Adriana, I will teach you how to use this program." << endl << endl;
cout << "Please enter your name: ";
cin >> name;
// Is there any way to use endl between these lines? Actually after cin function? Here and... see above//
cout << "Please enter your age: ";
cin >> age;
// and here...//
cout << "Your name is " << name << " and your age is " << age << endl << endl;
cout << "Thank you for using this program." << endl << endl;
system("Pause");
return 0;
}
|
The second program which breaks when entering the name (it doesn't let me enter the age) is bothering me, because I don't understand why it happens. Why does the int age; position matters in this case? I remeber that variables need to be expressed at the beginning.
So help me understand this, please :)
PS: I don't think it matters, but I'm writting and compiling in Visual Studio 2013.
PS 2: I've noticed that in program #1 if I put a name with space "Name Name" it won't let me put the age... I'm a little very confused :))
Best regards,
Robert