Hey all,
I recently started learning C++ off the tutorials on this site and they are great. I've started making my own simple program which takes in information and trying to make it display it in a structured format. I first used int name, and int alias. When i ran the program it would work fine but when it came to cin>>name; and cin>>alias; it would close the program because the user was inputting letters and instead of numbers. So i changed some stuff around and this is what i have so far.
#include <iostream.h>
#include <string.h>
int main()
{
char name[50], alias[50], filename[50];
int age;
int yearsexp;
int yesorno;
cout<<"Please enter your real name: ";
cin.getline(name, 50, '\n');
//Gets Name
cout<<"Please enter your age: ";
cin>>age;
//Gets Age
cout<<"How long have you been playing in years: ";
cin>>yearsexp;
//Gets years
cout<<"Whats your in-game name: ";
cin.getline(alias, 50, '\n');
//Gets In-Game Name
cout<<"This is all the info you have entered"<<endl;
cout<<"NAME:"<<name<<endl;
cout<<"AGE:"<<age<<endl;
cout<<"Years Playing:"<<yearsexp<<endl;
cout<<"ALIAS:"<<alias<<endl;
//Displays information inputted from user
cout<<"Is this information correct? Press 1(Yes) or 2(No) then enter: ";
cin>>yesorno;
if(yesorno==1)
{
cout<<"Please enter a 1-word name for the info to be saved in: ";
cin.getline(filename, 50, '\n');
cout<<"Cannot save file "<<filename<<endl;
//Added the "Cannot save file" msg to inform user it cant do anything yet
}
else if(yesorno==2)
{
cout<<"Please restart program";
//Loop function not implemented yet
}
return 0;
}
Excuse the untidyness. I compile and build the program just fine. But when i run it (via msdos) it takes in name part, then the age, years playing... and then "skips" the ALIAS part and goes right into the "do you want to save" part, if i pick yes it "skips" the filename part and closes the program.
Could someone tell me why is it doing this? And if possible explain it in a way a beginner would understand.
Thanks for taking the time to read this!