Writing to a file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 179
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Writing to a file

 
0
  #1
Nov 28th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,562
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1484
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Writing to a file

 
0
  #2
Nov 28th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC