-5

When I'm trying to run the program and execute it the for loop doesn't repeat of an adequate form, when I'm trying to execute it appears me this:

Enter a number:
5.6
Enter a number:
Enter a number:
Enter a number:
Enter a number:
Enter a number:
Enter a number:
Enter a number:
Enter a number:
Enter a number:

I tried to search some information about this problem, but I don't found anything. I know that is a foolish question but I don't know where else to turn. A help is appreciated. I left you my code:

#include <iostream>
using namespace std;
int main (void){
    int num, sum;
    float average;
    for(int i=0; i<10; i++){
        cout << " Enter a number: " <<endl;
        cin >>i;
        sum += num;
    }   
        average = num / 10;
        cout << " The total average is:\n " << media <<endl; 
}

2 Answers2

0

cin fails its formatted input operation. It was expecting an integer, but you entered into it a floating point number. Hence, it failed. change the input to a float or double.

A couple of more problems with your code:

  • You can write int main(), rather int main(void). The void isn't quite useful here
  • Always initialize your variables before use
  • Sometimes, check for cin failures and report errors if need be

Corrected code...

#include <iostream>
using namespace std;
int main (){
    int num = 0, sum = 0;
    float average = 0;
    for(int i=0; i<10; i++){
        cout << " Enter a number: " <<endl;
        if(!(cin >> num)){   //you can also use a while loop to force requirement of proper input
             cin.clear();
             cin.ignore(100000, '\n');
             // you can print an error message here
        }
        else
             sum += num;
    }   
        average = static_cast<float>(sum) / 10;
        cout << " The total average is:\n " << average <<endl; 
}

See Why would we call cin.clear() and cin.ignore() after reading input?

Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • @WhiZTiM !cin >> num, what does this do? doesn't ! have a higher precedence than >>? – Biruk Abebe Apr 10 '16 at 15:18
  • Cast `sum` to `float` before using division: `average = (float)sum / 10;` – Đăng Khoa Huỳnh Apr 10 '16 at 15:18
  • @juanchopanza, true... but that syntax was inherited in compatibility with `C` which meant a function with no argument because in C, `int main()` really means a function with a variable number of argument.... That doesn't hold for C++. See http://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c – WhiZTiM Apr 10 '16 at 15:20
  • So what? You were suggesting something is wrong with that. In a post with so many genuine bugs, it seems confusing to talk about something that is fine as if it wasn't. – juanchopanza Apr 10 '16 at 15:21
  • @ĐăngKhoaHuỳnh, Thanks ^^ Corrected... |{|}| Thanks bkVnet, clarified – WhiZTiM Apr 10 '16 at 15:23
  • @juanchopanza, ... Well, you are right, but don't you think its good to point people to "more modern" ways? ... Nonetheless, I have corrected my use of strong "Don't use" – WhiZTiM Apr 10 '16 at 15:28
0

You have three things that ain't correct:

  • First of all, you have to initialize sum before using, like so int sum = 0;.
  • Second of all, the entered number has to be saved in a variable. You can't use i as that variable 'cause it's you loop iterator, therefore you can use num.
  • Last of all, media has no meaning in your program. and i know you meant to use average.

After correcting all of these, your program should look like this one:

#include <iostream>
using namespace std;
int main (void){
    int num, sum=0;
    float average;
    for(int i=0; i<10; i++){
        cout << " Enter a number: ";
        cin >>num;
        sum += num;
    }   
    average = num / 10;
    cout << " The total average is:\n " << average <<endl; 
    system("pause");
}
Anwarvic
  • 12,156
  • 4
  • 49
  • 69