Home › Forums › C Programming › Some problems in C++ › Re: Re: Some problems in C++
January 27, 2009 at 8:48 pm
#3513
GWILouisaxwzkla
Participant
This seems to work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <br /> <br /> #include <iostream><br /> #include <fstream><br /> #include <string.h><br /> #include <stdlib.h><br /> #include <ctype.h><br /> #define MAX_NAME 1000<br /> <br /> <br /> using namespace std;<br /> char sendtext;<br /> char username [ MAX_NAME ];<br /> <br /> void read(){<br /> system("cls");<br /> cout << "You autorised as:" << username << "n";<br /> ifstream in;<br /> in.open("c:\programs\in1.txt");<br /> if (!in) {<br /> cout << "Input file cannot be opened.n";<br /> //return 1;<br /> }<br /> char str[20000];<br /> while (!in.eof()) {<br /> in >> str;<br /> cout << str << "n";<br /> <br /> }<br /> cout << "n";<br /> in.close();<br /> <br /> }<br /> void write ()<br /> {<br /> char badInput;<br /> char choice;<br /> cout << "do you want to input data to file ( y or n )" << endl;<br /> cin >> choice;<br /> cin.get(); //get newline<br /> while ( tolower ( choice ) != 'y' && tolower ( choice ) != 'n' )<br /> {<br /> badInput = cin.get();<br /> while ( badInput != 'n' ) //clear garbage out of stream<br /> {<br /> badInput = cin.get();<br /> }<br /> cout << "bad choice!" << endl;<br /> cout << "do you want to input data to file ( y or n )" << endl;<br /> cin >> choice;<br /> cin.get(); //get newline<br /> }<br /> <br /> if ( tolower ( choice ) == 'y' )<br /> {<br /> char inputText [ MAX_NAME ];<br /> cout << "enter input text: " << endl;<br /> cin.getline ( inputText , MAX_NAME , 'n' );<br /> <br /> ofstream outputFile;<br /> //PUT YOUR DATA FILE NAME HERE!<br /> outputFile.open ( "msngr.txt" , ios::app );<br /> outputFile << inputText << endl;<br /> outputFile.close();<br /> read();<br /> write();<br /> }<br /> <br /> }<br /> main(){<br /> cout << "Enter your username: ";<br /> cin >> username;<br /> cin.get();<br /> read();<br /> write();<br /> <br /> <br /> return (0);<br /> }<br /> </fstream></iostream> |