Okay I have made a c++ console application to take user input and append it to a text file and then read it back.

I am using the win32 version of Dev c++

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    
    int choice;

    cout << "What do you want to do?\n";
    cout << "1) Write To File\n";
    cout << "2) Read From File\n";
    cout << "Enter a choice: ";
    cin >> choice;
    
                
    ofstream outputFile("file.txt", ios::app);
    ifstream inputFile;
    
        char myString[50];

    switch (choice){

        case 1:

    cout << "Enter a string: ";
    cin.get( myString, 50, '\n');

    cout << "Writting string to file...\n";

    outputFile << myString;
    outputFile.close();

    inputFile.open("file.txt");
            
    break;
    
        case 2:
            
    inputFile.open("file.txt");

    cout << "Reading string from file...\n";

    inputFile.getline(myString, 50, '\n');

    cout << myString;    
            
    break;    
        
        default:
            cout << "Not a valid choice!";
            break;

    }

    system("pause");

    return 0;


}

The problem is that it when i type in my choice and hit enter, it skips past asking me for a string.

I remember hearing that you have to ask

cin

to throw away the enter or something?

The second part works fine by the way, if i add something to the text file using notepad, then it is displayed

Ive included the .exe in a .zip as well as a screenshot

Recommended Answers

All 7 Replies

I remember hearing that you have to ask cin to throw away the enter or something?

Yeah something like that. Try adding cin.ignore like this.

cin >> choice;
cin.ignore( 80, '\n' );
commented: thanks! +21

Mixing input methods is always a disaster waiting to happen, especially when you mix those which attempt to parse the input with those which just read the input. The different ways in which white-space and newlines get treated will catch you out.

Read a whole line into a string (preferably a std::string) then extract information from the string in memory.

It seems to be working now using

cin.ignore( 80, '\n' );

thanks Hamrick.

Salem, what would be the proper way to do this then? (im kind of a n00b at c++)

#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main ( ) {
    string input;

    if ( getline(cin,input) ) {
        istringstream convert(input);
        int result;
        if ( convert >> result ) {
            cout << "Success = " << result << endl;
        } else {
            cout << "Not an integer" << endl;
        }
    } else {
        cout << "No input" << endl;
    }

    return 0;
}

It might look like a lot of code compared to your 1-liner, but if you were to make your input bomb-proof, you'd be looking at a similar amount of code.

If your program does a lot of interactive I/O, then you'd create a number of wrapper functions to take care of some of the repetitiveness.

commented: Dead right! +11
commented: great little snippit you got there. +6

sounds good, i forgot about error handling ;)

ill try that later im going to try and modify it to search for strings (i hope to use a text file to hold information for a simple text based game im making)

Okay heresa question, using the code above, how could i get it to print each string on its own line, automatically enter them at the end and display them all

I know i use a loop but its the I/o im confused about

One example std::vector< std::string > words; Then in place of the cout, words.push_back( result ); At the end, a for loop to iterate over the vector to output all the words.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.