Counter/loop question

My program is not stopping after the number of maximum loops I have established.

Any suggestions?

I just need for it to stop looping after a certain number.

//Setup standard environment
#include <iostream>
using namespace std;

int main()
{
//Variable declarations

double wave_length; //user input
int i; //loop counter
int counter=0; //initialize counter
int max_iterations = 2; //maximum number of times for error loop
const int max_char = 30; //maximum numbers of characters

// Output program name
cout << endl; // Setups two blank lines before program name
cout << endl;

//Output program title to display
for (i=0; i<max_char; i++) //outputs blanks
cout << " " ;

cout<< "Assignment 2" <<endl;
cout <<endl;
cout <<endl;

do
{
//Output data to display
cout<< "Please enter the wavelength in microns to the thousandth decimal place (Valid entries 0.400-0.700): ";

//Input data
cin>> wave_length;

//Begin if-else statements
if (wave_length >= .400 && wave_length <=.700)
//Begin if-else statements for valid entry
{
if (wave_length <= 0.424)
cout<< "The associated color is Violet." <<endl;
else if (wave_length > 0.424 && wave_length <= 0.491)
cout<< "The associated color is Blue." <<endl;
else if (wave_length >0.491 && wave_length <=0.575)
cout<< "The associated color is Green." <<endl;
else if (wave_length >0.575 && wave_length <=0.585)
cout<< "The associated color is Yellow." <<endl;
else if (wave_length >0.585 && wave_length <=0.647)
cout<< "The associated color is Orange." <<endl;
else if (wave_length>0.585 && wave_length <=0.700)
cout <<"The associated color is Red." <<endl;
}
else
//Output error message
cout << "Wavelength outside visual range." <<endl<<endl<<endl;

counter= counter +1; //Add one to loop
}

//While statement
while (wave_length < .400 || wave_length >.700 && counter <= max_iterations);

return 0;
}
while ((wave_length < .400) && (wave_length >.700) && (counter <= max_iterations));
I added that, now it won't loop?

Help please? What can I do to make it loop again? I tried changing the first && to || then it just starts looping constantly again.

Sorry you were right to begin with. it should be logical OR.

i just compiled and ran the code and it seems to work fine, what is the problem? or how do you want it to work exactly ? Because it seems to me it is working how you would want it to.

if I enter invalid values then it runs for 3 tries, if I enter valid input it tells me the associated color.
So I'm unsure how else you want this to work.
Last edited on
I revised your code with this...

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
//Setup standard environment
#include <iostream>
using namespace std;

int main()
{
//Variable declarations

double wave_length; //user input
int i; //loop counter
int counter=0; //initialize counter
int max_iterations = 2; //maximum number of times for error loop
const int max_char = 30; //maximum numbers of characters

// Output program name
cout << endl; // Setups two blank lines before program name
cout << endl;

//Output program title to display
for (i=0; i<max_char; i++) //outputs blanks
cout << " " ;

cout<< "Assignment 2" <<endl;
cout <<endl;
cout <<endl;

while(1)
{
	//Output data to display
	cout<< "Please enter the wavelength in microns to the thousandth decimal place (Valid entries 0.400-0.700): ";

	//Input data
	cin>> wave_length;

	//Begin if-else statements
	if (wave_length >= .400 && wave_length <=.700){
		//Begin if-else statements for valid entry
		if (wave_length <= 0.424) 
			cout<< "The associated color is Violet." <<endl;
		else if (wave_length > 0.424 && wave_length <= 0.491)
			cout<< "The associated color is Blue." <<endl;
		else if (wave_length >0.491 && wave_length <=0.575)
			cout<< "The associated color is Green." <<endl;
		else if (wave_length >0.575 && wave_length <=0.585)
			cout<< "The associated color is Yellow." <<endl;
		else if (wave_length >0.585 && wave_length <=0.647)
			cout<< "The associated color is Orange." <<endl;
		else if (wave_length>0.585 && wave_length <=0.700)
			cout <<"The associated color is Red." <<endl;
	}
	else{
		//Output error message
		cout << "Wavelength outside visual range." <<endl<<endl<<endl; 
		counter= counter +1; //Add one to loop
	}
	if(counter >= 2 && (wave_length < 0.400 && wave_length >0.700))
		break;
}
return 0;
}
It's not really necessary he/she was right to begin with, however I think maybe needs to do a recompile or something because the first post is working exactly as intended. it only allows 3 inputs, and outputs correct values.
You're right now haha. I got it to work. It's perfect now. THANK YOU SO MUCH.

I just had to put while (((wave_length < .400) && (wave_length >.700)) && (counter <= max_iterations));

I guess it's my compiler?
Last edited on
Gcampton...

could it be my compiler? I've very new to C++, but why would yours work correctly and mine wouldn't? It seemed like silly things I had to fix to get it to work. Is it the environment I am working in?

I'm using Microsoft Visual C++ 2008
hmmm, seriously?

I wouldn't think you would want it to terminate if the user inputs incorrect value.
the way you had it in your first post was correct depending on how you wanted it:

with while (((wave_length < .400) && (wave_length >.700)) && (counter <= max_iterations));
it exits on incorrect values, and exits on correct value but with correct output. So the counter is redundant.

with while (wave_length < .400 || wave_length >.700 && counter <= max_iterations);
it will loop 3 times on incorrect values, and will display output and exit on correct value. This is the way you had it.

with while (((wave_length < .400) || (wave_length >.700)) || (counter <= max_iterations));
it will loop 3 times regardless of incorrect or correct values. correct values still output correct result.
Last edited on
I'm sorry. I was wrong in the previous quote.

It was actually (((wave_length < .400) || (wave_length >.700)) && (counter <= max_iterations));

I did want to allow the user to have 3 chances. Like baseball, 3 strikes and out. :) Thanks again.

Topic archived. No new replies allowed.