Al13y
Al13y

Reputation: 1

Why is my code not outputting (just) numbers?

exercise prompt for code: Write a program that tells what coins to give for any amount of change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half-dollar coins. Your program will use the following function (among others): void compute_coins(int coin_value, int& num, int& amount_left);

#include <iostream>
#include <string>
using namespace std;

void prompt(int *amount_left);
void remaining_change(int *amount_left, int coin_value);
void compute_coins(int coin_value, int *num, int *amount_left);
void output(string coin_name, int *num);



int main() {
    int change = 0, num = 0, amount_left = 0;
    const int quarter = 25, dime = 10, penny = 1;
    string q = "quarter(s)", d = "dime(s)", p = "penny(s)"; 

    prompt(&change);
    compute_coins(quarter, &num, &amount_left);
    remaining_change(&amount_left, quarter);
    output(q, &num);

    compute_coins(dime, &num, &amount_left);
    remaining_change(&amount_left, dime);
    output(d, &num);

    compute_coins(penny, &num, &amount_left);
    output(p, &num);

}

void prompt(int *change)
{
  cout << "How much change is there? ";
  cin >> *change;
  cout << "You entered " << change << endl;
  cout << "That is equal to: ";
}

void remaining_change(int *amount_left, int coin_value)
{
    *amount_left = (*amount_left % coin_value);
}
void compute_coins(int coin_value, int *num, int *amount_left)
{
   *num = *amount_left / coin_value; 
}

void output(string coin_name,int *num)
{
    cout << num << " " << coin_name << ", ";
}

Upvotes: 0

Views: 155

Answers (2)

Paul R
Paul R

Reputation: 213060

In prompt(), change is a pointer, so in order to output the value that change points to you would need to modify this line:

cout << "You entered " << change << endl;

to:

cout << "You entered " << *change << endl;

Better still, though, you could use a reference rather than a pointer:

void prompt(int &change)
{
    cout << "How much change is there? ";
    cin >> change;
    cout << "You entered " << change << endl;
    cout << "That is equal to: ";
}

and then you would just call this as:

prompt(change);

This is much more idiomatic C++ – the pointer method is more "old skool" C-style programming.

Ditto for the other places where you are printing the pointer itself, e.g. num.

Upvotes: 1

TartanLlama
TartanLlama

Reputation: 65640

You are outputting the value of the pointers, not the value of the object pointed to.

The simple fix is to dereference the pointers first:

cout << "You entered " << *change << endl;
//                        ^

cout << *num << " " << coin_name << ", ";
//      ^

However, I'd suggest not using pointers for things like this at all. For built-in types you should take a reference when you want to update the variable and a value otherwise.

Personally I wouldn't update those variables from inside the functions either, I'd carry out the necessary input or calculation and return a value to be assigned.

Upvotes: 1

Related Questions