My program is suppose to read positive and odd numbers and add their sums but any negative numbers they are not suppose to count or be added to the sum. My error message is coming up when I type a negative number in but I think it's still being counted and added to the sum. Can someone take a look and help me out?

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main ()
{
	
	int number1;
	int sumeven=0;
	int sumodd=0;
	int evencounter=0;
	int oddcounter=0;
	int counter;
	

	cout<<"Please enter 10 integers: \n";
	
	for( counter=1; counter<=10; counter++)
	{
		cin>> number1;
		if (number1 % 2 == 0)
		{
			evencounter++;
			sumeven=sumeven+number1;
		}
		else
		{		
			oddcounter++;
			sumodd=sumodd+number1;
		} 
		if (number1 < 0)
		cout<<"One number is invalid. Enter positive number: \n"<<number1;
		
	} 
cout<<"There are " <<evencounter<<" even numbers \n ";
cout<< "and the sum of all even numbers is: \n"<<sumeven;

cout<<"\nThere are " <<oddcounter<<" odd numbers \n";
cout<< "and the sum of all odd numbers is: \n"<<sumodd;


	return 0;

}

Dani AI

Generated

Quick diagnosis: the original program checks parity before it checks for negativity, so a negative entry still runs the even/odd branch and gets counted and added before the "invalid" message is printed. Both and noticed workable fixes (repeat the input or adjust the counter), but the root cause is simply doing the wrong test order and not validating input before updating counts or sums.

A safer pattern is to validate input first and only update counters/sums for a confirmed positive integer. This avoids fiddling with the loop index and is more robust against non-integer input. The helper below reads a positive integer and handles bad input; the main loop then only deals with parity and accumulation.

#include <iostream>
#include <limits>

int readPositiveInt() {
    int v;
    while (true) {
        if (!(std::cin >> v)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Not an integer. Try again: ";
            continue;
        }
        if (v < 0) {
            std::cout << "Please enter a positive number: ";
            continue;
        }
        return v;
    }
}

/* Use in main:
for (int i = 0; i < 10; ++i) {
    int n = readPositiveInt();
    if ((n % 2) == 0) { evencounter++; sumeven += n; }
    else { oddcounter++; sumodd += n; }
}
*/

Notes and cautions: decrementing the for-counter (as suggested by ) works but is fragile if extraction fails; prompting once (as in ) works but misses repeated bad input unless wrapped in a loop. Also consider using a larger integer type for sums if inputs could be large, and always clear/ignore on extraction failure to avoid infinite loops.

Recommended Answers

All 3 Replies

Whenever you enter a negative number your counter increases by 1 which i think you do not want.....rest is fine.....Modify the code like this....

if (number1 < 0)
		{
			cout<<"One number is invalid. Enter positive number: \n"<<number1<<"\n";
			counter=counter-1;
		}

hi i think you should do it like that

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main ()
{
	
	int number1;
	int sumeven=0;
	int sumodd=0;
	int evencounter=0;
	int oddcounter=0;
	int counter;
	

	cout<<"Please enter 10 integers: \n";
	
	for( counter=1; counter<=10; counter++)
	{
		cin>> number1;
		if (number1 < 0){
		cout<<"One number is invalid. Enter positive number: \n"<<number1<<endl;
cout<<"Please enter the correct number: \n";
		cin >>number1 ;
		}
		if (number1 % 2 == 0)
		{
			evencounter++;
			sumeven=sumeven+number1;
		}
		else
		{		
			oddcounter++;
			sumodd=sumodd+number1;
		} 
	
		
	} 
cout<<"There are " <<evencounter<<" even numbers \n ";
cout<< "and the sum of all even numbers is: \n"<<sumeven;

cout<<"\nThere are " <<oddcounter<<" odd numbers \n";
cout<< "and the sum of all odd numbers is: \n"<<sumodd<<endl;


	return 0;

}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

Thanks for all help :) . I can't believe all it was missing was one line.

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.