Programming Language : C++ Problem - The program is displaying wrong output when
ID: 3784303 • Letter: P
Question
Programming Language : C++
Problem - The program is displaying wrong output when entering a number
If number is 0-9 it displays " 0 is less than 10" ------ [ this one works correctly]
if number is 11 it displays "4213412 is greater than 10"
if number is 10 it displays " 3794234123 is equal to 10"
// compare iInput to integer 10
if (iInput < 10) // if input is less than 10
wsprintf(wcOutput, TEXT("%i is less than 10"), iInput);
if (iInput > 10) // if input is greater than 10
wsprintf(wcOutput, TEXT("%i is greater than 10", iInput));
else // if input is equal to 10
wsprintf(wcOutput, TEXT("%i is equal to 10", iInput));
Explanation / Answer
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int number = 0;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
do
{
cout << "Enter a number between 0 and 0: ";
cin >> number;
count++;
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
if (number < 0 || number > 0)
{
cout << "Please enter a number between 0 and 0. ";
cin >> number;
}
} while (key != 'n');
sum = sum + count;
average = float(sum)/number;
if (number > largest)
largest = number;
if (!count || smallest > number)
smallest = number;
cout << "You've entered " << count << " numbers. " << endl;
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.