| | |
Missing output from program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 24
Reputation:
Solved Threads: 0
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)
#include <cstdlib> #include <fstream> #include <iostream> #include <cstring> #include <string> using namespace std; int main() { cout << " Marking Program "; //Start of program string username; cout << "Enter Students Username:"; cin >> username; string filename = username + ".txt"; ifstream file(filename.c_str()); if (!file) { cerr<<"Username not found"<<endl; return EXIT_FAILURE; } { ifstream in("MarkScheme.txt"); if (!in){ cerr<<"Mark Scheme not found"<<endl; return EXIT_FAILURE; } } //beginning of section of code not working string search; int count(0); if (!getline(cin, search)) { count; } string line; while (getline(cin, line)) { if (line.find(search) == string::npos) ++count; } cout<< "students mark is"<<count<<"out of 10"<<endl; return EXIT_SUCCESS; }
C++ Syntax (Toggle Plain Text)
if (!getline(cin, search)) { count; }
C++ Syntax (Toggle Plain Text)
{ ifstream in("MarkScheme.txt"); if (!in){ cerr<<"Mark Scheme not found"<<endl; return EXIT_FAILURE; } } // End local
I have no idea what you want your code to do, so I can't give you an improved version.
I'm here to prove you wrong.
•
•
Join Date: Feb 2005
Posts: 24
Reputation:
Solved Threads: 0
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
A naive implementation would look something like this:
A better implementation would look more like this:
And there would be degrees of improvement as you went away from the manual version to an even more abstracted version.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> using namespace std; int main() { string line; cout<<"Enter a file to open: "; getline ( cin, line ); ifstream in ( line.c_str() ); if ( in ) { int count = 0; while ( getline ( in, line ) ) { ifstream markstream ( "MarkScheme.txt" ); string mark; while ( getline ( markstream, mark ) ) { if ( mark == line ) ++count; } } cout<<"The total count is "<< count <<endl; } }
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <set> #include <string> using namespace std; set<string> load_file ( const string& filename ) { ifstream in ( filename.c_str() ); if ( !in ) return set<string>(); string line; set<string> ret; while ( getline ( in, line ) ) ret.insert ( line ); return ret; } int main() { string filename; cout<<"Enter a file to open: "; getline ( cin, filename ); set<string> user = load_file ( filename ); set<string> mark = load_file ( "MarkScheme.txt" ); int count = 0; for ( set<string>::const_iterator it = user.begin(); it != user.end(); ++it ) { if ( mark.find ( *it ) != mark.end() ) ++count; } cout<<"The total count is "<< count <<endl; }
I'm here to prove you wrong.
>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:
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)
if ( mark.find ( line ) != string::npos ) ++count;
I'm here to prove you wrong.
![]() |
Similar Threads
- Weird build errors that are not even in my program Please help!! (C++)
- Vectors, Functions, and Sorting... oh my! (C++)
- Tapemaking program (Java)
- Troubled Baseball program (C++)
- water program using subprograms (Computer Science)
- system registry missing, Missing system.ini (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Why compiler complants error when compile.
- Next Thread: help me in this home work please
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






