Help with a simple loop that decides if a word needs to be formatted by adding ('s) to it or not

Hi everyone,

I'm hoping I could get some help pointing me in the right direction. My lab assignment is asking us to write a simple program that asks the user for a price amount and you would always be paying more so there is always going to be change. My professor wants us to be able to write a loop that determines if there are "1" or "multiple" of each currency. For example if there is only 1 quarter he does not want the code to say "1 quarters" but instead "1 quarter". The other part I've been trying to work on is that I can only get my code to function when I output everything in "for" loops. I've been trying to get my code to not output currency that has "0" in it, so it skips over those and only outputs currency that is being used.

If anyone could point me in the right direction I would really appreciate it. Thanks!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
using std::ios;
using std::cout;
using std::endl;
using std::cin;

#include <iomanip>
using std::setprecision;


int main ()
{
  cout.setf(ios::fixed|ios::showpoint);
  cout << setprecision(2);

  //variables
    int fifty,twenty,ten,five,dollar,half,quarter,dime,nickel,penny;
    double due,payment,change;

    cout << "Please enter the price:";
    cin >> due;

    // Get the payment amount
    cout << "Please enter payment amount:";
    cin >> payment;
    // Check payment amount for problems

    if (payment > due)
	{
        change = payment - due;
        cout << "----------------------------------------------\n";
        cout << "Your change is $" << change << endl;

		for (fifty = 0; change >= 50.00; fifty++)
		{
			change -= 50.00;
		}

		for (twenty = 0; change >= 20.00; twenty++)
		{
			change -= 20.00;
		}

		for (ten = 0; change >= 10.00; ten++)
		{
			change -= 10.00;
		}

		for (five = 0; change >= 5.00; five++)
		{
			change -= 5.00;
		}

        for (dollar = 0; change >= 1.00; dollar++)
		{
            change -= 1.00;
        }    

		for (half = 0; change >= 0.50; half++)
		{
			change -= 0.50;
		}

        for (quarter = 0; change >= 0.25; quarter++)
		{
            change -= 0.25;
        }

        for (dime = 0; change >= 0.10; dime++)
		{
            change -= 0.10;
        }

        for (nickel = 0; change >= 0.05; nickel++)
		{
            change -= 0.05;
        }

        for (penny= 0; change > 0.01; penny++)
		{
            change -= 0.01;
        }

        cout <<  fifty << " $50 bills" << endl;
		cout <<	twenty << " $20 bills" << endl;
		cout << ten << " $10 bills" << endl;
		cout << five << " 5 bills" << endl;
		cout << dollar << " 1 bills" << endl;
		cout << half << " 50-cent coins" << endl;
		cout << quarter << " quarters" << endl;
		cout << dime << " dimes" << endl;
		cout << nickel << " nickels" << endl;
		cout << penny << " pennies" << endl;
	
	}

}




So I've been thinking of a better way to accomplish the skipping over the unused currency but this is what I came up with by just using a bunch of "if statements". I was thinking maybe I could use a "switch"?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
using std::ios;
using std::cout;
using std::endl;
using std::cin;

#include <iomanip>
using std::setprecision;


int main ()
{
  cout.setf(ios::fixed|ios::showpoint);
  cout << setprecision(2);

  //variables
    int fifty,twenty,ten,five,dollar,half,quarter,dime,nickel,penny;
    double due,payment,change;

    cout << "Please enter the price:";
    cin >> due;

    // Get the payment amount
    cout << "Please enter payment amount:";
    cin >> payment;
    // Check payment amount for problems

    if (payment > due)
	{
        change = payment - due;
        cout << "----------------------------------------------\n";
        cout << "Your change is $" << change << endl;

		for (fifty = 0; change >= 50.00; fifty++)
		{
			change -= 50.00;
		}

		for (twenty = 0; change >= 20.00; twenty++)
		{
			change -= 20.00;
		}

		for (ten = 0; change >= 10.00; ten++)
		{
			change -= 10.00;
		}

		for (five = 0; change >= 5.00; five++)
		{
			change -= 5.00;
		}

        for (dollar = 0; change >= 1.00; dollar++)
		{
            change -= 1.00;
        }    

		for (half = 0; change >= 0.50; half++)
		{
			change -= 0.50;
		}

        for (quarter = 0; change >= 0.25; quarter++)
		{
            change -= 0.25;
        }

        for (dime = 0; change >= 0.10; dime++)
		{
            change -= 0.10;
        }

        for (nickel = 0; change >= 0.05; nickel++)
		{
            change -= 0.05;
        }

        for (penny= 0; change > 0.01; penny++)
		{
            change -= 0.01;
        }

        cout <<  fifty << " $50 bills" << endl;
		cout <<	twenty << " $20 bills" << endl;
		cout << ten << " $10 bills" << endl;
		cout << five << " 5 bills" << endl;
		cout << dollar << " 1 bills" << endl;
		cout << half << " 50-cent coins" << endl;
		cout << quarter << " quarters" << endl;
		cout << dime << " dimes" << endl;
		cout << nickel << " nickels" << endl;
		cout << penny << " pennies" << endl;
	
if (fifty > 0)
		{
        cout <<  fifty << " $50 bills" << endl;
		}

		if (twenty > 0)
		{
		cout <<	twenty << " $20 bills" << endl;
		}

		if (ten > 0)
		{
		cout << ten << " $10 bills" << endl;
		}

		if (five > 0)
		{
		cout << five << " 5 bills" << endl;
		}

		if (dollar > 0)
		{
		cout << dollar << " 1 bills" << endl;
		}

		if (half > 0)
		{
		cout << half << " 50-cent coins" << endl;
		}

		if (quarter > 0)
		{
		cout << quarter << " quarters" << endl;
		}

		if (dime > 0)
		{
		cout << dime << " dimes" << endl;
		}

		if (nickel > 0)
		{
		cout << nickel << " nickels" << endl;
		}

		if (penny > 0)
		{
		cout << penny << " pennies" << endl;
		}
	}

}
Unrelated to the code, not meaning to be a grammar nazi, but no apostrophe is needed for plurals.
Check the quantity in an if statement and if it is greater than one add the s to the end like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout << "How many do you want?" << endl;
getline(cin, input);
int amount = input;
while(!int(input)){
    cout << "A number please." << endl;
    cin.sync();
    cin.clear();
    getline(cin,input);
}
if(int(input) > 1 && cointype(cointype.length()) != "y"){
  output = output + "s";
} else {
 // statement here or some crap.
}
if(cointype(cointype.length()) == "y"){
    cointype(cointype.length()) = "ies"
}
cout << "That'll cost " << amount << " " << cointype << "!" << endl;
Thanks for the tip. I was working on another way of doing it (a more tedious way), basically by using a ton of if statements. It seems like it's working fine but I was wondering if these was a better way of doing this. I was thinking about using switch statements but couldn't figure out how to get it to work.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        
               if (fifty >= 2)
		{
			cout <<  fifty << " $50 bills" << endl;
		}

		if (fifty == 1) 
		{
			cout <<  fifty << " $50 bill" << endl;
		}

		if (twenty >= 2)
		{
		cout <<	twenty << " $20 bills" << endl;
		}
		if (twenty == 1)
		{
		cout <<	twenty << " $20 bill" << endl;
		}

		if (ten >= 2)
		{
		cout << ten << " $10 bills" << endl;
		}
		if (ten == 1)
		{
		cout << ten << " $10 bill" << endl;
		}
		
		if (five >= 2)
		{
		cout << five << " $5 bills" << endl;
		}
		if (five == 1)
		{
		cout << five << " 5 bill" << endl;
		}

		if (dollar >= 2)
		{
		cout << dollar << " $1 bills" << endl;
		}
		if (dollar == 1)
		{
		cout << dollar << " 1 bill" << endl;
		}

		if (half >= 2)
		{
		cout << half << " 50-cent coins" << endl;
		}
		if (half == 1)
		{
		cout << half << " 50-cent coin" << endl;
		}

		if (quarter >= 2)
		{
		cout << quarter << " 25-cent coins" << endl;
		}
		if (quarter == 1)
		{
		cout << quarter << " quarters" << endl;
		}

		if (dime >= 2)
		{
		cout << dime << " 10-cent coins" << endl;
		}
		if (dime == 1)
		{
		cout << dime << " 10-cent coin" << endl;
		}

		if (nickel >= 2)
		{
		cout << nickel << " 5-cent coins" << endl;
		}
		if (nickel == 1)
		{
		cout << nickel << " 5-cent coin" << endl;
		}

		if (penny >= 2)
		{
		cout << penny << " 1-cent coins" << endl;
		}
		if (penny == 1)
		{
		cout << penny << " 1-cent coin" << endl;
		}
Last edited on
Topic archived. No new replies allowed.