944,062 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1706
  • C++ RSS
Apr 7th, 2005
0

Missing output from program

Expand Post »
The following program executes without any errors, but the output is not what is expected. The code is correct down till the opening of the MarkScheme.txt file, but the student mark is not displayed on the screen, any thoughts

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6.  
  7. using namespace std;
  8. int main()
  9. {
  10. cout << " Marking Program "; //Start of program
  11.  
  12. string username;
  13.  
  14. cout << "Enter Students Username:";
  15. cin >> username;
  16.  
  17. string filename = username + ".txt";
  18. ifstream file(filename.c_str());
  19. if (!file) {
  20. cerr<<"Username not found"<<endl;
  21. return EXIT_FAILURE;
  22. }
  23.  
  24. { ifstream in("MarkScheme.txt");
  25. if (!in){
  26. cerr<<"Mark Scheme not found"<<endl;
  27. return EXIT_FAILURE;
  28. }
  29. }
  30. //beginning of section of code not working
  31. string search;
  32. int count(0);
  33. if (!getline(cin, search)) {
  34. count;
  35. }
  36.  
  37. string line;
  38. while (getline(cin, line)) {
  39. if (line.find(search) == string::npos)
  40. ++count;
  41. }
  42.  
  43. cout<< "students mark is"<<count<<"out of 10"<<endl;
  44.  
  45. return EXIT_SUCCESS;
  46. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 7th, 2005
0

Re: Missing output from program

C++ Syntax (Toggle Plain Text)
  1. if (!getline(cin, search)) {
  2. count;
  3. }
If getline fails, you basically do nothing here. You're reading the value of count (0) and doing nothing with it.
C++ Syntax (Toggle Plain Text)
  1. { ifstream in("MarkScheme.txt");
  2. if (!in){
  3. cerr<<"Mark Scheme not found"<<endl;
  4. return EXIT_FAILURE;
  5. }
  6. } // End local
This also does practically nothing. Because the definition of in is inside braces, it's local to that block. When you get to the "End local" comment, it's destroyed. I can imagine that you wanted to use in for later input, but had to change it to cin because the compiler said in didn't exist.

I have no idea what you want your code to do, so I can't give you an improved version.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 8th, 2005
0

Re: Missing output from program

What i want the code to do is open the mark scheme file. In that is 10 strings. I want to see if these strings are present in the file opened at the beginning of the code. Every string found I want the counter to be incremented and once the final string has been searched for I want the value of the counter displayed to the operator
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 8th, 2005
0

Re: Missing output from program

A naive implementation would look something like this:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10.  
  11. cout<<"Enter a file to open: ";
  12. getline ( cin, line );
  13.  
  14. ifstream in ( line.c_str() );
  15.  
  16. if ( in ) {
  17. int count = 0;
  18.  
  19. while ( getline ( in, line ) ) {
  20. ifstream markstream ( "MarkScheme.txt" );
  21. string mark;
  22.  
  23. while ( getline ( markstream, mark ) ) {
  24. if ( mark == line )
  25. ++count;
  26. }
  27. }
  28.  
  29. cout<<"The total count is "<< count <<endl;
  30. }
  31. }
A better implementation would look more like this:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. set<string> load_file ( const string& filename )
  9. {
  10. ifstream in ( filename.c_str() );
  11.  
  12. if ( !in )
  13. return set<string>();
  14.  
  15. string line;
  16. set<string> ret;
  17.  
  18. while ( getline ( in, line ) )
  19. ret.insert ( line );
  20.  
  21. return ret;
  22. }
  23.  
  24. int main()
  25. {
  26. string filename;
  27.  
  28. cout<<"Enter a file to open: ";
  29. getline ( cin, filename );
  30.  
  31. set<string> user = load_file ( filename );
  32. set<string> mark = load_file ( "MarkScheme.txt" );
  33. int count = 0;
  34.  
  35. for ( set<string>::const_iterator it = user.begin(); it != user.end(); ++it ) {
  36. if ( mark.find ( *it ) != mark.end() )
  37. ++count;
  38. }
  39.  
  40. cout<<"The total count is "<< count <<endl;
  41. }
And there would be degrees of improvement as you went away from the manual version to an even more abstracted version.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 10th, 2005
0

Re: Missing output from program

I have tried both recommended programs but all the final count always displays as 0. How should the markscheme.txt file be set up, is this the problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 10th, 2005
0

Re: Missing output from program

>I have tried both recommended programs
Both recommended programs were suggestions to get you started. Naturally, you would need to modify them to suit your needs. For example, if you wanted to do a substring search then comparison with == will most likely not do what you want. In that case you would probably use the find member function of the string class:
C++ Syntax (Toggle Plain Text)
  1. if ( mark.find ( line ) != string::npos )
  2. ++count;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Why compiler complants error when compile.
Next Thread in C++ Forum Timeline: help me in this home work please





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


Follow us on Twitter


© 2011 DaniWeb® LLC