Help with coin code

Hello, I am looking for some help with this issue I am having, I think its something to do with my variables but I am not sure. The goal is to make a program that figures out how many cents I have based on how many quarters, nickels, and dimes I have. It performs fine when I use positive values such as 1 quarter, 2 dimes, 4 nickels. The issue I am having is when I place a negative value in for quarters it wont ask me for dimes but it will ask me for nickels. I want the final product to read like:
Please enter quarters: -2
Please enter dimes: 3
Please enter nickels: 1
Input values must be positive!
: for when I enter a negative value.

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
#include <iostream>
using namespace std;

int main() {

    int quarters, dimes, nickels;

    cout << "Lets figure out how much money you have in change.\n";
    cout << "Please enter quarters\n";
    cin >> quarters;

    if ( quarters >= 0 )
    {
        cout << "Please enter dimes\n";
        cin >> dimes;
    }

    if ( dimes >= 0 )
    {
        cout << "Please enter nickels\n";
        cin >> nickels;
    }

    if ( nickels >= 0 )
    {
        cout << "You have " << ( quarters * (25) + dimes * (10) + nickels * (5) ) << " cents.\n";
    }

    if ( ( quarters <= -1 ) || ( dimes <= -1 ) || ( nickels <= -1 ) )
    {
        cout << " Input Values must be positive\n";
    }


    return 0;
Last edited on
The issue I am having is when I place a negative value in for quarters it wont ask me for dimes

You explicitly tell it to ask for dimes only if quarters is non-negative:
1
2
3
4
5
if ( quarters >= 0 )
{
cout << "Please enter dimes\n";
cin >> dimes;
}
It seems so obvious when someone else points it out to you. We were working on if/else ifs so I thought I needed it in there. I changed the code to this.

#include <iostream>
using namespace std;

int main() {

int quarters, dimes, nickels;

cout << "Lets figure out how much money you have in change.\n";
cout << "Please enter quarters\n";
cin >> quarters;
cout << "Please enter dimes\n";
cin >> dimes;
cout << "Please enter nickels\n";
cin >> nickels;
cout << "You have " << ( quarters * (25) + dimes * (10) + nickels * (5) ) << " cents.\n";

if ( ( quarters <= -1 ) || ( dimes <= -1 ) || ( nickels <= -1 ) )
{
cout << " Input Values must be positive\n";
}

return 0;
}

Now it functions correctly. Thank you
Hello mikeyb,

No not really.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



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
#include <iostream>

using namespace std;

int main()
{
    int quarters, dimes, nickels;

    cout << "Lets figure out how much money you have in change.\n";

    cout << "Please enter quarters\n";
    cin >> quarters;

    cout << "Please enter dimes\n";
    cin >> dimes;

    cout << "Please enter nickels\n";
    cin >> nickels;

    cout << "You have " << (quarters * (25) + dimes * (10) + nickels * (5)) << " cents.\n";  // <--- Prints even if you have a negative number. () not needed.

    if ((quarters <= -1) || (dimes <= -1) || (nickels <= -1))  // <--- Check is after the fact.
    {
        cout << " Input Values must be positive\n";
    }

    return 0;
}

Make the if statement an if/else and move line 20 into the else part.

Andy
Why the brackets around the numbers? You don't need them.

 
quarters * 25 + dimes * 10 + nickels * 5


Topic archived. No new replies allowed.