Home Forums C Programming Some problems in C++ Re: Re: Some problems in C++

#3513
GWILouisaxwzkla
Participant

This seems to work:



#include
#include
#include
#include
#include
#define MAX_NAME 1000


using namespace std;
char sendtext;
char username [ MAX_NAME ];

void read(){
system("cls");
cout << "You autorised as:" << username << "n";
ifstream in;
in.open("c:\programs\in1.txt");
if (!in) {
cout << "Input file cannot be opened.n";
//return 1;
}
char str[20000];
while (!in.eof()) {
in >> str;
cout << str << "n";

}
cout << "n";
in.close();

}
void write ()
{
char badInput;
char choice;
cout << "do you want to input data to file ( y or n )" << endl;
cin >> choice;
cin.get(); //get newline
while ( tolower ( choice ) != 'y' && tolower ( choice ) != 'n' )
{
badInput = cin.get();
while ( badInput != 'n' ) //clear garbage out of stream
{
badInput = cin.get();
}
cout << "bad choice!" << endl;
cout << "do you want to input data to file ( y or n )" << endl;
cin >> choice;
cin.get(); //get newline
}

if ( tolower ( choice ) == 'y' )
{
char inputText [ MAX_NAME ];
cout << "enter input text: " << endl;
cin.getline ( inputText , MAX_NAME , 'n' );

ofstream outputFile;
//PUT YOUR DATA FILE NAME HERE!
outputFile.open ( "msngr.txt" , ios::app );
outputFile << inputText << endl;
outputFile.close();
read();
write();
}

}
main(){
cout << "Enter your username: ";
cin >> username;
cin.get();
read();
write();


return (0);
}