Alright. I am teaching myself how to program C++ and I am writing a program that takes input from the user and puts it into a string from which I manipulate it to put it into a file on the same line. I separate information using special character £ so I can recall that information later without any problems. I am having a problem where I take input from the user. Say I wanna put "William V." into a string. When it enters it goes to the next cin. Below is an extract of the code I am referring to:
int main(){
while(true){
fstream myFile;
int switchInt;
file *a = new file();
cout << "Enter '1' to create new account, '2' to read all accounts, '3' to find an\naccount using ID, '4' to edit an account: ";
cin >> switchInt;
system("cls");
switch(switchInt){
case 1:
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
system("cls");
a->write();
break;
case 2:
system("cls");
a->read();
break;
case 3:
cout << "Enter ID: ";
cin >> IDFind;
system("cls");
a->find();
break;
case 4:
cout << "Enter ID: ";
cin >> IDFind;
system("cls");
a->edit();
break;
case 666:
myFile.open("IDStorage.txt", ios::out);
myFile << flush;
myFile.close();
myFile.open("NextID.txt", ios::out);
myFile << "000000\n";
myFile.close();
cout << "Flushing the data out of 'NextID.txt' and 'IDStorage.txt'.\n";
system("pause");
system("cls");
break;
default:
cout << "Number entered is out of bounds, enter new number.\n";
system("pause");
system("cls");
break;
}}}
Extra information: file is a custom class that manipulates the file "IDStorage.txt" by either creating (in the case that it doesn't already exist), reading, writing to, or editing it.
firstName and lastName are strings.
Yes, 666 does clear the file. I needed something to flush the file if needed without actually opening the file outside of the program.
Edit: I didn't explain this very well. When I have a space, it doesn't put all of the input into the string. It enters everything before the space into the string, then it goes to the next cin and enters the rest of the input until the next space in that string. I do not want this to happen. How do I fix this?
Maybe this is just me, but when I hit enter it does two cin.getline();s It's like I hit enter twice, but i don't. I'm switching machines really fast to see if it does the same