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.

#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 != '4');

   return 0;
}

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?

You have given a case ‘4’: but you haven’t displayed it on the menu.
You 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

similarly for case ‘2’ keep a separate counter.

when you 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;
    }

Besides, you do not have a break statement for case 3. You need to put break statements for each case.

#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

commented: Use code tags. +0

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

#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");
                   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;
                              }
                 
                   
         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;
}

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,

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();
         [B]break;[/B]

         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();
         [B]break;[/B]

         if ( choice == 1 )
         {
            gwb = gwb + 1;
         }
         else if ( choice == 2 )
         {
            jfk = jfk + 1;
         }

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;
}

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

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

switch (answer)
{
    case 'y':
    case 'Y':
    {
        // User says yes, do something
    }
    break;

    case 'n':
    case 'N':
    {
        // User said no, do something else
    }
    break; 
    default:
    {
        // didnt understand / error
    }
    break;
}

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

hope this helps.. :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.