+ 1

Can anyone please fix my code? I'm trying to compute the weight and the amount of coin (Basic)

https://code.sololearn.com/cGUFLz3btgcx/?ref=app

22nd Mar 2021, 8:48 AM
Jessica Zarate
Jessica Zarate - avatar
6 Réponses
+ 3
That's because you first print the uninitialized values in coin[] and then you read the values one by one and for every value you do some calculations This is going to work instead: cout<<"Enter a number of coins\n"; cout<<"Penny, Nickle, Dime, Quarter, Half Dollar, Dollar:\n\n"; for(i=0; i<6; i++){ // it was: for(i=0; i<coin[6]; i++){ cin>>coin[i]; } cout<<"Penny="<<coin[0]<<endl; cout<<"Nickle="<<coin[1]<<endl; cout<<"Dime="<<coin[2]<<endl; cout<<"Quarter="<<coin[3]<<endl; cout<<"Half Dollar="<<coin[4]<<endl; cout<<"Dollar="<<coin[5]<<endl; ... Make sure you also remove the } before "total_amount =" because it now moved after: cin>>coin[i]; Check how it works after making this change.
22nd Mar 2021, 9:30 AM
Eddy M
+ 1
Well the computation of totals is correct. But: You output the numbers before you have read them. C++ doesn't initialize variables on its own, so the random values you see are whatever was at those memory locations before. You compute the totals six times, because that code is in the loop. Move the closing bracket from line 56 to 39. This doesn't produce errors, but is certainly not what you wanted. Use indention to visualize the structure if your code. Double check your values, especially dollar amount.
22nd Mar 2021, 9:31 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Thank you Eddy M for helping me. It totallyy worrksss!!
22nd Mar 2021, 9:34 AM
Jessica Zarate
Jessica Zarate - avatar
0
I couldn't put the input into the coin[] array correctly
22nd Mar 2021, 8:50 AM
Jessica Zarate
Jessica Zarate - avatar
0
I have ^^ Thank you so much Eddy M
22nd Mar 2021, 9:45 AM
Jessica Zarate
Jessica Zarate - avatar
0
Hello
24th Mar 2021, 12:04 AM
Kei 0377.exe
Kei 0377.exe - avatar