I'm trying to build a program that will create a .txt file and store some basic data within. Currently, the setup is pretty basic, but I've run into a problem where after inputting one's full name, the terminal seems to skip the second cin
statement, (cin >> number1
). I'm not certain why, and if anyone sees what might be the issue here, I would really appreciate the feedback.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout1;
int number1;
string name;
fout1.open("numbers.txt", ios::out);
if (fout1.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
cout << "Input your full name: ";
cin >> name;
cout << "Enter a value: ";
cin >> number1;
fout1 << name << endl << number1 << endl;
fout1.close();
cout << "Your file 'numbers.txt' has been created!";
}
In addition, from every source I've seen, it seems that commanding .open
on a file that doesn't yet exit will create the file in question, but I'm not able to confirm this due to the previous problem. Can anyone verify that my understanding here is correct?
Thank in advance.