Hi, need an explanation for a simple program

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
Last edited on
yes,

1
2
3
4
5
6
7
..
	cout << "Please enter your name: ";
	cin >> name;
	cout << endl << endl << endl;// Is there any way to use endl between these lines? Actually after cin function? Here and... see above//	

	cout << "Please enter your age: ";
..


Use std::string http://www.mochima.com/tutorials/strings.html

To read a line which may contain white spaces as a string, use std::getline()
http://www.cplusplus.com/reference/string/string/getline/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
	std::string name ;

	std::cout << "Hello My name is Adriana, I will teach you how to use this program.\n\n" ; // << endl << endl;

	std::cout << "Please enter your name: ";
        std::getline( std::cin, name ) ;


	int age;
        std::cout << "Please enter your age: ";
        std::cin >> age;

        std::cout << "Your name is " << name << " and your age is " << age << "\n\n" ;

	std::cout << "Thank you for using this program.\n\n" ;
}
Last edited on
Thank you :)

So I've worked around the code and it resulted like this (and it works):

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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "Hello My name is Adriana, I will teach you how to use this program.\n\n\n";
	
	string name;
	int age;
		
		cout << "Please enter your name: ";
		getline (cin,name);
		cout << "\n";
	
		cout << "Please enter your age: ";
		cin >> age;
		cout << "\n\n\n";
	
		if (name == "")
		{
			cout << "You didn't entered your name. Your default name will be now Gheorghe.\n\n";
			name = "Gheorghe";
			cout << "Your name is " << name << " and your age is " << age << " years old.\n\n\n\n";
		}

		else
		{
			cout << "Your name is " << name << " and your age is " << age << " years old.\n\n\n\n";

		}

	cout << "Thank you for using this program.\n\n";
	system("Pause");
	return 0;

}


Uhm, second question now... I can't remember if I can use something like this:

if (name == "") & (age == "") so I can put two conditions. I want to add a message similar to the name. "You didn't input your age. Your default age is 0 years old."

I tried with this option but I do get some errors...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (name == "")
		{
			cout << "You didn't entered your name. Your default name will be now Gheorghe.\n\n";
			name = "Gheorghe";
			cout << "Your name is " << name << " and your age is " << age << " years old.\n\n\n\n";
		}

		else if (age == "")
		{
			cout << "You didn't entered your age. Your default age will be now 0 years old.\n\n";
			age = "0";
			cout << "Your name is " << name << " and your age is " << age << " years old.\n\n\n\n";
		}

		else
		{
			cout << "Your name is " << name << " and your age is " << age << " years old.\n\n\n\n";

		}


I'm missing something here... Don't know what to be honest...

Thank you :)
age is an integer; we can compare it with an integer eg. if( age == 14 ) , not with a string.

To check if the user entered a valid integer for the age, use std::cin >> age as the condition in the if statement.

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
28
29
30
31
32
33
34
#include <iostream>
#include <string>

int main()
{
    const std::string default_name = "Gheorghe" ;

    std::cout << "Hello My name is Adriana, I will teach you how to use this program.\n\n" ; // << endl << endl;

    std::cout << "Please enter your name: ";
    std::string name ;
    std::getline( std::cin, name ) ;
    if( name == "" ) // or if( name.empty() )
    {
        std::cout << "You didn't enter your name. Your default name is '" << default_name << "'\n";
        name = default_name ;
    }

    std::cout << "Please enter your age: ";

    int age = 0 ; // default age is zero
    if( std::cin >> age ) // if the user entered an age
    {
        // nothing needs to be done
    }
    else
    {
        std::cout << "You didn't enter your age. Your default age is " << age << '\n' ;
    }

    std::cout << "Your name is '" << name << "' and your age is " << age << "\n\n" ;

    std::cout << "Thank you for using this program.\n\n" ;
}
Topic archived. No new replies allowed.