944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6927
  • C++ RSS
Feb 22nd, 2005
0

c++ homework

Expand Post »
ok, help!!! I'm totally lost. I have to use a file stream program and add names to file, then keep count of file names, then display the count of names entered and the names entered. Use a loop to enter names until prompt to end.

these are just two of the errors I get when trying to use for loops. I had error with while loop also.

error C2143: syntax error : missing ';' before ')'
error C2143: syntax error : missing ';' before ')'




C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h> // This is used for the cout's and cin's
  2. #include <fstream.h> // This is used to read from and write to files
  3. #include <stdlib.h> // This gives us the exit(1) command
  4.  
  5.  
  6. void main()
  7. {
  8. char filename[12];
  9. char name [20];
  10. int count = 0;
  11. int x = 0;
  12. char end;
  13. int y = 0;
  14.  
  15. // int ch;
  16.  
  17.  
  18. cout << "Enter the filename: ";
  19. cin >> filename;
  20. cout << endl;
  21.  
  22. // Creating an instance of outbound data
  23. ofstream outfile;
  24.  
  25. outfile.open(filename, ios::app);
  26.  
  27. if (outfile.fail())
  28. {
  29. cout <<"The file failed to open" <<endl;
  30. exit(1);
  31. }
  32.  
  33.  
  34. cout << "Enter names to the file called: "<< filename <<endl;
  35. cout << "Enter end to stop entering names." << endl;
  36.  
  37.  
  38. cout << endl;
  39.  
  40.  
  41. count = 1;
  42. while(name != "end");
  43. {
  44. cout << "Enter names: " << endl;
  45. cin >> name;
  46. count++;
  47. cout << endl;
  48.  
  49. }
  50.  
  51.  
  52. //send data to outfile
  53. outfile << name;
  54.  
  55. outfile << "\n";
  56.  
  57. // Closing the outfile to free up memory
  58. outfile.close();
  59.  
  60.  
  61.  
  62. // Creating an instance of inbound data
  63. ifstream infile;
  64.  
  65. // Opening the infile "filename" for reading
  66. infile.open(filename);
  67.  
  68. // Reading the contents of "filename"
  69. infile >> name;
  70.  
  71.  
  72.  
  73. infile.close();
  74.  
  75.  
  76. for(y = 0; y <= count; y++)
  77. {
  78.  
  79. cout << "You have entered " << count << "names"<< endl;
  80. }
  81.  
  82. for( x = 0, x <= 20, x++)
  83. {
  84. cout <<"The name you have entered is: " << name << endl;
  85. }
  86.  
  87. }
Last edited by alc6379; Feb 23rd, 2005 at 6:17 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cap2361 is offline Offline
13 posts
since Feb 2005
Feb 22nd, 2005
0

Re: c++ homework

:lol: Single step the code with your debugger. That will reveal exaclty where it is choking and how. BTW, the while loop has a subtle defect.
Reputation Points: 10
Solved Threads: 3
Light Poster
mcldev is offline Offline
31 posts
since Feb 2005
Feb 22nd, 2005
0

Re: c++ homework

thx I found the two errors for loop I had a comma instead of semi colon.

yes while loop is buggy and driving me nuts.
I have error warning C4101: 'end' : unreferenced local variable
been going through book and past programs I did and will keep trying to figure out what I am doing wrong.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cap2361 is offline Offline
13 posts
since Feb 2005
Feb 23rd, 2005
0

Re: c++ homework

Don't use void main; it's always int
C++ Syntax (Toggle Plain Text)
  1. while(name != "end");
  2. {
  3. ..........
The while statements never ends with a semicolon.
You are comparing in fact 2 strings (or char arrays); for this, you should use strcmp, not !=
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Feb 23rd, 2005
0

Re: c++ homework

thx for the excellent help all!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cap2361 is offline Offline
13 posts
since Feb 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: Chargen -- A character Generator
Next Thread in C++ Forum Timeline: Reading Binary files via stdin





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


Follow us on Twitter


© 2011 DaniWeb® LLC