+ 1

[SOLVED] Need help, code doesn't work as expected, no errors

Ticket Office You are working on a ticketing system. A ticket costs $10. The office is running a discount campaign: each group of 5 people is getting a discount, which is determined by the age of the youngest person in the group. You need to create a program that takes the ages of all 5 people as input and outputs the total price of the tickets. Sample Input: 55 28 15 38 63 Sample Output: 42.5 The youngest age is 15, so the group gets a 15% discount from the total price, which is $50 - 15% = $42.5 My code: #include <iostream> using namespace std; int main() { int ages[5]; for(int i=0; i<5; ++i) { cin >> ages[1]; } //your code goes here int yg = ages[0]; for(int t=0; t<5; ++t) { if(yg>ages[t]) { yg = ages[t]; } } double disc=0, total=50; disc = total-((total*yg)/100); cout << disc << endl; return 0; } // outputs 50 with the sample imput, but I expected it to output 42.5

24th Apr 2021, 8:59 PM
Samuel Aguirre
5 odpowiedzi
+ 2
Only thing you did wrong is that in first loop you wrote "cin >> ages[1]" instead of "cin >> ages[i]"😅
24th Apr 2021, 9:28 PM
Nazeekk
Nazeekk - avatar
+ 1
Please try cin >> ages[i] instead of cin >> ages[1]
24th Apr 2021, 9:32 PM
Coding Cat
Coding Cat - avatar
+ 1
Oh thank you @Nazeekk, it worked. The thing is, the code bit above the comment that says " //your code goes here" was already there and I wasn't supposed to change it since it was already there in the C++ quiz, but this really seems like the only way for the rest of the code to work, i've been trying different things for over an hour and nothing but this worked
24th Apr 2021, 9:33 PM
Samuel Aguirre
+ 1
In the first loop, you assign the inputs only to the ages' second element which is ages[1]. You don't store any other inputs in the other elements. To store them correctly, you must change it to ages[i]. Later you can check if your algorithm works out.
24th Apr 2021, 9:34 PM
ogoxu
ogoxu - avatar
+ 1
It worked. The thing is, the issue was in the part of the code that was already there in the quiz section, so apparently it was wrong. I was confident you didn't have to change the part above "your code goes here" but I will double check all of the code from now on.
24th Apr 2021, 9:37 PM
Samuel Aguirre