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