10-12-2004, 06:20 PM
|
#14 (permalink)
|
| Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
| Quote:
k, here's what i got. it's not working. it just halts after i press ctrl-z. i'm sure there are way better ways of figuring this out, don't mind my bloated code. not sure if i want to have the computations in the loop, so i commented out per and avg. and yes i did find out how to do the ctrl-z. it's the (!cin.eof()) part. not really sure if it's working though. so yeah, anyway when i run this and type a few numbers in...it just halts after i press ctrl-z anyone know what i'm doing wrong? Code: #include <iostream>
using namespace std;
int main()
{
int input, sum = 0, rem, oddsum = 0, negsum = 0, possum = 0, total = 0;
float per, avg;
cout << "Enter an integer or ctrl-z to exit: ";
cin >> input;
while (!cin.eof()) {
cin >> input; //user input
while (input != 0) {
cin >> input; //user input
total++; //increment total by 1
if (input < 0) { //find out negative integers
negsum++; //increment negsum by 1
//per = negsum / total; //compute percent of negative numbers
}//end of if
else { //find out positive integers
rem = input % 2;
if (rem = 0) { //find out even integers
sum += input;
}//end of if
else { //find out odd integers
possum++;
oddsum += input;
//avg = oddsum / possum; //compute average of odd positive integers
}//end of else
}//end of else
}//end of while
}//end of while
per = negsum / total;
avg = oddsum / possum;
cout << "Here is the stats of the " << total << " numbers you entered:" << endl;
cout << per << " of the numbers were negative." << endl;
cout << "The sum of the positive even integers is " << sum << endl;
cout << "The average of the positive odd integers is " << avg << endl;
} | another update |
| |