Writing a C++ program using the following:

ttt
Last edited on
If you edit your post, highlight your code, then click on the code format button [<>] on the right side of the post - it makes your code a lot easier to read.

Right now you just have the loop running on checking whether a number is even/odd. You need to have the whole process in the loop, so for each number entered, you check if it's even or odd, see if it's the largest or smallest number entered so far, and add it to the running total.

Even if they enter ten numbers here separated by spaces, cin is just going to read up to the first blank space, so essentially you'd just be saving the first number they enter here.
1
2
std::cout << "Please enter 10 numbers"; // get 10 numbers from the user
std::cin >> n;



max and min aren't initialized when you use them in comparisons with n.
thanks
Last edited on
line 15 - n is uninitialized here when you try to use the modulus operator on it.

If the number of iterations in a loop is known, I'd usually use a for loop. Does the assignment need to use a while loop? You'd need a counter that increments every iteration of the while loop.
yes it needs a while loop as explained in the question. If you can show me how i can replace the for loop that would be much appreciated.
You had the framework of the while loop in your first post - basically you just need to pull the other steps inside that loop instead of having them outside.

while less than 10 numbers entered
- prompt user to enter a number
- check if it's even or odd
- see if it's the largest entered
- see if it's the smallest entered
- add it to the running total
- increment counter
// end while loop when all numbers have been entered

output summary
- smallest number entered
- largest number entered
- sum of all numbers entered
lll
Last edited on
Hi @billybob1,
in addition to
@wildblue's advices
this could give you an
idea;

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
//in_num.cpp
//##

#include <iostream>

using namespace std;

bool isEven(int number);

int main(){


        int const NUMBERS=3;
        int number;

        cout<<"\nEnter "<<NUMBERS<<" numbers"<<endl;

        for(int i=0;i<NUMBERS;i++){
                cout<<"Enter number "<<i+1<<": ";
                cin>>number;

                if(isEven(number))
                        cout<<number<<" is even."<<endl;
                else
                        cout<<number<<" is odd."<<endl;
        }//end for



return 0; //indicates success
}//end of main

bool isEven(int number){
        if(number%2==0)return true;

return false;
}//end function isPositive
./in_num 

Enter 3 numbers
Enter number 1: 2
2 is even.
Enter number 2: 3
3 is odd.
Enter number 3: 4
4 is even.
Since you don't know if they're going to enter a positive or a negative number, you could pick a random positive or negative number to initialize the smallest and largest variables. I've included the <climits> header to be able to use the INT_MIN and INT_MAX constants. http://www.cplusplus.com/reference/climits/

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
#include <iostream>
#include <climits>
using std::cout; using std::cin; using std::endl;

int main() 
{
	int number = 0;
	int counter = 1;
	int sum = 0;
	int largest = INT_MIN;
	int smallest = INT_MAX;

	while (counter <= 10)
	{
		cout << "Please enter integer # " << counter << " : ";
		cin >> number;
		cout << number << " is " << (number%2 == 0 ? "even\n" : "odd\n");
		if (number > largest)
			largest = number;
		if (number < smallest)
			smallest = number;
		sum += number;
		counter++;
	}
	
	cout << "The largest number entered is " << largest << endl;
	cout << "The smallest number entered is " << smallest << endl;
	cout << "The sum of all numbers entered is " << sum << endl;
	
	return 0;
}
4
8
-2
7
15
-20
6
3
20
2

Please enter integer # 1 : 4 is even
Please enter integer # 2 : 8 is even
Please enter integer # 3 : -2 is even
Please enter integer # 4 : 7 is odd
Please enter integer # 5 : 15 is odd
Please enter integer # 6 : -20 is even
Please enter integer # 7 : 6 is even
Please enter integer # 8 : 3 is odd
Please enter integer # 9 : 20 is even
Please enter integer # 10 : 2 is even
The largest number entered is 20
The smallest number entered is -20
The sum of all numbers entered is 43
Topic archived. No new replies allowed.