c++ homework

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

Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

c++ homework

 
0
  #1
Feb 22nd, 2005
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 ')'




  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 31
Reputation: mcldev is an unknown quantity at this point 
Solved Threads: 2
mcldev mcldev is offline Offline
Light Poster

Re: c++ homework

 
0
  #2
Feb 22nd, 2005
: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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: c++ homework

 
0
  #3
Feb 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: c++ homework

 
0
  #4
Feb 23rd, 2005
Don't use void main; it's always int
  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 !=
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: c++ homework

 
0
  #5
Feb 23rd, 2005
thx for the excellent help all!!!
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC