-1

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.

  • 1
    Please show your inputs. I guess you are inputting a string with a space. You have to use `getline`. – 273K Oct 06 '22 at 16:30
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 06 '22 at 16:32
  • Try changing `cin >> name;` to `getline(cin, name);` – Remy Lebeau Oct 06 '22 at 16:40
  • `cout << "Input your full name: ";` then `cin >> name;` remember that `cin >> name;` stops reading on the first space character typed leaving the rest of the input for the next `cin >>` use the help from @RemyLebeau to fix that. – drescherjm Oct 06 '22 at 16:42
  • Then review this question as you may (will likely in the future if not now) run into this: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Oct 06 '22 at 16:44
  • @drescherjm good for the OP to know in general, but that issue doesn't apply to this particular code, since the `getline()` is not being performed after a previous `>>` extraction – Remy Lebeau Oct 06 '22 at 16:45
  • 1
    I know in this case it won't happen but just wanted to mention if the user changed the order of questions that this will be the next problem.. – drescherjm Oct 06 '22 at 16:46

1 Answers1

0

As user:6752050 and user:65863 said, using getline(cin, name) solved the problem. In all my tests, the input name had a space somewhere in it which was causing some messy things to go on behind the scenes when cin was looking for a string with no spaces.

Solved code should read as follows:

#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: ";
   getline(cin,name);
   cout << "Enter a value: ";
   cin >> number1;
   fout1 << name << endl << number1 << endl;
   fout1.close();
   cout << "Your file 'numbers.txt' has been created!";
}