Soundwave
Soundwave

Reputation: 1

How to add money denominations c++

very new programmer, I am having a difficult time trying to find a way to set up this problem. I have money denominations , one dollar through one hundred dollar. A user must input lets say for example 4 one dollar, and 5 five dollar, and receive a sum of 29 dollars. I am at a loss, I have been trying here is what i have...

{   // dollar values 

    int n = std::numeric_limits<int>::max();
    int b = std::numeric_limits<int>::max();
    int dollarOne = 1;
    int dollarTwo = 2;
    int sum1;                                               // defines sum 


    cin >> n >> dollarOne; 
    sum1 = ((n*dollarOne)+(b*dollarTwo);                                // sum function 


    cout << sum1 << endl;                               // displays total amount


    system("pause");
    return 0;
}

New int dollarAm1; int dollarAm2; int sum; cin >> dollarAm1; cin >> dollarAm2; sum = ((dollarAm1 * 1) + (dollarAm2 * 2));

    cout << sum << endl; 

Upvotes: 0

Views: 610

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 595732

Try something more like this instead (error handling removed for brevity):

{
    int n;
    int sum1 = 0;

    cout << "How many $1 bills: ";
    cin >> n;
    sum1 += (n*1);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $5 bills: ";
    cin >> n;
    sum1 += (n*5);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $10 bills: ";
    cin >> n;
    sum1 += (n*10);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $20 bills: ";
    cin >> n;
    sum1 += (n*20);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $50 bills: ";
    cin >> n;
    sum1 += (n*50);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $100 bills: ";
    cin >> n;
    sum1 += (n*100);

    cout << "Total: $" << sum1 << endl;

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.get();

    return 0;
}

Upvotes: 1

Andre
Andre

Reputation: 1257

int n = 0;
int b = 0;
int dollarOne = 1;
int dollarFive = 5;
int sum1; // defines sum


std::cin >> n;
std::cin >> b;

sum1 = (n*dollarOne)+(b*dollarFive); // sum function


std::cout << sum1 << std::endl; // displays total amount


system("pause");
return 0;

Upvotes: 1

Don Larynx
Don Larynx

Reputation: 695

You're missing a parentheses

sum1 = ((n*dollarOne)+(b*dollarTwo);

Next, you should use cin.get() to end your programs, not system("PAUSE"). This is more effective in terms of processing speed. Also , you should simply do int n, b; to initialize the variables.

You should read input like so:

std::cout << "Enter number of 1-dollar bills, and press \"Enter\". Next, enter number of 5-dollar bills:" << std::endl;

cin >> dollarOne;
cin >> dollarTwo;

Upvotes: 1

Related Questions