user5336127
user5336127

Reputation:

C++ Issue with file open and file output

#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?

Upvotes: 0

Views: 118

Answers (2)

Nitin Tripathi
Nitin Tripathi

Reputation: 1254

Here Adding cin.ignore your original code should resolve the issue. your question might be, what is cin.ignore ,

istream& ignore (streamsize n = 1, int delim = EOF);

ignore - Extracts characters from the input sequence/stream and discards them, until either n characters have been extracted, or one compares equal to delim.

how to decide what size and which delimiter? - This basically depends on your requirement, size - i will prefer to use max std::numeric_limits<std::streamsize>::max() its value is streamsize: 9223372036854775807 - delimiter can be anything, space,new line, comma, semi colon, anything depends on code.

I hope it resolves your issue :)

Modified part of your code

cout << "Enter the name of your file." << endl;
cin >> outFile;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

OUTPUT:

$ g++ cpp2.cpp ; ./a.out 
Are you creating a new file? y/n
y
Enter the name of your file.
HH
What is your full name?
Why do you Need?
What is the code for your class?
Not necessary!

Upvotes: 0

Nitin Tripathi
Nitin Tripathi

Reputation: 1254

Take another string variable say string x; and use following modification:

getline(cin >> x, fullName);
getline(cin >> x, courseCode);

It should resolve your problem.

Output:

./a.out 
Are you creating a new file? y/n
y
Enter the name of your file.
HELLO
What is your full name?
NO Need
What is the code for your class?
I dont know

Upvotes: 1

Related Questions