While Statement inputs the last input
Oct 2, 2014 at 2:48am UTC
Alright, so this is pretty self explanatory, but the while statement in my program is using the last input whenever there is a cin which then puts the program into an endless loop. What am I doing wrong?
Sorry for the size of the program and yes this is homework that I really need some help on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int pin; // PIN #
int money = 0; // Money to withdraw
int tens = 0; // Number of $10
int fives = 0; // Number of $5
int ones = 0; // Number of $1
bool finreq; // Finish Requirement
bool okayreq; // Okay-to-process Requirement
int response; // Yes or no response
cout << "Enter your PIN please: " ; // PIN message
std::cin >> pin; // Read PIN
while ( pin > 0 ) // If PIN is good
{
finreq = false ; // Request to finish is false
while ( finreq != true ) // While request is false
{
cout << "Enter amount to withdraw: " ; // Withdrawl message
cin >> money; // Read money value
cout << endl;
okayreq = true ;
while ( okayreq != false )
{
if ( money > 500 )
{
cout << "Invalid amount: " << money << endl;
okayreq = false ;
}
else if ( money <= 0 )
{
cout << "Invalid amount: " << money << endl;
okayreq = false ;
}
else
{
if ( pin % 2 == 0 )
{
if ( ( money % 5 != 0 ) )
{
cout << "You are a type 2 customer, but the amount you requested was " << money << '.' << endl;
cout << "You will be charged $1 for changing your default setting." << endl;
cout << "Do you want to be charged $1? (Y/N)" << endl;
cin >> response;
if ( ( response = 'y' ) || ( response = 'Y' ) )
{
cout << "You requested $" << money << endl;
cout << "You have been charged $1." << endl;
cout << "You are recieving the following bills:" << endl;
tens = money / 10;
fives = ( money - ( tens * 10 ) ) / 5;
ones = ( money - ( ( tens * 10 ) + ( fives * 5 ) ) );
cout << " Number of $10 bills: " << tens << endl;
cout << " Number of $5 bills: " << fives << endl;
cout << " Number of $1 bills: " << ones << endl;
okayreq = false ;
finreq = true ;
}
else
{
okayreq = false ;
}
}
else if ( ( money % 5 != 0 ) || (okayreq == true ) )
{
cout << "You have requested $" << money << endl;
cout << "You are receiving the following bills:" << endl;
tens = money / 10;
fives = ( money - ( tens * 10 ) ) / 5;
ones = ( money - ( ( tens * 10 ) + ( fives * 5 ) ) );
cout << " Number of $10 bills: " << tens << endl;
cout << " Number of $5 bills: " << fives << endl;
cout << " Number of $1 bills: " << ones << endl;
okayreq = false ;
finreq = true ;
}
}
else if ( (pin % 2 != 0) && (okayreq == true ) )
{
cout << "You have requested $" << money << endl;
cout << "You are receiving the following bills:" << endl;
tens = money / 10;
fives = ( money - ( tens * 10 ) ) / 5;
ones = ( money - ( ( tens * 10 ) + ( fives * 5 ) ) );
cout << " Number of $10 bills: " << tens << endl;
cout << " Number of $5 bills: " << fives << endl;
cout << " Number of $1 bills: " << ones << endl;
okayreq = false ;
finreq = true ;
}
}
}
}
cout << "Please enter PIN: " ;
cin >> pin;
}
cout << "The program will terminate!" << endl;
return 0;
}
Oct 2, 2014 at 4:11am UTC
in line 18
, you declare int response;
as int
.
In line 54
, you ask the user for a char
.
cout << "Do you want to be charged $1? (Y/N)" << endl;
The user then enters the char
which you stored as int
. Boom!!
Declare response
as char
.
Oct 3, 2014 at 6:41pm UTC
THANK YOU SO MUCH!
I would have never found that!
Topic archived. No new replies allowed.