Unexpected output when program exucuted

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

Unexpected output when program exucuted

 
0
  #1
Apr 10th, 2005
The following code is not performing correctly. It opens the username file but does not display the mark.

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,738
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Unexpected output when program exucuted

 
0
  #2
Apr 10th, 2005
  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.
  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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 24
Reputation: jonnie83 is an unknown quantity at this point 
Solved Threads: 0
jonnie83 jonnie83 is offline Offline
Newbie Poster

Re: Unexpected output when program exucuted

 
0
  #3
Apr 10th, 2005
Changed sections of the code and an answer is now generated, only problem is is is always 10 out of ten. anythoughts

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,738
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Unexpected output when program exucuted

 
0
  #4
Apr 10th, 2005
>if (string1.find(filename.c_str()) == string::npos)
This tests for failure. The search succeeds if the result is not string::npos.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 24
Reputation: jonnie83 is an unknown quantity at this point 
Solved Threads: 0
jonnie83 jonnie83 is offline Offline
Newbie Poster

Re: Unexpected output when program exucuted

 
0
  #5
Apr 10th, 2005
if (string1.find(filename.c_str()) == string::npos)
Does this line search for string1 in the file, or the file in string1?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,738
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Unexpected output when program exucuted

 
0
  #6
Apr 10th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC