Hello,

I need some help on processing only even numbers on a counter-controlled do-while-loop, an event-controlled do-while-loop, and event-controlled do-while-loop to process only even numbers that breaks within the loop.
I'm new at C++ so help would be appreciated.

Thanks.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>
#include <limits.h>				
using namespace std; 

int main( )
{
	//variable declarations
	int number;					//var to store an int value
	int sum, min, max, avg;		//vars to store simple statistical info
	int counter;				//a counter for controling the loop
	int size;					//another counter
	/*********************************************************************************
	Part D. Write a counter-controled do-while-loop to process only even numbers
	*********************************************************************************/
	//Write your code here:
	cout<<"************** PLay Counter Controled do-while-Loop************\n"; 
	
	//initialization
	sum=max=avg =0;
	min=INT_MAX;				//min has the maximum integer valued assigned//changed by sirisha
	cout<<" Enter How many integers do you want to play with? => "; 
	cin>>counter;				//get the counter
	size = counter;				//memory the counter
	//The control condition is at the end. This means that we enter the loop first regardless the condition
	do {
		cout<<"Enter an interger ==> ";				
		cin>>number;			//get a number
		sum += number;						
		if (number != 2%)
		cout<<"Enter an interger ==> ";				
		cin>>number;			//get a number
		else 
		{
			if (number > max)
			max = number; 
			if (number < min)
			min = number;
		}
	}while (counter-- > 1) ; 

	cout<<" You entered "<< counter << " many integers. " <<endl
		<<" The sum is " << sum <<endl
		<<" The max is " << max <<endl
		<<" The min is " << min <<endl
		<<" The Avg is " << sum / size<<endl; 

	/*********************************************************************************
	Part E. Write an event-controled do-while-loop to process only even numbers
	*********************************************************************************/
	//Write your code here:
	cout<<"************** PLay Sentinel/Event controled do-while Loop************\n"; 
	cout<<" Compute simple stats of integers. Stop -99 is entered."<<endl;	
	//intitialization
	counter=sum=max=avg =0;
	min=INT_MAX;				
	
	//The control condition is at the end. This means that we enter the loop first regardless the condition
	do {
		cout<<"Enter an interger ==> ";				
		cin>>number;			//get a number
		
		if (number != 2%)		//check for event but cannot break in for practice
		{
			counter++;			//count numbers
			sum += number;					
			if (number > max)
				max = number; 
			if (number < min)
				min = number;
		}
	}while ( number != 2%);	//use event -99 to control the loop

	cout<<" You entered "<< counter << " many integers. " <<endl
		<<" The sum is " << sum <<endl 
		<<" The max is " << max <<endl
		<<" The min is " << min <<endl
		<<" The Avg is " << sum/counter <<endl; 

	/*********************************************************************************
	Part F. Write an event-controled do-while-loop to process only even numbers
	that breaks within the loop
	*********************************************************************************/
	//Write your code here:

	//well done and exit
	return 0; 
}

Here are the errors I get.

1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>Lab11.cpp
1>g:\projects\lab11\lab11.cpp(154) : error C2059: syntax error : ')'
1>g:\projects\lab11\lab11.cpp(157) : error C2181: illegal else without matching if
1>g:\projects\lab11\lab11.cpp(187) : error C2059: syntax error : ')'
1>g:\projects\lab11\lab11.cpp(188) : error C2143: syntax error : missing ';' before '{'
1>g:\projects\lab11\lab11.cpp(196) : error C2059: syntax error : ')'

Recommended Answers

All 13 Replies

Let me see if I understand you here. You want the user to input a bunch of numbers, but only take the ones that are even in account right?

Let me see if I understand you here. You want the user to input a bunch of numbers, but only take the ones that are even in account right?

Yes, the user must enter only even numbers and if odd numbers are entered tell the user only even numbers are accepted and continue entering numbers.

Alright, that's easy to do. This is how I would rewrite your first loop if I understand you correctly. See if it is what you are looking for your code to produce and I'm sure you can adjust the others accordingly.

do {
		 cout<<"Enter an interger ==> ";				
		 cin>>number;			//get a number
		 if (number%2 == 0)
		    { sum += number;	
		  	  if (number > max) max = number; 
			  if (number < min) min = number;
			  counter--;
		    }
		 else { cout << "You must enter an even number.\n"; }
	   } while (counter > 1) ;

Umm... I get this errors, any idea?

1>g:\projects\lab11\lab11.cpp(153) : error C2065: 'number2' : undeclared identifier
1>g:\projects\lab11\lab11.cpp(153) : error C2059: syntax error : '=='
1>g:\projects\lab11\lab11.cpp(154) : error C2143: syntax error : missing ';' before '{'
1>g:\projects\lab11\lab11.cpp(161) : error C2181: illegal else without matching if

Also do u know what does part F. mean when it says "that breaks within the loop?"

I believe the errors you are still receiving are from the rest of the code you have. Try commenting out Part E through the end of F and see if you still get the errors.

I believe that "breaks within the loop" means something like this.

int x = 0;
while (1) // always true, will never end
{ if (x == 5) break; } // this line will cause it to break out of the loop when x == 5

Now, how you will implement it I'm not sure, but that's a very basic way to show how it works.

Those errors are from the code u gave me, and trying to fix them only gives me more errors.
I don't know how to fix the "illegal else without matching if" if the "if" is right there.

I got it to run fine for me. Here's the exact code I complied. See if it works for you.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>
#include <limits.h>				
using namespace std; 

int main( )
{
	//variable declarations
	int number;					//var to store an int value
	int sum, min, max, avg;		//vars to store simple statistical info
	int counter;				//a counter for controling the loop
	int size;					//another counter

	cout<<"************** Play Counter Controled do-while-Loop************\n"; 

	sum=max=avg =0;
	min=INT_MAX;
	cout<<" Enter How many integers do you want to play with? => "; 
	cin >> counter;
	size = counter;	

	do {
		 cout<<"Enter an interger ==> ";				
		 cin>>number;			//get a number
		 if (number%2 == 0)
		    { sum += number;	
		  	  if (number > max) max = number; 
			  if (number < min) min = number;
			  counter--;
		    }
		 else { cout << "You must enter an even number.\n"; }
	   } while (counter > 0) ;

	cout<<" You entered "<< size << " many integers. " <<endl
		<<" The sum is " << sum <<endl
		<<" The max is " << max <<endl
		<<" The min is " << min <<endl
		<<" The Avg is " << sum / size <<endl; 

    system("pause");
	return 0; 
}

Weird, it is the same code and I still get those errors, I even deleted everything else and left exactly what you have and still the same errors.
Can it be Visual C++?

Could be. I got mine to run in Bloodshed Dev-C++. Could try downloading that and see if it gives you the errors that visual is, cause I don't know what else to suggest.

I only get two errors using Dev-C++.
They are the same errors, only two errors are gone.

59 C:\Dev-Cpp\main.cpp `number2' undeclared (first use this function) 
59 C:\Dev-Cpp\main.cpp expected primary-expression before '==' token

Is a % sign between number and 2 in the line:

if (number % 2 == 0)

That's the only reason I can see why they would produce those two errors.

Yea it was that. Thanks a lot.

Heh, no problem. Good luck with the rest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.