well this is weird

edit
Last edited on
Your code is working fine. You always try to print out the values of quarters, etc. even if the function computer_coins() had "failed" due to an invalid value of coin_value and didn't make any changes to quarters, etc.
not what you mean? The values it returns for say 112 cents is way off. thanks for the reply
Have you considered using a debugger to step through and look at the values of the variables as the program progresses? That'll probably help you find any errors in your algorithm.
not sure how the debugger works honestly. The code works great 0-99, only outside of that does it throw weird numbers. And before I added the if else, there was no problems. Im guessing its in the else statement, would anyone know how to terminate the else statement after the cout?
Pepper12704 wrote:
edit

why would you remove your post? Someone may google search and land up here, but now this thread is useless to help them.

Think of others.
Last edited on
His thread's title sucks anyway; nobody will ever know what it was about.
closed account (28poGNh0)
coin_value, quarters,num and amount_left are variables that contains random values at the first place because they are not initialized ,to check this write :

1
2
3
4
cout << coin_value << endl;
.
.
cout << amount_left << endl;


after the declaration of these variables

In the computer_coins function ,when your coin_values is between 0 and 99 those variables got assigned ,the random values are gone ..everything is ok

when you enter a coin_value,whice is out of rang(0,99) you got the error only because the parameters are referenced, every change occurs to these parameters affects directly to coin_value, quarters,num and amount_left

try to imagine this scenario

int coin_value;// may contains randomly 2365487
int change; // may contains randomly 548154
int quarters; // may contains randomly 658954
int num; // may contains randomly 8547452
int amount_left; // may contains randomly 2395874

You input the coin_value(for exp 455) ,now only of coin_value is changed to to 455

the program call the function computer_coins and send the parameters
computer_coins(455,2365487,548154,658954)

In the function ,the if condition is not correcte, goes to else ..,function ended
these are still the same

finaly you got an input
455cents will be:
2365487quarters
548154dimes
658954pennies



Please next time dont edit,Your problem my help others
Last edited on
closed account (28poGNh0)
Fortunatily I still have the source code

Problem why when input a number outside the rang(0,99) we got an random numbers for quarters ,dimes and pennies ,thats what I remmembered

source code

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

void computer_coins(int coin_value, int& num, int& amount_left, int& quarters);

int main ( )

{
  const int Quart_value = 25;
  const int Dime_value = 10;
  const int Pennie_value = 1;


char ans;
int coin_value;
int change;
int quarters;
int num;
int amount_left;

    do
        {

        cout << "please enter a number of pennies from 1 to 99" << endl;
        cin >> coin_value;

        computer_coins(coin_value, num, amount_left, quarters);

        cout << coin_value << "cents will be:" << endl;
        cout << quarters << "quarters" << endl;
        cout << num << "dimes"  << endl;
        cout << amount_left << "pennies" << endl;

        cout << "would you like to try again?" << endl;
        cin  >> ans;
}
        while (ans == 'y' || ans == 'Y');
        return 0;
}
void computer_coins(int coin_value, int& num, int& amount_left, int& quarters)
{

    if (coin_value >= 0 && coin_value < 100)
        {
        quarters = (coin_value/25);
        num = (coin_value - (quarters * 25)) / 10;
        amount_left = coin_value - (quarters * 25+ num * 10);}

else

    {cout << "incorrect value, would you like to try again?" <<  endl;}
}
Last edited on
Topic archived. No new replies allowed.