Having some trouble with an if-else statement
Sep 4, 2012 at 2:14pm UTC
I've been trying to figure out what could be the problem for a while, but still can't figure it out. This is the source code for a revolving credit account. I'm getting an error saying expected primary expression before "else".
The error is on the if-else line
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
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
double accountBalance, interestDue, totalAmountDue, minimumPayment;
cout << "Please enter the account balance for your credit account: " ;
cin >> accountBalance;
cout << "Thanks.\n" ;
if (accountBalance > 1000)
{
interestDue = (accountBalance * (.01));
totalAmountDue = interestDue + accountBalance;
if (totalAmountDue > 10)
{ minimumPayment = totalAmountDue * .10;
}
else
{
minimumPayment = (totalAmountDue);
}
else if (accountBalance < 1000 && accountBalance > 10)
{
interestDue = (accountBalance * 1.5);
totalAmountDue = interestDue + accountBalance;
}
else
{
totalAmountDue = accountBalance;
}
cout << interestDue << "\n." ;
cout << totalAmountDue << "\n." ;
cout << minimumPayment << "\n." ;
system("PAUSE" );
return EXIT_SUCCESS;
}
Anyone have ideas on what may be the problem here?
Thanks
Last edited on Sep 4, 2012 at 2:45pm UTC
Sep 4, 2012 at 2:18pm UTC
If you indent your code properly, you'll see a lot more.
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
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
double accountBalance, interestDue, totalAmountDue, minimumPayment;
cout << "Please enter the account balance for your credit account: " ;
cin >> accountBalance;
cout << "Thanks.\n" ;
if (accountBalance > 1000)
{
interestDue = (accountBalance * (.01));
totalAmountDue = interestDue + accountBalance;
if (totalAmountDue > 10)
{
minimumPayment = totalAmountDue * .10;
}
else // MISSING SOMETHING HERE?
{
minimumPayment = (totalAmountDue);
}
else if (accountBalance < 1000 && accountBalance > 10)
{
interestDue = (accountBalance * 1.5);
totalAmountDue = interestDue + accountBalance;
}
else
{
totalAmountDue = accountBalance;
}
cout << interestDue << "\n." ;
cout << totalAmountDue << "\n." ;
cout << minimumPayment << "\n." ;
system("PAUSE" );
return EXIT_SUCCESS;
}
// MISSING SOMETHING HERE?
Sep 4, 2012 at 2:38pm UTC
I have been indenting correctly, how do I copy the source code in that form?
Sep 4, 2012 at 2:39pm UTC
Sep 4, 2012 at 2:44pm UTC
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
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
double accountBalance, interestDue, totalAmountDue, minimumPayment;
cout << "Please enter the account balance for your credit account: " ;
cin >> accountBalance;
cout << "Thanks.\n" ;
if (accountBalance > 1000)
{
interestDue = (accountBalance * (.01));
totalAmountDue = interestDue + accountBalance;
if (totalAmountDue > 10)
{ minimumPayment = totalAmountDue * .10;
}
else
{
minimumPayment = (totalAmountDue);
}
else if (accountBalance < 1000 && accountBalance > 10)
{
interestDue = (accountBalance * 1.5);
totalAmountDue = interestDue + accountBalance;
}
else
{
totalAmountDue = accountBalance;
}
cout << interestDue << "\n." ;
cout << totalAmountDue << "\n." ;
cout << minimumPayment << "\n." ;
system("PAUSE" );
return EXIT_SUCCESS;
}
Okay, thank you. This is how I have it indented. I have an if statement inside the if statement. My problem is with the if-else
I think..
Last edited on Sep 4, 2012 at 2:45pm UTC
Sep 4, 2012 at 2:46pm UTC
Yes, before line 26 there's a missing }
Sep 4, 2012 at 2:50pm UTC
You're right. Such a simple mistake, thank you.
Sep 4, 2012 at 2:51pm UTC
As coder777 pointed out, all the following is inside one if statement.
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
interestDue = (accountBalance * (.01));
totalAmountDue = interestDue + accountBalance;
if (totalAmountDue > 10)
{ minimumPayment = totalAmountDue * .10;
}
else
{
minimumPayment = (totalAmountDue);
}
else if (accountBalance < 1000 && accountBalance > 10)
{
interestDue = (accountBalance * 1.5);
totalAmountDue = interestDue + accountBalance;
}
else
{
totalAmountDue = accountBalance;
}
cout << interestDue << "\n." ;
cout << totalAmountDue << "\n." ;
cout << minimumPayment << "\n." ;
system("PAUSE" );
return EXIT_SUCCESS;
Your indentation is nice but you have indented according to what you think is the logic. You should indent according to the code. In your code above, why is line 26 not indented? You meant to put a
} there, but it isn't there. Indent according to the code,
not according to what you think is the program flow.
Last edited on Sep 4, 2012 at 2:51pm UTC
Topic archived. No new replies allowed.