Won't go to else statement
Feb 2, 2013 at 10:18pm UTC
I'm trying to get this program to receive a response from the user of "Y" or "NO" or something similar. The program checks the conditions properly for the "N" but when I input "Y" or similar it doesn't do the conditional check and jumps right into the "Bi-Weekly" section. Any ideas why it's doing this?
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
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char * pszArgz[])
{
int inputBalance;
int savingsBalance;
string monthyCalculationQuestion;
cout << "This program is an approximation tool." << endl;
cout << endl;
cout << endl;
cout << "Would you like to do a monthly calculation? Y/N" << endl;
cin >> monthyCalculationQuestion;
if (monthyCalculationQuestion == "N" ||"n" ||"No" ||"no" ||"no " ||"No " ||"NO" ||"NO " )
{
cout << "Bi-Weekly Calculation." << endl;
cout << endl;
cout << "Please input your current paycheck." << endl;
cout << "Balance: " ;
cin >> inputBalance;
if (inputBalance >= 540)
{
inputBalance = inputBalance - 55;
cout << "After Cigarettes (Withdrawal -55$) $" << inputBalance << endl;
inputBalance = inputBalance - 150;
cout << "After Gas (Withdrawal -150$) $" << inputBalance << endl;
inputBalance = inputBalance - 200;
cout << "After Cell Bill (Withdrawal -200$) $" << inputBalance << endl;
inputBalance = inputBalance - 35;
cout << "After Internet Bill (Withdrawal -35$) $" << inputBalance << endl;
inputBalance = inputBalance - 100;
cout << "After Play Money (Withdrawal -100$) $" << inputBalance << endl;
int savingsBalance;
cout << endl;
cout << endl;
cout << "Please enter savingsBalance." << endl;
cout << "Balance: " ;
cin >> savingsBalance;
if (savingsBalance >=0)
{
savingsBalance = savingsBalance + inputBalance;
cout << "Your savings will be up to: $" << savingsBalance << " if you deposit $" << inputBalance << "." << endl;
}
else
{
cout << "You do not have a savings account but if you deposit " << inputBalance << "you can start one today!" << endl;
}
}
}
else
{
cout << "TEST" << endl;
}
}
Feb 2, 2013 at 10:47pm UTC
if (monthyCalculationQuestion == "N" ||"n" ||"No" ||"no" ||"no " ||"No " ||"NO" ||"NO " )
You can't do this^. You need to write out every single case:
if (monthyCalculationQuestion == "N" || monthyCalculationQuestion == "n" || ...)
To make it easier,
you could use a switch statement, or transform the string into all lowercase before doing any checks.
Last edited on Feb 2, 2013 at 11:10pm UTC
Feb 2, 2013 at 10:49pm UTC
Can you give a short example of this for me @Branflakes91093? Don't do it for me I'm trying to learn lol. Thanks in advance!
Last edited on Feb 2, 2013 at 11:02pm UTC
Feb 2, 2013 at 11:18pm UTC
Actually, disregard what I said about the switch statement, you can't do that with strings.
Since you ask the user only for "Y/N", you could do this:
1 2 3 4 5 6
if (user entered 'n' or 'N' )
...
if (user entered 'y' or 'Y' )
...
else
//tell the user their input could not be accepted
Or, if you want to check for all of the possible combinations, you're going to have to write it all out. You can use the tolower() function in <cctype> to make the string lowercase:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <cctype>
#include <string>
using std::string;
string str;
...
for (int i = 0; i < str.length(); ++i)
str[i] = tolower(str[i]);
...
//or
#include <cctype>
#include <algorithm>
#include <string>
using std::string;
string str;
...
std::transform(str.begin(), str.end(), str.begin(), tolower);
...
Feb 2, 2013 at 11:21pm UTC
I simply went and made an option to have the user use numeral values rather than a string. It feels more robotic but I haven't a clue how to convert it into lower case letter nor is it possible to use strings within a switch statement.
Feb 2, 2013 at 11:48pm UTC
Else, savings == 0
The if on top is SavingsBalance > = 0;
If you savings is 0;
then you can start an account
do this
if (savingsBalance > 0)
take the = out and it will work.
'
Topic archived. No new replies allowed.