| | |
If....else password statement
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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?
C++ Syntax (Toggle Plain Text)
#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? <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;
}
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;
}
•
•
Join Date: Dec 2004
Posts: 24
Reputation:
Solved Threads: 1
#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
#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
To yb1pls: Use code tags when you are posting some code - http://www.daniweb.com/techtalkforum...nouncementid=3
This is a little update... Thanks for the replies guys.
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,
C++ Syntax (Toggle Plain Text)
#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();
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;
} 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:
no www
no nonsense
coming soon to a pc near you! :cool:
btw cant you use another mini switch block for the Y/N problem????
ie, get the string (answer) and try something like the following...
this could probably be #define'd or put in a function so that it tidies up the code a bit
hope this helps..
ie, get the string (answer) and try something like the following...
C++ Syntax (Toggle Plain Text)
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..
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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- IO and Embedded SQL (Java)
- JDBC connection (Java)
Other Threads in the C++ Forum
- Previous Thread: Help w/Structs and 'For' loops
- Next Thread: C++ Help
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






