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
#include "iostream"
usingnamespace 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;
}
#include <iostream>
usingnamespace 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)
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 . . .
#include "iostream"
usingnamespace 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.
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)