943,503 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3701
  • C++ RSS
Dec 6th, 2004
0

If....else password statement

Expand Post »
I am having a little trouble incorporating a simple password into a voting machine style program. If anybody can see what's wrong with it and point it out, I would be very grateful.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include<string>
  4. #include<cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char choice;
  11. string password = "jim";
  12. string guess = "";
  13.  
  14. do
  15. {
  16. system("color F0");
  17. system("CLS");
  18. cout << "\n\n\n\n";
  19.  
  20.  
  21. cout << setw(50) << "Voting Menu";
  22. cout << endl << setw(40) << " George W. Bush" << ": 1 " ;
  23. cout << endl << setw(40) << " John F. Kerry" << ": 2 ";
  24. cout << endl << setw(40) << " EXIT" << ": 3 " << endl;
  25. cout << endl << endl << setw(40) << "Please enter your choice :";
  26. cin >> choice;
  27. cin.get();
  28.  
  29. switch( choice)
  30. {
  31. case '1': system("CLS");
  32. cout << "\n\n\n\n";
  33. cout << setw(50) << "Thank you for voting. " << endl;
  34. cin.get();
  35. break;
  36.  
  37. case '2': system("CLS");
  38. cout << "\n\n\n\n";
  39. cout << setw(50) <<"Thank you for voting" << endl;
  40. cin.get();
  41. break;
  42.  
  43. case '3': system("CLS");
  44. cout << "\n\n\n\n";
  45. cout << setw(50) <<"Please enter supervisors password :" ;
  46. cin >> guess;
  47.  
  48.  
  49. if(guess == password)
  50. {
  51.  
  52. cout << endl << endl << endl << setw(10) << ""
  53. << "Are you sure you want to quit and start counting votes? <Y/N> " << endl;
  54.  
  55.  
  56. }
  57.  
  58.  
  59. else
  60. {
  61. cin.get();
  62. }
  63.  
  64. case '4': break;
  65.  
  66. }
  67.  
  68.  
  69. }while( choice != '4');
  70.  
  71. return 0;
  72. }

See after if(guess == password), it must revert back to the original screen if the password is incorrect. If the password is correct it will continue onto a Y/N option.

There's much more to do but I should be able to complete those parts on my own...hopefully.
BTW, is there a tutorial that explains simple Y/N parts?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Marauder is offline Offline
25 posts
since Nov 2004
Dec 7th, 2004
0

Re: If....else password statement

u have given a case `4`: but u haven`t displayed it on the menu.
u must go on calculating while the voting is going on
e.g; if the
case is `1` then gw++
where gw is a counter for George Bush

similaraly for case `2` keep a separate conter.

when u r asking the user to count the votes
i.e; cout << endl << endl << endl << setw(10) << ""
<< "Are you sure you want to quit and start counting votes? <Y/N> " << endl;

then take the user`s choice as input
e.g; cin>>ans;

now check if(ans==`y` or ans == `Y`)
{then simply display the results
i.e; cout<<gw; and the other one
and then come out of the loop by using : break;
}
Reputation Points: 10
Solved Threads: 1
Light Poster
varunrathi is offline Offline
41 posts
since Aug 2004
Dec 8th, 2004
0

Re: If....else password statement

Besides, you do not have a break statement for case 3. You need to put break statements for each case.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nvanevski is offline Offline
12 posts
since Dec 2004
Dec 8th, 2004
-1

Re: If....else password statement

#include <iostream>
#include <iomanip>
#include<string>
#include<cstdlib>

using namespace std;

int main()
{
char choice;
string password = "jim";
string guess = "";

do
{
system("color F0");
system("CLS");
cout << "\n\n\n\n";


cout << setw(50) << "Voting Menu";
cout << endl << setw(40) << " George W. Bush" << ": 1 " ;
cout << endl << setw(40) << " John F. Kerry" << ": 2 ";
cout << endl << setw(40) << " EXIT" << ": 3 " << endl;
cout << endl << endl << setw(40) << "Please enter your choice :";
cin >> choice;
cin.get();

switch( choice)
{
case '1': system("CLS");
cout << "\n\n\n\n";
cout << setw(50) << "Thank you for voting. " << endl;
cin.get();
break;

case '2': system("CLS");
cout << "\n\n\n\n";
cout << setw(50) <<"Thank you for voting" << endl;
cin.get();
break;

case '3': system("CLS");
cout << "\n\n\n\n";
cout << setw(50) <<"Please enter supervisors password :" ;
cin >> guess;


if(guess == password)
{

cout << endl << endl << endl << setw(10) << ""
<< "Are you sure you want to quit and start counting votes? <Y/N> " << endl;


}


else
{
cin.get();
}

case '4': break;

}


}while( choice != '3');

return 0;
}

