If....else password statement

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

Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

If....else password statement

 
0
  #1
Dec 6th, 2004
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.

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

Re: If....else password statement

 
0
  #2
Dec 7th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: nvanevski is an unknown quantity at this point 
Solved Threads: 1
nvanevski nvanevski is offline Offline
Newbie Poster

Re: If....else password statement

 
0
  #3
Dec 8th, 2004
Besides, you do not have a break statement for case 3. You need to put break statements for each case.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 24
Reputation: yb1pls is an unknown quantity at this point 
Solved Threads: 1
yb1pls yb1pls is offline Offline
Newbie Poster

Re: If....else password statement

 
-1
  #4
Dec 8th, 2004
#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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: If....else password statement

 
0
  #5
Dec 8th, 2004
To yb1pls: Use code tags when you are posting some code - http://www.daniweb.com/techtalkforum...nouncementid=3
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: If....else password statement

 
0
  #6
Dec 8th, 2004
This is a little update... Thanks for the replies guys.

  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,
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,346
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: If....else password statement

 
0
  #7
Dec 8th, 2004
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;
         }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: If....else password statement

 
0
  #8
Dec 14th, 2004
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;
}
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: If....else password statement

 
0
  #9
Dec 14th, 2004
btw cant you use another mini switch block for the Y/N problem????

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

  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
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: If....else password statement

 
0
  #10
Dec 14th, 2004
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
May 'the Google' be with you!
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