944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1717
  • C++ RSS
Nov 28th, 2007
0

Writing to a file

Expand Post »
Hi all
My friend sent me a source file that wrote hello world to a text file (im a noob), I tried to modify it and give myself a challange to make a small database but it has gone horribly wrong. I dont know how to get it so when the person enters n it goes back to the start of the program, but im also getting errors all over the shop.
Here are the error messages coming up, im using Dev C++ Beta 5.something
1)answer does not name a type
2)djout undeclared
3)unknown escape sequence /T
4)getline undeclared
5)answer undeclared

c++ Syntax (Toggle Plain Text)
  1. #include<fstream>// headerfile for output/input
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10. string personname;
  11. string activity;
  12. string arriving;
  13. string answer
  14. ofstream djout; // declare our djoutput
  15. djout.open("C:\TEST.txt"); // opens/creates and edits "TEST.txt"
  16.  
  17. cout << "Please enter the persons name: ";
  18. getline (cin, personname);
  19. cout << "Please enter the activity: ";
  20. getlline (cin, activity);
  21. cout << "How are they arriving: ";
  22. getline (cin, arriving);
  23. cout << "Please confirm the details: ";
  24. cout << "Name: " << personname << endl;
  25. cout << "Activity: " << activity << endl;
  26. cout << "Arriving by: " << arriving << endl;
  27. cout << "Are the details correct? Y/N" << endl;
  28. getline (cin, answer);
  29.  
  30.  
  31. if (answer == 'y')
  32. {
  33. djout << personname << endl;
  34. djout << activity << endl;
  35. djout << arriving << endl;
  36. djout.flush(); // to save the file, you must close the file, or flush
  37. djout.close(); // the buffer to the file Closing the file will not let
  38. // you access it anymore, so only call it when you are
  39. // done using the file and it will automatically save
  40. // the file for you. Flushing the buffer will write the
  41. // buffer to the file and still keep the file open, so
  42. // use this function when necessary.
  43.  
  44. }
  45. if (answer == 'n')
  46. {
  47. //I want it to go back to the beginning of the program
  48. }
  49. else
  50. {
  51. cout << "System error sorry guys im not always right :)" << endl;
  52. }
  53.  
  54. system("pause");
  55. return 0;
  56. }

Thanks for your help

HLA91
Last edited by Ancient Dragon; Nov 28th, 2007 at 1:49 pm. Reason: add line numbers to existing code tags
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 28th, 2007
0

Re: Writing to a file

line 13 -- you forgot the semicolon at the end.

line 15: need to escape the '\' character djout.open("C:\\TEST.txt");
I'm not going to tell you the rest of the errors. Just fix them one at a time and compile after each correction.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with Sort() file TXT
Next Thread in C++ Forum Timeline: Adding arrays, selections, and a quit option





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC