943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1817
  • C++ RSS
Apr 10th, 2005
0

Unexpected output when program exucuted

Expand Post »
The following code is not performing correctly. It opens the username file but does not display the mark.

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib> //these commands are pre processors directive
  2. #include <fstream> //required for library function
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12. cout << "Marking Program"<<endl;
  13.  
  14. string username;
  15.  
  16. cout << "Enter Students Username:";
  17. cin >> username;
  18.  
  19. string filename = username + ".txt";
  20. ifstream file(filename.c_str());
  21. if (!file) {
  22. cerr<<"Username not found"<<endl;
  23. return EXIT_FAILURE;
  24.  
  25.  
  26. string string1 ("DDRA");
  27. string string2 ("DDRB");
  28. string string3 ("0021");
  29. string string4 ("CLR");
  30. string string5 ("LDAA");
  31. string string6 ("LDAB");
  32. string string7 ("READ");
  33. string string8 ("ABA");
  34. string string9 ("STAA");
  35. string string10 ("END");
  36.  
  37.  
  38. int counter;
  39. counter=0;
  40. if (string1.find(filename.c_str()))
  41. ++counter;
  42.  
  43. if (string2.find(filename.c_str()))
  44. ++counter;
  45.  
  46. if (string3.find(filename.c_str()))
  47. ++counter;
  48.  
  49. if (string4.find(filename.c_str()))
  50. ++counter;
  51.  
  52. if (string5.find(filename.c_str()))
  53. ++counter;
  54.  
  55. if (string6.find(filename.c_str()))
  56. ++counter;
  57.  
  58. if (string7.find(filename.c_str()))
  59. ++counter;
  60.  
  61. if (string8.find(filename.c_str()))
  62. ++counter;
  63.  
  64. if (string9.find(filename.c_str()))
  65. ++counter;
  66.  
  67. if (string10.find(filename.c_str()))
  68. ++counter;
  69.  
  70. cout<< "students mark is"<<counter<<"out of 10"<<endl;
  71. return EXIT_SUCCESS;
  72. }
  73. }
after the username is entered the message press any key appears but no mark is displayed
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 10th, 2005
0

Re: Unexpected output when program exucuted

C++ Syntax (Toggle Plain Text)
  1. string string1 ("DDRA");
  2. string string2 ("DDRB");
  3. string string3 ("0021");
  4. string string4 ("CLR");
  5. string string5 ("LDAA");
  6. string string6 ("LDAB");
  7. string string7 ("READ");
  8. string string8 ("ABA");
  9. string string9 ("STAA");
  10. string string10 ("END");
This is a sign you should be using an array.
C++ Syntax (Toggle Plain Text)
  1. int counter;
  2. counter=0;
  3. if (string1.find(filename.c_str()))
  4. ++counter;
  5.  
  6. if (string2.find(filename.c_str()))
  7. ++counter;
  8.  
  9. if (string3.find(filename.c_str()))
  10. ++counter;
  11.  
  12. if (string4.find(filename.c_str()))
  13. ++counter;
  14.  
  15. if (string5.find(filename.c_str()))
  16. ++counter;
  17.  
  18. if (string6.find(filename.c_str()))
  19. ++counter;
  20.  
  21. if (string7.find(filename.c_str()))
  22. ++counter;
  23.  
  24. if (string8.find(filename.c_str()))
  25. ++counter;
  26.  
  27. if (string9.find(filename.c_str()))
  28. ++counter;
  29.  
  30. if (string10.find(filename.c_str()))
  31. ++counter;
This is a sign you should be using a loop.

