-5

Can someone help me solve this question. I've been stuck on it for quite a while now. The question is: Find the sum of 10 numbers, but will stop asking for a number if ZERO (0) is entered.

#include <iostream>
using namespace std;
int main()
{
float num, sum =0.0;

do {

    cout << "Enter a number" <<endl;
    cin >> num;
    sum += num;


}
while (num !=0.0);
cout << "Total is:" << sum;

return 0;
}

Here's what I can do so far. Please tell me what to change so I can only insert 10 numbers

ReXy Ru
  • 1
  • 2
  • 1
    Tell us how far you were able to do, we will help you further, from where you are stuck :) – Aravind Anil Dec 26 '19 at 11:15
  • Can you show us what all methods you have tried? Or what thoughts you have had? If you are unable to even get a start point, then let us know. Before all that, post the question here in text, no pictures. – Rahul Bharadwaj Dec 26 '19 at 11:15

1 Answers1

-3

i wrote something, you can compile it online http://cpp.sh/6u447

#include <iostream>

int main()
{
  int arr[10] = { 0 };
  for(int i=0;i<10;i++){
      int temp ;
      std::cin>>temp;
      if(temp == 0){
        break;   
      }
      arr[i] = temp; 
  }
  int sum =0;
  for(int j=0;j<10;j++){
      sum+=arr[j];
  }
  std::cout<<"sum is "<<sum;
}
Mahdi Khalili
  • 1,025
  • 15
  • 32
  • oh yeah it works there, but I can't use it on the application that I use which is Code::Blocks – ReXy Ru Dec 26 '19 at 11:36
  • i edited my code without autos , you may have a problem with code:blocks , this code is perfectly working. can you tell me what is error at least? – Mahdi Khalili Dec 26 '19 at 11:40
  • 1
    it says error: 'i' does not name a type, and also expected ';' before 'i'. Same goes for 'j' – ReXy Ru Dec 26 '19 at 11:47
  • in first version of answer i wrote `auto i` but now test it with `int i` you may have different version of c++ it will only work on c++ 11 – Mahdi Khalili Dec 26 '19 at 11:50
  • also you can add c++11 https://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler – Mahdi Khalili Dec 26 '19 at 11:51