View Single Post
Old 10-14-2004, 08:30 AM   #21 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
udate...
Quote:
argh. it's still not working. at least the output is being displayed but, my numbers still arent coming out right. let me do a recap on the input output

input - negative or positive integers, ctrl-z to exit input
output -
1. a count of the total numbers entered
-this part is working
2. a percentage of the numbers that were negative
-this is a problem. when entering 3 numbers, 2 of which are negative, the output displays 33.33%. i can't understand why.
3. a sum of the positive even integers
-can't seem to get this one working right. when the sum should be 12, it'll be 16. i've tried a table of values and i am completely at a loss.
4. an average of the positive odd integers
-seems like the compiler is pulling numbers out of it's ass with this one.

anyway, here is my code so far;

Code:
 #include <iostream> 
 using namespace std; 
  
 int main() 
 { 
   int input, rem, evensum = 0, oddsum = 0, negsum = 0, total = 0, possum = 0; 
   double per, avg; 
          
   cout << "Enter an integer or ctrl-z to exit: "; 
   cin >> input; 
          
   while (!cin.fail()) { 
     cin >> input;  //user input 
     //while (input !=0)   ~if i use this instead of if, it just freezes 
     if (input != 0) { 
       total++;  //increment total by 1 
       if (input <= -1) {  //find out negative integers 
         negsum++; //increment negsum by 1 
       }//end of if 
       else { //find out positive integers 
         rem = input % 2; 
         if (rem != 0) {  //find out even integers 
         //if (input % 2 != 0)   ~this doesnt seem to work either 
           possum++; 
           oddsum += input; 
         }//end of if 
         else {  //find out odd integers 
           evensum += input; 
         }//end of else 
       }//end of else 
     }//end of if 
   }//end of while  
          
 per = negsum * 100.0/ total; 
 avg = oddsum * 1.0/ 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 " << evensum << endl; 
 cout << "The average of the positive odd integers is " << avg << endl; 
 }
where is my logic wrong? why am i getting these ghost numbers that appear from nowhere? what really pisses me off is that my nested while isnt working. i'm forced to use an if. too many fricken if's. and the "if (input % 2 = 0) isn't working either. is it just mingw? ofcourse it doesnt help that the textbook that we use is for a c++ app called lookout. and the actual app that we use is mingw. so, sometimes the examples in the book don't exactly work and i'm forced to google things. for example, cin.eof() (from my book) didnt work, but cin.fail() did. i really want to learn this stuff....am feeling a bit discouraged.
Androto is offline   Reply With Quote