Need help but not understanding

Oct 4, 2018 at 9:16pm
So my program and everything works. I just need to be able to allow the user to rerun the program with char y, but it just ends. Do you guys have any ideas? I can't use :
for(;;)
while(1)
while(true)
do{//code}while(1);

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


int main()

{
	char choice = 'y';
	int QUARTERS = 25;
	int DIMES = 10;
	int NICKELS = 5;
	int PENNIES = 1;
	int change, numQuarters, numDimes, numNickels, numPennies;

	{
		cout << " Enter an amount : ";
		cin >> change;
		if (change > 0)

		{

			numQuarters = change / QUARTERS;
			change -= numQuarters * QUARTERS;
			numDimes = change / DIMES;
			change -= numDimes * DIMES;
			numNickels = change / NICKELS;
			change -= numNickels * NICKELS;
			numPennies = change / PENNIES;
			change -= numPennies * PENNIES;

			if (numQuarters != 0) // this represent the amount of quarter there are
				if (numQuarters > 1)
					cout << " There are Quarters " << numQuarters;
				else
					cout << " There is Quarter " << numQuarters << endl;
					cout << endl;
			if (numDimes != 0) // this represent the amount of dimes there are
				if (numDimes>1)
					cout << " There are Dimes " << numDimes;
				else
					cout << " There is Dime " << numDimes << endl;
					cout << endl;

			if (numNickels != 0) // this represent the amount of nickels there are
				if (numNickels > 1)
					cout << " There are Nickels " << numNickels;
				else
					cout << " There is Nickel " << numNickels;
					cout << endl;

			if (numPennies != 0) // this represent the amount of pennies there are
				if (numPennies > 1)
					cout << " There are Pennies " << numPennies;
				else
					cout << " There is Penny " << numPennies;
			cout << endl;
		}
		else
			cout << "Invalid amount" << endl;
		cout << "Would you like to try another number? press(y). Anything else will quit : "; 
		cin >> choice;
	}
	system("pause");
	return 0;
}.


Output:
Enter an Amount:13
There is dime 1
There are pennies 3
Would you like to try another number? press(y). Anything else will quit: y
press any key to continue...

Last edited on Oct 4, 2018 at 9:17pm
Oct 4, 2018 at 9:22pm
Use a loop. Your instructor gave you a hint for where to put it with his 'extra' { } pair.
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
while (choice == 'y')
{
    // ...
}
Last edited on Oct 4, 2018 at 9:23pm
Oct 4, 2018 at 9:22pm
The thing I know with loop it will work, but I'm not allowed to use loop.
Last edited on Oct 4, 2018 at 9:23pm
Oct 4, 2018 at 9:24pm
No where does it say you're not allowed to use loops. It looks like he just doesn't want you using infinite loops.
Can I see the exact text of the requirements?
Last edited on Oct 4, 2018 at 9:26pm
Oct 4, 2018 at 9:26pm
No global variables
No labels or go-to statements
No infinite loops, examples include:
for(;;)
while(1)
while(true)
do{//code}while(1);
No break statements to exit loops
If you violate any of these restrictions, you will automatically get a score of ZERO!

This is my professor requirements...
Oct 4, 2018 at 9:27pm
Yes, there we go. No infinite loops. Infinite being the key word.

What I wrote is not an infinite loop, does not use labels or goto, and does not use break statements.
Last edited on Oct 4, 2018 at 9:28pm
Oct 4, 2018 at 9:27pm
Nothing in the above says you can't use a loop. The loop just can't be an infinite loop.

Oct 4, 2018 at 9:31pm
Wouldn't while (choice == 'y') be considered while(true)?
It still does infinite loops when I inputted the code.
Oct 4, 2018 at 9:33pm
An infinite loop is one with no "break" or exit statements, and where the condition inside the "while (condition)" always evaluates to true.

while (choice == 'y) will only evaluate to true if... choice equals 'y'.

At line 61, you enter a new value for choice. If that value is not y, the loop will end, therefore it is not infinite.
Last edited on Oct 4, 2018 at 9:36pm
Oct 4, 2018 at 9:35pm
No. Somewhere in the loop you will be able to set the value of choice, therefore it is not an endless loop. An endless loop has no way to exit the loop other than break, exit(), or return.
Oct 4, 2018 at 9:35pm
wckedsck wrote:
It still does infinite loops when I inputted the code.
Show the code...

jlb wrote:
An endless loop has no way to exit the loop other than break, exit(), or return
Heh, technically the requirements didn't mention return statements, depends on how literal we're being about the term "break". It also doesn't mention using undefined behavior, or using OS calls to end the loop as well. So many possibilities!
Last edited on Oct 4, 2018 at 9:47pm
Oct 4, 2018 at 9:43pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
	char choice = 'y';
	int QUARTERS = 25;
	int DIMES = 10;
	int NICKELS = 5;
	int PENNIES = 1;
	int change, numQuarters, numDimes, numNickels, numPennies;
	while (choice == 'y')
	{
}
		else
			cout << "Invalid amount" << endl;
		cout << "Would you like to try another number? press(y). Anything else will quit : "; 
		cin >> choice;
	}


Oct 4, 2018 at 9:44pm
Your indentation is super misleading. Remove the closing curly brace on line 10. And you'll then need to fix and remove probably another curly brace somewhere else in the code.
Last edited on Oct 4, 2018 at 9:45pm
Oct 4, 2018 at 9:50pm
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
#include <iostream>
using namespace std;


int main()

{
	char choice = 'y';
	int QUARTERS = 25;
	int DIMES = 10;
	int NICKELS = 5;
	int PENNIES = 1;
	int change, numQuarters, numDimes, numNickels, numPennies;
	while (choice == 'y')

	{
		cout << " Enter an amount : ";
		cin >> change;
		if (change > 0)
		{

			numQuarters = change / QUARTERS;
			change -= numQuarters * QUARTERS;
			numDimes = change / DIMES;
			change -= numDimes * DIMES;
			numNickels = change / NICKELS;
			change -= numNickels * NICKELS;
			numPennies = change / PENNIES;
			change -= numPennies * PENNIES;

			if (numQuarters != 0) // this represent the amount of quarter there are
				if (numQuarters > 1)
					cout << " There are Quarters " << numQuarters;
				else
					cout << " There is Quarter " << numQuarters << endl;
			cout << endl;
			if (numDimes != 0) // this represent the amount of dimes there are
				if (numDimes > 1)
					cout << " There are Dimes " << numDimes;
				else
					cout << " There is Dime " << numDimes << endl;
			cout << endl;

			if (numNickels != 0) // this represent the amount of nickels there are
				if (numNickels > 1)
					cout << " There are Nickels " << numNickels;
				else
					cout << " There is Nickel " << numNickels;
			cout << endl;

			if (numPennies != 0) // this represent the amount of pennies there are
				if (numPennies > 1)
					cout << " There are Pennies " << numPennies;
				else
					cout << " There is Penny " << numPennies;
			cout << endl;
		}
		else
			cout << "Invalid amount" << endl;
			cout << "Would you like to try another number? press(y). Anything else will quit : "; 
			cin >> choice;
	}
	system("pause");
	return 0;
}


Oct 4, 2018 at 9:55pm
That is not an infinite loop for me, sorry...

D:\code\cplusplus243597>make main
g++     main.cpp   -o main

D:\code\cplusplus243597>main
 Enter an amount : 42
 There is Quarter 1

 There is Dime 1

 There is Nickel 1
 There are Pennies 2
Would you like to try another number? press(y). Anything else will quit : y
 Enter an amount : 1791
 There are Quarters 71
 There is Dime 1

 There is Nickel 1
 There is Penny 1
Would you like to try another number? press(y). Anything else will quit : n
Press any key to continue . . .



It is, however, possible for it to become an infinite loop.... if std::cin fails.... I figured this was out of the scope of the problem. I will provide an example that can prevent this.
Last edited on Oct 4, 2018 at 9:55pm
Oct 4, 2018 at 9:56pm
It becomes an infinite loop, when you input letters/character in the input. That is when it becomes constant loop.
Oct 4, 2018 at 10:03pm
I honestly think that's outside of the scope of what your professor expects, here you go.

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


int main()

{
	char choice = 'y';
	int QUARTERS = 25;
	int DIMES = 10;
	int NICKELS = 5;
	int PENNIES = 1;
	int change, numQuarters, numDimes, numNickels, numPennies;
	while (choice == 'y')

	{
		cout << " Enter an amount : ";
		if (!(cin >> change))
		{
			change = 0;
            
			// reset std::cin stream
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
		if (change > 0)
		{

			numQuarters = change / QUARTERS;
			change -= numQuarters * QUARTERS;
			numDimes = change / DIMES;
			change -= numDimes * DIMES;
			numNickels = change / NICKELS;
			change -= numNickels * NICKELS;
			numPennies = change / PENNIES;
			change -= numPennies * PENNIES;

			if (numQuarters != 0) // this represent the amount of quarter there are
				if (numQuarters > 1)
					cout << " There are Quarters " << numQuarters;
				else
					cout << " There is Quarter " << numQuarters << endl;
			cout << endl;
			if (numDimes != 0) // this represent the amount of dimes there are
				if (numDimes > 1)
					cout << " There are Dimes " << numDimes;
				else
					cout << " There is Dime " << numDimes << endl;
			cout << endl;

			if (numNickels != 0) // this represent the amount of nickels there are
				if (numNickels > 1)
					cout << " There are Nickels " << numNickels;
				else
					cout << " There is Nickel " << numNickels;
			cout << endl;

			if (numPennies != 0) // this represent the amount of pennies there are
				if (numPennies > 1)
					cout << " There are Pennies " << numPennies;
				else
					cout << " There is Penny " << numPennies;
			cout << endl;
		}
		else
			cout << "Invalid amount" << endl;
			
		cout << "Would you like to try another number? press(y). Anything else will quit : "; 

		if (!(cin >> choice))
		{
			choice = 'n';
            
			// reset std::cin stream
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	}
	system("pause");
	return 0;
}


Basically, you need to guard every time you request input from std::cin.

Last edited on Oct 4, 2018 at 11:11pm
Oct 4, 2018 at 10:37pm
Thank you, but what does the std::cin.clear(); and std::cin.ignore?
Oct 4, 2018 at 11:09pm
In a nutshell, it resets the std::cin stream to make it usable again.

std::cin.clear(); resets the failure state of the stream, allowing it to take in input again.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); Ignores (clears out from the buffer) everything the user entered the last time before they pressed Enter/Return.

Edit: Oh, and checking if (cin >> choice) means "were we correctly able to put the value the user entered into the given variable".
Last edited on Oct 4, 2018 at 11:28pm
Topic archived. No new replies allowed.