Loop help!

Pages: 12
closed account (ENhkSL3A)
I am not sure what I am doing. I need this thing to keep running the loop but it will only ask for input once when I run it. What do I need to change and/or add? Also, the last statement will not display? I need it to show up when the loop stops. My teacher doesn't want us to set the loop to run a specific number of times. she said set up some sort of termination criteria. The instructions are vague. I'll post them after the code too.

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

int main()
{
	int input;
	int count;
	int num;
	int sentinel = 999;


	cout << "Exercise 6A" << endl;
	cout << "Kaitlin N. Stevers" << endl;
	cout << "September 25, 2016" << endl;
	cout << "\t\t" << endl;
	cout << "\t\t" << endl;
	cout << "Please input value:  \n" << endl;
	cin >> num;
	cin.get();

	while (input != sentinel)
	{
	if (num < 10)
	{ 
		cout << "The wire is: White" << endl;
	}
	else 
	if (num < 30)
	{
		cout << "The wire is: Green" << endl;
	}
	else
	if (num < 40)
	{
		cout << "The wire is: Blue" << endl;
	}
	else
	{
		cout << "The wire is: Red" << endl;
		break;
	}
	cout << "Have a great day & thank you for yousing Katie's program!" << endl;
	return 0;
}


Here are the instructions:
EGR 126
Updated 8/20/2016
Exercise 6A – Wires, if-else

The objective of this Exercise is to develop a program similar to the one in Exercise 5A, with the alteration that a loop is
used to process several flag values in one run of the program.

Please review the problem description in the document for Exercise 5A.

Write a program with a while loop to process several different flag values. For each flag value that is input, determine the
appropriate wire color, printing out the appropriate message before requesting another input value. The decision on wire
color is determined with if-else statements, so use the code developed in Exercise 5A. This program can be viewed as an
enhancement/modification of Exercise 5A.

You may read the values for the input flag from screen or file, as you choose.

Please use the following values, in this order: 55, 25, 5, 35, 40, 10

If input from screen, then part of the output may look something like the following:


Please input a flag value: 55
You input a value of 55
The wire color is RED
Please input a flag value: 25
You input a value of 25
The wire color is GREEN


It is very easy to utilize the input prompt as a pseudo-echo for inputs:


Please input a flag value: 55
The wire color is RED
Please input a flag value: 25
The wire color is GREEN


In this case, the input request as in the example above (Please input a flag value: ) serves as an
identifier for the input value. This is not good programming practice, and this technique should not be used for
Assignments in this class.

How will you terminate the loop? Do not set the loop to run for exactly 6 values. You will need to set up some
sort of termination criteria. Some suggestions may be a flag value that is negative or a flag value of 999. Be
sure to print something to the user to identify how the loop is to be terminated.

** The other instructions just let us know the if statements which are in my code.





-----Thanks to everyone who helped me and tried to help me :) ----
Last edited on
Your program does not compile, and the compiler also warns about couple things:
 In function 'int main()':
7:6: warning: unused variable 'count' [-Wunused-variable]
21:2: warning: 'input' is used uninitialized in this function [-Wuninitialized]
44:1: error: expected '}' at end of input

(Each message starts with a line-number.)

Focus on those items first.


You do have a break at line 40. Why?
closed account (ENhkSL3A)
I put the break on line 40 because I thought it was required to end the loop. I'm really unfamiliar with loops. This is my very first time doing this.
Also, i know you're supposed to have count according to some other notes from my professor but I'm not sure why. I assume it has something to do with the ending of the loop. I'm not sure what to do about the two warnings and the error. Advice on correcting my work or how loops work?
closed account (ENhkSL3A)
What should I do as my for statement? That I'm finding confusing for my code specifically.
closed account (ENhkSL3A)
How do I get the code to keep repeating?
closed account (ENhkSL3A)
New Code right now:

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

int main()
{

	int num;
	int sentinel = 999;


	cout << "Exercise 6A" << endl;
	cout << "Kaitlin N. Stevers" << endl;
	cout << "September 25, 2016" << endl;
	cout << "\t\t" << endl;
	cout << "\t\t" << endl;
	cout << "Please input value:  \n" << endl;
	cin >> num;
	cin.get();

	while (num != sentinel)
	{
	if (num < 10)
	{ 
		cout << "The wire is: White" << endl;
	}
	else 
	if (num < 30)
	{
		cout << "The wire is: Green" << endl;
	}
	else
	if (num < 40)
	{
		cout << "The wire is: Blue" << endl;
	}
	else
	{
		cout << "The wire is: Red" << endl;
	}
	cout << "Have a great day & thank you for yousing Katie's program!" << endl;
	return 0; }
}
closed account (ENhkSL3A)
Anyone???????????
What is the problem with your new code?
Move the return 0; outside the while loop.

1
2
	cout << "\t\t" << endl;
	cout << "\t\t" << endl;

What is the purpose of the two tabs then a new line? Tabs are meant to make the output stand out or more readable, but you don't have any text on the same line as the tabs.
Last edited on
It seems like you'd want to ask again in the while loop for the user to enter a value.

If they enter anything other than the sentinel value at the first prompt (over lines 16-17), it looks like you have an infinite loop.
closed account (ENhkSL3A)
Okay, thanks guys. Also, that was for spacing. I didn't want the next line directly under the previous. My professor showed us that to create space. Is there another way?
I didn't want the next line directly under the previous. My professor showed us that to create space.

What do you mean?
closed account (ENhkSL3A)
It would display like this:

Exercise 6A
Kaitlin N. Stevers
September 28, 2016
Please input value:

without the tabs. With them it is like this:
Exercise 6A
Kaitlin N. Stevers
September 28, 2016


Please input value:
How is it different? Just two additional newlines if you ask me.
closed account (ENhkSL3A)
Move the return 0; outside the while loop.


That did not help... It made things a LOT worse.... ummm...
@Katie Forester
I sent you a PM.
closed account (ENhkSL3A)
So I got my loop to work. I'll post my code in case someone comes along needing help! I still don't know how to make it terminate properly like my professor asks.
The instructions say: "How will you terminate the loop? Do not set the loop to run for exactly 6 values. You will need to set up some
sort of termination criteria. Some suggestions may be a flag value that is negative or a flag value of 999. Be
sure to print something to the user to identify how the loop is to be terminated."

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


void first()
{
    int flag;
    int sentinel = 999;
    cout << "Please input value:  \n" << endl;
    cin.get();
    cin >> flag;
    
    while (flag != sentinel)
    
	if (flag < 10)
	{ 
		cout << "The wire is: White" << endl;
		first();
	}
	else 
	if (flag < 30)
	{
		cout << "The wire is: Green" << endl;
		first();
	}
	else
	if (flag < 40)
	{
		cout << "The wire is: Blue" << endl;
		first();
	}
	else
	{
		cout << "The wire is: Red" << endl;
		first();
	}
}	
int main()
{
	cout << "Kaitlin N. Stevers" << endl;
	cout << "September 25, 2016" << endl;
	cout << "\t\t" << endl;
	cout << "\t\t" << endl;
	cout << "Before beginning, you need to know in order to end the loop you need to input the value 999." << endl;
	first();
}

Recursive function is not a good idea. Stick to a do-while or a while loop.
closed account (ENhkSL3A)
I do not know how to do a do-while and I do not know how to get a while loop to loop over and over without doing what I did. I'm a beginner. :/


----- Thanks for all your help guys. I finally got it to work like I wanted! :) -----
Last edited on
Pages: 12