fstream and getline

+1 Earl Hudson · February 27, 2015

I wrote a crude little file writer. I'm certain it can be coded better, however it serves my purpose.
There is one problem with it. It takes an extra input. on a getline command.

The problem is in the lines. 
 ctext (14,"Please enter a description : ");
    getline (cin,desc);
    thefile
It issues two inputs, and you must hit enter on the first one, and enter the data on the second line before it will accept it. Any advice?


The entire code is here.

#include
#include
#include
#include

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);


// Color display (endl;)
void ctext(int color,string display){
SetConsoleTextAttribute(console,color);
cout

Post a Reply

Replies

Oldest  Newest  Rating
0 Earl Hudson · February 28, 2015
Don't know what happened... The code was fine last night. Let's try this again.
The entire code (As crude as it is) is here...

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

void ctext(int color,string display){
SetConsoleTextAttribute(console,color);
cout << display << endl;
SetConsoleTextAttribute(console,15);
}

void cwrite(int color,string display){
SetConsoleTextAttribute(console,color);
cout << display;
SetConsoleTextAttribute(console,15);
}
int main()
{
    int Rnum;
    string Rname,desc;
    int exitnum [10];
    cout << "\t\t\tRoom writer\n\n" << endl;
    cwrite(12,"Please enter the number of the room you wish to create : ");
    cin >> Rnum;
    while (Rnum!=0){
    ofstream thefile("roomdata.txt",ios::app);
    ctext (12,"\n\nI need a room name. Finish this statement.");
    cout <<"You are ";
    cin.ignore(1000, '\n');
    getline(cin,Rname);
    if (Rname=="done"){break;}
    thefile << Rnum << ",";Rnum++;
    thefile << "\"" << "You are "<< Rname << "\",";

    ctext (14,"Please enter a description : ");
    cin.ignore(1000, '\n');
    getline (cin,desc);
    thefile << "\"" << desc << "\"\n";
    cwrite (13,"North exits to room number     : ");
    cin >> exitnum[0];
    thefile << exitnum[0] << ",";
    cwrite (13,"Northeast exits to room number : ");
    cin >> exitnum[1];
    thefile << exitnum[1] << ",";
    cwrite (13,"East exits to room number      : ");
    cin >> exitnum[2];
    thefile << exitnum[2] << ",";
    cwrite (13,"Southeast exits to room number : ");
    cin >> exitnum[3];
    thefile << exitnum[3] << ",";
    cwrite (13,"South exits to room number     : ");
    cin >> exitnum[4];
    thefile << exitnum[4] << ",";
    cwrite (13,"Southwest exits to room number : ");
    cin >> exitnum[5];
    thefile << exitnum[5] << ",";
    cwrite (13,"West exits to room number      : ");
    cin >> exitnum[6];
    thefile << exitnum[6] << ",";
    cwrite (13,"Northwest exits to room number : ");
    cin >> exitnum[7];
    thefile << exitnum[7] << ",";
    cwrite (13,"Up exits to room number        : ");
    cin >> exitnum[8];
    thefile << exitnum[8] << ",";
    cwrite (13,"Down exits to room number      : ");
    cin >> exitnum[9];
    thefile << exitnum[9] << "\n";
    }
return 0;
  • 1

C++

107,319 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator