Console closing before I put an input

After the last question, about the term, even tho the last 2 INTs are completely the same, its not letting me put an input about term, and says, press any button to exit

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"
using namespace std;


int main()
{
	cout << "Hello! What is your name?\n";
	char name[20];
	cin >> name;
	cout << "Hi, " << name << ". Nice to meet you! How are you?\n";
	char feelings[20];
	cin >> feelings;
	cout << "Do you like to study informatics in NBU?\n";
	char enjoy[20];
	cin >> enjoy;
	cout << "Which year are you?\n";
	int year;
	cin >> year;
	cout << "And which term you are in?\n";
	int term;
	cin >> term;
	return 0;
}

Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
using namespace std;

int main()
{
    char name[20];
    char feelings[20];
    char enjoy[20];
    
    int year = 0;
    int term = 0;
    
	cout << "Hello! What is your name? ";
	cin >> name;
	cout << "\nHi, " << name << ". Nice to meet you!";
	
	cout << "\nHow are you? ";
	cin >> feelings;
	
	cout << "\nDo you like to study informatics in NBU? ";
	cin >> enjoy;

	cout << "\nWhich year are you? ";
	cin >> year;
	
	cout << "\nAnd which term you are in? ";
	cin >> term;
	
	return 0;
}


Hello! What is your name? eee

Hi, eee. Nice to meet you!
How are you? hop

Do you like to study informatics in NBU? no

Which year are you? 1923

And which term you are in? 8
 
Exit code: 0 (normal program termination)
Any idea what was wrong with my code?
closed account (48T7M4Gy)
Probably you didn't clean/rebuild/compile properly - maybe.
When you have problem with unexpected program behavior, it is helpful to provide input you have tested your program with, as kemort had shown.
Hello! What is your name?
Name
Hi, Name. Nice to meet you! How are you?
Fine
Do you like to study informatics in NBU?
Yes
Which year are you?
1st
And which term you are in?
Press any key to continue . . .


And here is the file again, kinda cleaner:

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


int main()
{

	char name[20];
	char feelings[20];
	char enjoy[20];

	int year = 0;
	int term = 0;


	cout << "Hello! What is your name?\n";
	cin >> name;

	cout << "Hi, " << name << ". Nice to meet you! How are you?\n";
	cin >> feelings;

	cout << "Do you like to study informatics in NBU?\n";
	cin >> enjoy;

	cout << "Which year are you?\n";
	cin >> year;

	cout << "And which term you are in?\n";
	cin >> term;

	return 0;
}


1st is not an integer. You cannot store it in int. If you read stuff in int, you can only read digits (not considering diferent bases here).

What happens, is everything until first non-digit is read into year (1). Everything else is left in input buffer (st). Then next input operation happens and tries to read the rest. As it is int read too, and there is no digits in input, it stops reading, and sets error flag on input stream. Now your program is broken.

When you need to input integers, input only digits.
Aha, I get it! Thanks a bunch, so can I use char <name>[20] and replace the 2 ints? would it be better?

And one more question, what is the difference between using char and string ?
what is the difference between using char and string ?
You probably meant char array and string.

std::string is safer, supports strings of arbitrary size, contains many useful functions.
null-terminated character array aka c-string is susceptible to buffer overflows, is represented by pointer with all problems it has, uses C library to manipulate strings (which neds constant awareness of buffer sizes to avoid problems)
Thanks a lot mate! =)
Topic archived. No new replies allowed.