| | |
Unexpected output when program exucuted
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 24
Reputation:
Solved Threads: 0
The following code is not performing correctly. It opens the username file but does not display the mark.
after the username is entered the message press any key appears but no mark is displayed
C++ Syntax (Toggle Plain Text)
#include <cstdlib> //these commands are pre processors directive #include <fstream> //required for library function #include <iostream> #include <cstring> #include <string> using namespace std; int main() { cout << "Marking Program"<<endl; 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; string string1 ("DDRA"); string string2 ("DDRB"); string string3 ("0021"); string string4 ("CLR"); string string5 ("LDAA"); string string6 ("LDAB"); string string7 ("READ"); string string8 ("ABA"); string string9 ("STAA"); string string10 ("END"); int counter; counter=0; if (string1.find(filename.c_str())) ++counter; if (string2.find(filename.c_str())) ++counter; if (string3.find(filename.c_str())) ++counter; if (string4.find(filename.c_str())) ++counter; if (string5.find(filename.c_str())) ++counter; if (string6.find(filename.c_str())) ++counter; if (string7.find(filename.c_str())) ++counter; if (string8.find(filename.c_str())) ++counter; if (string9.find(filename.c_str())) ++counter; if (string10.find(filename.c_str())) ++counter; cout<< "students mark is"<<counter<<"out of 10"<<endl; return EXIT_SUCCESS; } }
C++ Syntax (Toggle Plain Text)
string string1 ("DDRA"); string string2 ("DDRB"); string string3 ("0021"); string string4 ("CLR"); string string5 ("LDAA"); string string6 ("LDAB"); string string7 ("READ"); string string8 ("ABA"); string string9 ("STAA"); string string10 ("END");
C++ Syntax (Toggle Plain Text)
int counter; counter=0; if (string1.find(filename.c_str())) ++counter; if (string2.find(filename.c_str())) ++counter; if (string3.find(filename.c_str())) ++counter; if (string4.find(filename.c_str())) ++counter; if (string5.find(filename.c_str())) ++counter; if (string6.find(filename.c_str())) ++counter; if (string7.find(filename.c_str())) ++counter; if (string8.find(filename.c_str())) ++counter; if (string9.find(filename.c_str())) ++counter; if (string10.find(filename.c_str())) ++counter;
>after the username is entered the message press any key appears but no mark is displayed
Your braces are misplaced. Here's the code with proper indentation:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> //these commands are pre processors directive #include <fstream> //required for library function #include <iostream> #include <cstring> #include <string> using namespace std; int main() { cout << "Marking Program"<<endl; 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; string string1 ("DDRA"); string string2 ("DDRB"); string string3 ("0021"); string string4 ("CLR"); string string5 ("LDAA"); string string6 ("LDAB"); string string7 ("READ"); string string8 ("ABA"); string string9 ("STAA"); string string10 ("END"); int counter; counter=0; if (string1.find(filename.c_str())) ++counter; if (string2.find(filename.c_str())) ++counter; if (string3.find(filename.c_str())) ++counter; if (string4.find(filename.c_str())) ++counter; if (string5.find(filename.c_str())) ++counter; if (string6.find(filename.c_str())) ++counter; if (string7.find(filename.c_str())) ++counter; if (string8.find(filename.c_str())) ++counter; if (string9.find(filename.c_str())) ++counter; if (string10.find(filename.c_str())) ++counter; cout<< "students mark is"<<counter<<"out of 10"<<endl; return EXIT_SUCCESS; } }
I'm here to prove you wrong.
•
•
Join Date: Feb 2005
Posts: 24
Reputation:
Solved Threads: 0
Changed sections of the code and an answer is now generated, only problem is is is always 10 out of ten. anythoughts
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <fstream> #include <iostream> #include <cstring> #include <string> using namespace std; int main() { cout << "Marking Program"<<endl; string username; cout << "Enter Students Username:"; cin >> username; string filename = username + ".txt"; ifstream file(filename.c_str()); if (!file) cerr<<"Username not found"<<endl; string string1 ("DDRA"); string string2 ("DDRB"); string string3 ("0021"); string string4 ("CLR"); string string5 ("LDAA"); string string6 ("LDAB"); string string7 ("READ"); string string8 ("ABA"); string string9 ("STAA"); string string10 ("END"); int counter; counter=0; if (string1.find(filename.c_str()) == string::npos) ++counter; if (string2.find(filename.c_str()) == string::npos) ++counter; if (string3.find(filename.c_str()) == string::npos) ++counter; if (string4.find(filename.c_str()) == string::npos) ++counter; if (string5.find(filename.c_str()) == string::npos) ++counter; if (string6.find(filename.c_str()) == string::npos) ++counter; if (string7.find(filename.c_str()) == string::npos) ++counter; if (string8.find(filename.c_str()) == string::npos) ++counter; if (string9.find(filename.c_str()) == string::npos) ++counter; if (string10.find(filename.c_str()) == string::npos) ++counter; cout<< "students mark is"<< counter <<"out of 10"<<endl; return EXIT_SUCCESS; }
Last edited by jonnie83; Apr 10th, 2005 at 6:40 pm. Reason: missing code definer
>Does this line search for string1 in the file, or the file in string1?
You wrote code and don't know what it does? The member function find is called on behalf of string1 with the argument filename.c_str(). So it's searching for filename in string1.
I got the feeling straight away that you have no idea what you're doing, and now I'm pretty sure of it. Perhaps you should work with pseudocode until you're comfortable with the design of your program. That way you won't be confused with piddling details until the time is right.
You wrote code and don't know what it does? The member function find is called on behalf of string1 with the argument filename.c_str(). So it's searching for filename in string1.
I got the feeling straight away that you have no idea what you're doing, and now I'm pretty sure of it. Perhaps you should work with pseudocode until you're comfortable with the design of your program. That way you won't be confused with piddling details until the time is right.
I'm here to prove you wrong.
![]() |
Similar Threads
- Pascal Help (Pascal and Delphi)
- .exe crashing on executing output !! (C)
- Tapemaking program (Java)
- water program using subprograms (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: what is the problem
- Next Thread: C++ CString Class Help!
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






