question and see what's wrong

Hello, I keep trying to do this after I type name and then click on enter. It keeps closing itself. Did I write any wrong codes?

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
  #include <iostream>
using namespace std;

int main()
{
	cout << "Hello everyone, this is C++ language." << endl;
	int a = 250;
	int b = 250;
	cout << "250 + 250 =" << endl;
	int price = a + b;
	cout << "Final result is \b " << price << endl;
	int c;
	int d;
	int e;
	cout << "What is your name?" << endl;
	cin >> c;
	cout << "Where are you from?" << endl;
	cin >> d;
	cout << "How old are you?" << endl;
	cin >> e;
	cout << "My name is" << c << "." << endl;
	cout << "I am from" << d << "." << endl;
	cout << "I am" << e << "\n years old." << endl;

	return 0;
}
Line 16: c is an int. How many people do you know with a number for a name?
If you try to enter alphas in response to the cin, that cin and subsequent cins will fail. .

Line 18: ditto regarding d.

Line 24: Your program is going to close immediately. See this thread:
http://www.cplusplus.com/forum/beginner/1988/


What code should they be for line 16, line 18, and line 24?
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "Hello everyone, this is C++ language." << endl;
	int a = 250;
	int b = 250;
	cout << "250 + 250 =" << endl;
	int price = a + b;
	cout << "Final result is \b " << price << endl;
	string c,d,e;
	cout << "What is your name?" << endl;
	cin >> c;
	cout << "Where are you from?" << endl;
	cin >> d;
	cout << "How old are you?" << endl;
	cin >> e;
	cout << "My name is " << c << "." << endl;
	cout << "I am from " << d << "." << endl;
	cout << "I am " << e << " years old." << endl;

	return 0;
}


This should work for you ...
change the type of c,d,e to string will solve this problem
It's almost perfect. Line 20, line 21, and line 22 don't appear. It still closes itself after I put age.
@DCisthebest I have only read about it and never experienced it before. so i don't know if it correct or if it would work but try this:
1
2
3
4
5
6
7
8
9
cout << "How old are you?" << endl;
cin >> e;
cin.ignore();///add this
cout << "My name is " << c << "." << endl;
cout << "I am from " << d << "." << endl;
cout << "I am " << e << " years old." << endl;
cin.get();///add this too

return 0;


did it work?
Yes, it's perfect. Thank you very much. I have to memorize them.

I wonder why did that have to be cin.ignore(); and cin.get();
@DCisthebest in most cases it doesn't happen(never happened to me before) but sometimes, program exits immediately. if we add cin.get(), it waits for user input to move forward. eg:
cout<<"hello world";///exits immediately but
1
2
cout<<"hello world";
cin.get();///now program waits for user  


but when we use cin>> it takes the input until but including the spaces or newline(enter) character. eg: for string a; cin>>a;, if we type: "games" and then press enter, "games " is saved in 'a' but newline character(enter) is present in the buffer(temporary memory) that would trigger cin.get() so that it would no longer wait for user's input. using cin.ignore() would clear anything that is present in the buffer(in this case, enter key) and cin.get() would again wait for external input before terminating.

hope that was clear enough.
Line 20, line 21, and line 22 don't appear. It still closes itself after I put age.

Did you bother to read the link I posted which explains how the prevent the program from closing immediately?
AbstractionAnon wrote:

Line 24: Your program is going to close immediately. See this thread:
http://www.cplusplus.com/forum/beginner/1988/


@AbstractionAnon: Yes, I did. I was surprised that they use std::cout and std::cin. I never use std:: part. I keep learning. I am going to have to learn array and if else again.

@koopey: Those codes work great. I would have to remember them. Thank you so much.

I was surprised that they use std::cout and std::cin. I never use std:: part.

std:: prefix is required if using namespace std; is not specified.

Specifying using namespace std; is generally considered a poor practice since it brings in the entire std:: namespace creating potential naming conflicts. That being said, using namespace std; is commonly used here for brevity and clarity.
I noticed it has a small problem with answering to the question. For example:

What is your name?
John Smith <- It should be right. But it shows me like

What is your name?
John Smith
Where are you from?
How old are you?
25

It didn't ask you the question about where are you from. Then final part shows me like this

I am John.
I am from Smith
I am 25 years old.

I thought

char str[15]; would solve that problem but it didn't.
Last edited on
@DCisthebest
1
2
cout << "What is your name?" << endl;
getline(cin,c,'\n');///use this instead of cin 

for how it works: http://www.cplusplus.com/reference/string/string/getline/?kw=getline hope this helps.
I had to edit my message here since I finally solved the problem. The code doesn't look the same at all. I keep practicing with C++.

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string a, b, c, d, e;
	int f;
	std::cout << "Hello there!" << endl;
	std::cout << "What is your first name?" << endl;
	std::getline(std::cin, a);
	std::cout << "What is your middle name?" << endl; 
	std::getline(std::cin, b);
	std::cout << "What is your last name?" << endl;
	std::getline(std::cin, c);
	std::cout << "Where are you from?" << endl;
	std::getline(std::cin, d);
	std::cout << "Where are you working at?" << endl;
	std::getline(std::cin, e);
	std::cout << "How old are you?" << endl;
	std::cin >> f;
	cin.ignore();
	std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
	std::cout << "I am from \b " << d << "." << endl;
	std::cout << "I am \b " << e << "." << endl;
	std::cout << "I am \b " << f << " years old." << endl;
	cin.get();
	system("Pause");

	return 0;
}
Last edited on
Topic archived. No new replies allowed.