#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
string inFile;
ifstream inData;
ofstream outData;
string outFile;
string fullName;
string fileName;
string courseCode;
char answer;
cout << "Are you creating a new file? y/n" << endl;
cin >> answer;
if(answer == 'n')
{
cout << "Enter the name of your file." << endl;
cin >> inFile;
inData.open(inFile.c_str());
inData >> fileName;
cout << fileName << endl;
}
else if(answer == 'y')
{
cout << "Enter the name of your file." << endl;
cin >> outFile;
outData.open(outFile.c_str());
cout << "What is your full name?" << endl;
getline(cin, fullName);
outData << fullName;
cout << "What is the code for your class?" << endl;
getline(cin, courseCode);
outData << courseCode;
}
return 0;
}
I am attempting to create a program that can either open files already present, or create a new file that reads name, course code, course information, and professor. This is a project and I am VERY new to coding. I've attempted to research this via the forums but I can't find an exact replica of my problem.
My question is, when I attempt to run this program, there is no pause between "What is your full name" and "What is the code for this course?", so there is only one reply. What ends up happening is after you hit 'y' and then input a new file name (example: 'test.txt') it asks BOTH questions at once, so there is only one chance to input data before the file terminates. What am I doing wrong?
EDIT:
cout << "Enter the name of your file." << endl;
cin >> outFile;
outData.open(outFile.c_str());
cout << "What is your full name?" << endl;
cin.ignore (200, '\n');
getline(cin, fullName);
outData << fullName << endl;
cout << "What is the code for your class?" << endl;
getline(cin, courseCode);
outData << courseCode << endl;
cout << "What is the name of the course?" << endl;
getline(cin, courseName);
outData << courseName << endl;
This seems to work, and correctly stores the data in the file, but if I do it the way dark told me...
cout << "Enter the name of your file." << endl;
cin >> outFile;
outData.open(outFile.c_str());
cout << "What is your full name?" << endl;
cin.ignore (200, '\n');
getline(cin, fullName);
outData << fullName << endl;
cout << "What is the code for your class?" << endl;
cin.ignore (200, '\n');
getline(cin, courseCode);
outData << courseCode << endl;
cout << "What is the name of the course?" << endl;
cin.ignore (200, '\n');
getline(cin, courseName);
outData << courseName << endl;
Then it seems to cut off the rest of the data. Thoughts, Dark?