email me if you have any problems on yb1pls@yahoo.co.uk
Reputation Points: 11
Solved Threads: 1
Newbie Poster
yb1pls is offline Offline
24 posts
since Dec 2004
Dec 8th, 2004
0

Re: If....else password statement

To yb1pls: Use code tags when you are posting some code - http://www.daniweb.com/techtalkforum...nouncementid=3
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 8th, 2004
0

Re: If....else password statement

This is a little update... Thanks for the replies guys.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include<string>
  4. #include<cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char choice;
  11. string correctpassword = "jim";
  12. string password = "";
  13. char ans;
  14. int gwb = 0;
  15. int jfk = 0;
  16.  
  17. do
  18. {
  19. system("color F0");
  20. system("CLS");
  21. cout << "\n\n\n\n";
  22.  
  23.  
  24. cout << setw(50) << "Voting Menu";
  25. cout << endl << setw(40) << " George W. Bush" << ": 1 " ;
  26. cout << endl << setw(40) << " John F. Kerry" << ": 2 ";
  27. cout << endl << setw(40) << " EXIT" << ": 3 " << endl;
  28. cout << endl << endl << setw(40) << "Please enter your choice :";
  29. cin >> choice;
  30. cin.get();
  31.  
  32. switch( choice)
  33. {
  34. case '1': system("CLS");
  35. cout << "\n\n\n\n";
  36. cout << setw(50) << "Thank you for voting. " << endl;
  37. cin.get();
  38. break;
  39.  
  40. cout << setw(30) <<"Please enter supervisors password to enable next voter:" ;
  41. getline(cin, password);
  42.  
  43.  
  44.  
  45.  
  46. case '2': system("CLS");
  47. cout << "\n\n\n\n";
  48. cout << setw(50) <<"Thank you for voting" << endl;
  49. cin.get();
  50. break;
  51.  
  52. if(choice == 1)
  53. {
  54. gwb = gwb + 1;
  55. }
  56. else if(choice == 2)
  57. {
  58. jfk = jfk + 1;
  59. }
  60.  
  61.  
  62. case '3': system("CLS");
  63. cout << "\n\n\n\n";
  64. cout << setw(50) <<"Please enter supervisors password :" ;
  65. getline(cin, password);
  66.  
  67.  
  68. if(password != correctpassword)
  69. {
  70. cout << "\n\n\n\n";
  71. cout << setw(50) <<"Please enter supervisors password :" ;
  72. getline(cin, password);
  73.  
  74. }
  75.  
  76. else
  77. {
  78. cout << endl << endl << endl << setw(10) << ""
  79. << "Are you sure you want to quit and start counting votes? <Y/N> " << endl;
  80. cin >> ans;
  81.  
  82.  
  83. cout << setw(30) << "Totals for this election. ";
  84. cout << endl << setw(40) << " George W. Bush: " << "\n\n\n" << gwb ;
  85. cout << endl << setw(40) << " John F. Kerry: " << "\n\n\n" << jfk ;
  86. }
  87.  
  88. break;
  89. }
  90.  
  91.  
  92. }while( choice != '4');
  93.  
  94.  
  95. cin.get();
  96. return 0;
  97. }

What I don't know how to do is the little (Yes/No) feature. I googled it but no luck. Does is work like my password?

Marauder,
Reputation Points: 10
Solved Threads: 0
Light Poster
Marauder is offline Offline
25 posts
since Nov 2004
Dec 8th, 2004
0

Re: If....else password statement

