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

  • 2
    possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – The Dark Sep 15 '15 at 02:39
  • I think almost everyone hits this issue when first starting with cin and getline. Hopefully the linked question is similar enough for you to work it out. – The Dark Sep 15 '15 at 02:41
  • Hm, I am reading it right now. I'm struggling a bit because I've learned very little (this is my first class and I have no prior experience). When I use cin.ignore it seems to delete the first character of each line....I'm not sure what I'm doing wrong still, but will be working on it. Thanks for the link! –  Sep 15 '15 at 02:47
  • Rather than looking at the question (where he puts `ignore` in quite a bit), look at the top rated answer. In your case, putting `cin.ignore (std::numeric_limits::max(), '\n'); ` after the `cin >> inFile;` (and other `cin >>` calls before a `getline`) should do the trick. The problem is that `cin >>` calls leave the carriage return in the input stream, meaning the next `getline` will read an empty string. – The Dark Sep 15 '15 at 02:53
  • That worked! Although it was still a little confusing, the compiler wouldn't take a copy+paste version of that cin.ignore that you posted...so I simply set it to an arbitrary high number. Example: cin.ignore(200, '\n'); and I set it before each getline and it seems to have worked. Thank you so much! –  Sep 15 '15 at 03:02
  • Sorry about that - you need to include for `numeric_limits. 200 should do for what you want (or even bigger if you think someone might type 200 spaces) – The Dark Sep 15 '15 at 03:08
  • I'm running into something strange based off what you told me though...you said to place it before each getline that I use, but if I do that it cuts off everything after that in the file...but if I only place one...it seems to work. I will post an example. –  Sep 15 '15 at 03:11
  • If you put it after a `getline` then the `getline` call has already removed the carriage return, so the `ignore` call will remove the entire next line. That is why I said to place it after `cin >>` calls before a `getline`. – The Dark Sep 15 '15 at 03:39
  • @KnicholasKennedy: Please do not mark **incorrect/incomplete answer** as **Best answer**. You should wait until you get **Best complete correct answer**. – Nitin Tripathi Sep 20 '15 at 03:26

2 Answers2

1

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
Nitin Tripathi
  • 1,224
  • 7
  • 17
  • This option seems to sort of work. If I input a space then it cuts off the first word. For example under name if I put a two part name, in the file output it cuts off the first name. –  Sep 15 '15 at 03:04
  • @KnicholasKennedy: Please see my latest answer for space issue. – Nitin Tripathi Sep 20 '15 at 03:22
0

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!
Nitin Tripathi
  • 1,224
  • 7
  • 17