I am trying to write a simple calculation program but the answer keeps coming back as 0

前端 未结 1 950
不知归路
不知归路 2021-01-29 10:12

I have spent a couple hours trying to figure out why this comes back as 0 If someone could help that would be great.

  1 /*Written by Connor Green*/
  2 /*CISP 1         


        
相关标签:
1条回答
  • 2021-01-29 10:37

    That's because you calculate number_of_omelettes before getting the input variables for this calculation. Move that calculation to just before outputting it:

    ...
    number_of_omelettes = carton_size * number_cartons / eggs_per_omelette;
    cout << "You can make " << number_of_omelettes << " omelettes with this amount of eggs.\n";
    

    Also, beware that the division / operator precedes the multiplication * operator, which could result in zero due to integer division. To avoid this, force the multiplication to precede the division, using parentheses:

    number_of_omelettes = (carton_size * number_cartons) / eggs_per_omelette;
    
    0 讨论(0)
提交回复
热议问题