Did you know you have unreachable code?
      switch ( choice )
      {
      case '1': system("CLS");
         cout << "\n\n\n\n";
         cout << setw(50) << "Thank you for voting. " << endl;
         cin.get();
         break;

         cout << setw(30) <<"Please enter supervisors password to enable next voter:" ;
         getline(cin, password);

      case '2': system("CLS");
         cout << "\n\n\n\n";
         cout << setw(50) <<"Thank you for voting" << endl;
         cin.get();
         break;

         if ( choice == 1 )
         {
            gwb = gwb + 1;
         }
         else if ( choice == 2 )
         {
            jfk = jfk + 1;
         }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 14th, 2004
0

Re: If....else password statement

Incorporating the vote counting into the switch statement will solve one of the unreachable segments

#include <iostream>
#include <iomanip>
#include<string>
#include<cstdlib>

using namespace std;

int main()
{
   char choice;
   string correctpassword = "jim";
   string password = "";
   char ans;
   int gwb = 0;
   int jfk = 0;
   
do
   {
      system("color F0");
      system("CLS");
      cout << "\n\n\n\n";


      cout << setw(50) << "Voting Menu";
      cout << endl << setw(40) << " George W. Bush" << ": 1 " ;
      cout << endl << setw(40) << " John F. Kerry" << ": 2 ";
      cout << endl << setw(40) << "  EXIT" << ": 3 " << endl;
      cout << endl << endl << setw(40) << "Please enter your choice :";
      cin >> choice;
      cin.get();

      switch( choice)
      {
         case '1': system("CLS");
                   gwb++;
                   cout << "\n\n\n\n";
                   cout << setw(50) << "Thank you for voting. " << endl;
                   cin.get();
                   break;
                                                     
         case '2': system("CLS");
                   jfk++;
                   cout << "\n\n\n\n";
                   cout << setw(50) <<"Thank you for voting" << endl;
                   cin.get();
                   break;
                    
/* Should this not be after the switch so that you are asked AFTER the program has analyzed the choice?

                cout << setw(30) <<"Please enter supervisors password to enable next voter:" ;
                   getline(cin, password);
                   
*/
         case '3': system("CLS");
                   cout << "\n\n\n\n";
                   cout << setw(50) <<"Please enter supervisors password :" ;
                   getline(cin, password);
         
                   
         if(password != correctpassword)
         {
               cout << "\n\n\n\n";
               cout << setw(50) <<"Please enter supervisors password :" ;
               getline(cin, password);
              
         }
           
         else 
           {
               cout << endl << endl << endl << setw(10) << ""
           << "Are you sure you want to quit and start counting votes? <Y/N> " << endl;
           cin >> ans; 
           
      
            cout << setw(30) << "Totals for this election. ";
            cout << endl << setw(40) << " George W. Bush: " << "\n\n\n" << gwb ;
            cout << endl << setw(40) << " John F. Kerry: " << "\n\n\n" << jfk ;
                  }           
       
       break;      
     }                  
   
   
}while( choice != '4');


cin.get();
return 0;
}
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 14th, 2004
0

Re: If....else password statement

btw cant you use another mini switch block for the Y/N problem????

ie, get the string (answer) and try something like the following...

C++ Syntax (Toggle Plain Text)
  1. switch (answer)
  2. {
  3. case 'y':
  4. case 'Y':
  5. {
  6. // User says yes, do something
  7. }
  8. break;
  9.  
  10. case 'n':
  11. case 'N':
  12. {
  13. // User said no, do something else
  14. }
  15. break;
  16. default:
  17. {
  18. // didnt understand / error
  19. }
  20. break;
  21. }

this could probably be #define'd or put in a function so that it tidies up the code a bit

hope this helps..
Last edited by 1o0oBhP; Dec 14th, 2004 at 2:05 am. Reason: addition
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 14th, 2004
0

Re: If....else password statement

Quote originally posted by nvanevski ...
Besides, you do not have a break statement for case 3. You need to put break statements for each case.
Funny way to do it, but case 4 provides the break for case 3
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 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: Help w/Structs and 'For' loops
Next Thread in C++ Forum Timeline: C++ Help





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


Follow us on Twitter


© 2011 DaniWeb® LLC