Okay, so here's my set of problems: I need a simple error check, so that if the user enters the wrong type of character, he/she will be notified and allowed to re-enter a new value. Secondly, I need some sort of loop that allows the user, after having running the program at least once successfully, to choose whether or not to run it again. Finally, since this program is supposed to read in only odd integers, tally the number of odd integers, calculate the average, and give the lowest and highest odd integers entered, I need some help with that, as although I don't enter 0, the lowest odd integer is always printed as if it were 0. Here's my code, so far:

#include <iostream>
#include <iomanip>
#define Exit -1
using namespace std;

int main(int argc, char** argv)

{
	int cur_num;
	int min_num = 0; 
	int max_num = 0;
	int odd_num_count = 0;
	int sum_odd_num = 0;
	double avg_num = 0;
	 

	

	while (true) {

		cout << "Enter a Positive Integer or -1 to Exit: \n"; // display message
		cin >> cur_num; //enter a positive integer value
		if (cur_num == Exit) break; //exit statment to initiate calculation
		if ((cur_num%2 != 0)  && (cur_num > 0)) //varifies that value entered is odd
		{
			if ( cur_num <= min_num) min_num = cur_num; //varifies if value entered is smallest number
			if (cur_num >= max_num)	max_num = cur_num; //varifies if value entered is largest number
			odd_num_count++; //increments the odd number counter
			sum_odd_num = sum_odd_num + cur_num; //algorith for odd number counter
			avg_num = sum_odd_num/odd_num_count; //algorith for average value calculation
			cout << fixed << showpoint << setprecision (2);
			
		}
	}

	

	cout << "Results:\n"; //display message
	cout << "Number of Odd Integers Entered: " << odd_num_count << "\n"; //display message
	cout << "Average Value: " << avg_num << "\n"; //display message
	cout << "Largest Odd Integer Entered: " << max_num << "\n"; //display message
	cout << "Smallest Odd Integer Entered: " << min_num << "\n"; //display message


	fflush (stdin);
	cin.get();
	return 0;
}

Thanks for your time and assistance.

Desh2350

Recommended Answers

All 3 Replies

Okay, so here's my set of problems: I need a simple error check, so that if the user enters the wrong type of character, he/she will be notified and allowed to re-enter a new value

template <class T>
void input(T* variable) {
    while(1) {
    if (cin >> *variable)
return true;

if(cin.fail()) {
cout << "Incorrect data input: ";
cin.clear();
cin.ignore( 1000, '\n' );
}
}
}

If you don't know templates yet, to invoke it use input <type> (&variablename); where type is the type of the variable for example input <int> (&myvar);

Secondly, I need some sort of loop that allows the user, after having running the program at least once successfully, to choose whether or not to run it again.

bool endprogram = false;
while (!endprogram) {
do program stuff
.
.
cout << "You want to run the program again? y/n ";
char again;
cin >> again;
if (again == 'y') ;
if (again == 'n') endprogram = true;

And I am not really sure what you are asking for the third question.
Also please use code tags for code.

I need a simple error check, so that if the user enters the wrong type of character, he/she will be notified and allowed to re-enter a new value.

Rad the input as a string. Then test each character to make sure it's a valid number.

Secondly, I need some sort of loop that allows the user, after having running the program at least once successfully, to choose whether or not to run it again.

Use a do-while loop. Start it just before the first statement you want to start repeating. At the end, ask your yes/no question. Use a condition that stays in the loop if YES is indicated.

That's a start...

Your third problem is caused by your conditional check to see if the current number is less than the minimum number. But you initialise your minimum number to 0. So it will never be smaller unless you put in negative numbers ;)

Also, templates would be good in this circumstance, but there's a slight error in the given code. The return type should be bool not void. :)

EDIT: Also, something else I noticed. You're doing Integer division for your average number. I don't know if this is intentional or not, but it means you won't be given a double as a result. If you do want a double as a result, you want to cast both numerator and denominator to double in the equation.

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.