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?
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? " << 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<
Besides, you do not have a break statement for case 3. You need to put break statements for each case.
#include
#include
#include
#include
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? " << endl;
}
else
{
cin.get();
}
case '4': break;
}
}while( choice != '3');
return 0;
}
email me if you have any problems on [email]yb1pls@yahoo.co.uk[/email]
To yb1pls: Use code tags when you are posting some code - http://www.daniweb.com/techtalkforums/announcement.php?f=8&announcementid=3
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();
<strong>break;</strong>
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();
<strong>break;</strong>
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