Here's the problem: Ask the user for a floating point number until the user enters a negative number or zero. After each number the user enters print the highest and lowest number so far, and the average so far. Do not print the highest, lowest, and average when the user enters a negative number or zero, but just say a friendly "good-bye". Here's what I have so far:

#include "stub.h"

#include <iostream>             

using namespace std; 

int main()
{

    int counter=0;
    float highest = 0;
    float input = 0;
    float lowest; 
    float sum=0; 
    int limit;
    float average;       

    for(input>0;counter<=limit;)
    {
        cin >> input;

        sum = sum + input;
        counter = counter + 1;
        average = sum/counter;
        
         if(input > highest)
              highest = input;

         if (input < lowest)   
         lowest = input;
         
         cout << average << "  " << highest << "   " << lowest << endl; 
  
   }
  
    if ( input <= 0)
         cout << "Good-Bye" << endl;

    
 cin.ignore(INT_MAX);

    

    return 0;
}

I don't know what I did wrong.

Recommended Answers

All 6 Replies

that is stub.h ?

line 18: limit is an uninitialized variable, so it could be any old random number including 0. You need to initialize it with something if you expect that line to work properly.

line 21: you need to test if input is <= 0 and if it is break out of that loop

line 36: delete it because its outside the loop. No need to test the value of input here.

A for loop typically has 3 clauses, you've got 2.

You're not going to execute code inside a loop if that particular hunk of code is itself outside the loop.

A for loop typically has 3 clauses, you've got 2.
.

In this program it is ok because the loop counter is incremented inside the loop.

but now that I look at that line some more it is all screwed up. for(input>0;counter<=limit;) sould be for(input=1;counter<=limit;++counter) then you can delete line 23

Guys, guys, we're supposed to be 'experts' here! :D

#1: You are using a for loop but your exit criteria of the loop depends on counter and limit. Your instructions say "when the user enters a negative number or zero" and counter has no bearing on the exit.

#2: After you input your value, whether positive or negative, you process the value.

Recommendation:
#1: Try a different type of loop. There is a better choice, although for will work, it's just a little clumsy.
#2: Is there a better place to input the value within the loop so it doesn't get processed?

commented: Good clarification. I'm trending terse lately. +13

Here's the problem: Ask the user for a floating point number until the user enters a negative number or zero. After each number the user enters print the highest and lowest number so far, and the average so far. Do not print the highest, lowest, and average when the user enters a negative number or zero, but just say a friendly "good-bye". Here's what I have so far:

#include "stub.h"

#include <iostream>             

using namespace std; 

int main()
{

    int counter=0;
    float highest = 0;
    float input = 0;
    float lowest; 
    float sum=0; 
    int limit;
    float average;       

    for(input>0;counter<=limit;)
    {
        cin >> input;

        sum = sum + input;
        counter = counter + 1;
        average = sum/counter;
        
         if(input > highest)
              highest = input;

         if (input < lowest)   
         lowest = input;
         
         cout << average << "  " << highest << "   " << lowest << endl; 
  
   }
  
    if ( input <= 0)
         cout << "Good-Bye" << endl;

    
 cin.ignore(INT_MAX);

    

    return 0;
}

I don't know what I did wrong.

`

hai i am vignesh. I think FOR loop has error. FOR loop allow more than one condition . that will splitted by comma(,). The correct format is
for(;input>0,counter<=limit;)
the for loop format must contain only two ;.not more than that. I think what i have wrote is correct.

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.