>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)
  1. #include <cstdlib> //these commands are pre processors directive
  2. #include <fstream> //required for library function
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12. cout << "Marking Program"<<endl;
  13.  
  14. string username;
  15.  
  16. cout << "Enter Students Username:";
  17. cin >> username;
  18.  
  19. string filename = username + ".txt";
  20. ifstream file(filename.c_str());
  21. if (!file) {
  22. cerr<<"Username not found"<<endl;
  23. return EXIT_FAILURE;
  24.  
  25. string string1 ("DDRA");
  26. string string2 ("DDRB");
  27. string string3 ("0021");
  28. string string4 ("CLR");
  29. string string5 ("LDAA");
  30. string string6 ("LDAB");
  31. string string7 ("READ");
  32. string string8 ("ABA");
  33. string string9 ("STAA");
  34. string string10 ("END");
  35.  
  36. int counter;
  37. counter=0;
  38. if (string1.find(filename.c_str()))
  39. ++counter;
  40.  
  41. if (string2.find(filename.c_str()))
  42. ++counter;
  43.  
  44. if (string3.find(filename.c_str()))
  45. ++counter;
  46.  
  47. if (string4.find(filename.c_str()))
  48. ++counter;
  49.  
  50. if (string5.find(filename.c_str()))
  51. ++counter;
  52.  
  53. if (string6.find(filename.c_str()))
  54. ++counter;
  55.  
  56. if (string7.find(filename.c_str()))
  57. ++counter;
  58.  
  59. if (string8.find(filename.c_str()))
  60. ++counter;
  61.  
  62. if (string9.find(filename.c_str()))
  63. ++counter;
  64.  
  65. if (string10.find(filename.c_str()))
  66. ++counter;
  67.  
  68. cout<< "students mark is"<<counter<<"out of 10"<<endl;
  69. return EXIT_SUCCESS;
  70. }
  71. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 10th, 2005
0

Re: Unexpected output when program exucuted

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)
  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"<<endl;
  11. string username;
  12. cout << "Enter Students Username:";
  13. cin >> username;
  14.  
  15. string filename = username + ".txt";
  16. ifstream file(filename.c_str());
  17. if (!file)
  18. cerr<<"Username not found"<<endl;
  19.  
  20. string string1 ("DDRA");
  21. string string2 ("DDRB");
  22. string string3 ("0021");
  23. string string4 ("CLR");
  24. string string5 ("LDAA");
  25. string string6 ("LDAB");
  26. string string7 ("READ");
  27. string string8 ("ABA");
  28. string string9 ("STAA");
  29. string string10 ("END");
  30.  
  31. int counter;
  32. counter=0;
  33.  
  34. if (string1.find(filename.c_str()) == string::npos)
  35. ++counter;
  36.  
  37. if (string2.find(filename.c_str()) == string::npos)
  38. ++counter;
  39.  
  40. if (string3.find(filename.c_str()) == string::npos)
  41. ++counter;
  42.  
  43. if (string4.find(filename.c_str()) == string::npos)
  44. ++counter;
  45.  
  46. if (string5.find(filename.c_str()) == string::npos)
  47. ++counter;
  48.  
  49. if (string6.find(filename.c_str()) == string::npos)
  50. ++counter;
  51.  
  52. if (string7.find(filename.c_str()) == string::npos)
  53. ++counter;
  54.  
  55. if (string8.find(filename.c_str()) == string::npos)
  56. ++counter;
  57.  
  58. if (string9.find(filename.c_str()) == string::npos)
  59. ++counter;
  60.  
  61. if (string10.find(filename.c_str()) == string::npos)
  62. ++counter;
  63.  
  64. cout<< "students mark is"<< counter <<"out of 10"<<endl;
  65.  
  66. return EXIT_SUCCESS;
  67. }
Last edited by jonnie83; Apr 10th, 2005 at 6:40 pm. Reason: missing code definer
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 10th, 2005
0

Re: Unexpected output when program exucuted

>if (string1.find(filename.c_str()) == string::npos)
This tests for failure. The search succeeds if the result is not string::npos.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 10th, 2005
0

Re: Unexpected output when program exucuted

if (string1.find(filename.c_str()) == string::npos)
Does this line search for string1 in the file, or the file in string1?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Apr 10th, 2005
0

Re: Unexpected output when program exucuted

>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.
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: what is the problem
Next Thread in C++ Forum Timeline: C++ CString Class Help!





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


Follow us on Twitter


© 2011 DaniWeb® LLC