0

I started learning cpp again after a while. Can you help me and tell me what's wrong with these if commands?

(The question will be in the comments) #include <iostream> using namespace std; int labour; //total number of workers int present; //workers that are present int yes; //workers that voted yes int present100; int yes100; int main() { cout << "Enter the number of the workers " << endl; cin >> labour; cout << "Enter the workers that are present " << endl; cin >> present; cout << "Enter the number of workers that voted for strike " << endl; cin >> yes; present100 = (present / labour) * 100; yes100 = (yes / present) * 100; if (((present100+1) >= 50) && (yes100 >= 75)) { cout << "Strike " << endl; } else { cout << "No strike " << endl; } system("PAUSE"); return 0; }

7th Feb 2020, 11:52 AM
Elias Kamakas
Elias Kamakas - avatar
3 Réponses
+ 1
Division of two integer values will result in another integer value. If you expect a floating point value, at least one of the operands should be a floating point value itself, for example through casting: present100 = ( static_cast< double >( present ) / labour ) * 100; For greater precision, both "present100" and "yes100" should be floating point values, too. Lastly, the condition ( present100 + 1 ) >= 50 checks if more than or equal to 49% of the workers are present, not if at least 50% + 1 of the workers attended the meeting.
7th Feb 2020, 12:08 PM
Shadow
Shadow - avatar
0
The exercise I picked up to do a refresh says: "If a union of workers wants to strike they have to attend a meeting. In order for the strike to happen, 50% of workers + 1 have to be there and 75% of those who are present have to vote yes for the strike to happen. Write a code that takes the total number of workers, the number of workers that were present in the meeting and the number that voted yes and says if a strike will happen or not."
7th Feb 2020, 11:53 AM
Elias Kamakas
Elias Kamakas - avatar
0
Shadow thanks a lot!
7th Feb 2020, 12:33 PM
Elias Kamakas
Elias Kamakas - avatar