+ 1
Can anyone please fix my code? I'm trying to compute the weight and the amount of coin (Basic)
6 Answers
+ 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.
+ 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.
+ 1
Thank you Eddy M for helping me. It totallyy worrksss!!
0
I couldn't put the input into the coin[] array correctly
0
I have ^^ Thank you so much Eddy M
0
Hello