470,833 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Help Debug Stoney L's CPP Code...

5 Nibble
Could anyone tell me what's happening?I wrote:
Expand|Select|Wrap|Line Numbers
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int days,gold,silver,copper,all,sum,sumg,sums,sumc;
  4. int main(){
  5.     days<=17;
  6.     cin>>days;
  7.     for(int i;i<=days;i++;){
  8.         cin>>gold>>silver>>copper;
  9.         sumg=gold+sumg;
  10.         sums=silver+sums;
  11.         sumc=copper+sumc;
  12.         sum=sumg+sums+sumc;
  13.     }
  14.     cout<<days<<" "<<sumg<<" "<<sums<<" "<<sumc<<" "<<sum;
  15.     return 0;
  16. }
And it says:
C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp In function 'int main()':
7 23 C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp [Error] expected ')' before ';' token
7 24 C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp [Error] expected primary-expression before ')' token
7 24 C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp [Error] expected ';' before ')' token
Why does this happened?
Tips:please send it before 2022/5/28, because that day is my coding class, I need to finish this question before the class.
4 Weeks Ago #1
4 15044
dev7060
575 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. days<=17;
<= is a relational operator.

Expand|Select|Wrap|Line Numbers
  1. for(int i;i<=days;i++;){
There should be no semicolon after the update expression. What is the initial value of i?
4 Weeks Ago #2
zmbd
5,463 Expert Mod 4TB
@dev7060 - thank you for such a quick reply
Unfortunately, Stoney hasn't returned since the post was made - for those that follow, it's really rude to say something is urgent with a deadline, in this case 2022-05-28, (as I write this it is 2022-05-28-19H10-UTC) and then not return.

For those that follow:
+First problem is on line 5 days<=17;, as dev7060 pointed out, this is a conditional comparison. One can only guess that OP intended for the loop that follows to have 17 itterations; therefore, the code is corrected to days=17;
(My personal preference is to never use "day" or "days" as a variable name in that these are often reserved keywords - IMNSHO one should use something like "myDay" or "varDay" etc... i tend to use things like "zDay" or "z_Day"
+
7 23 C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp [Error] expected ')' before ';' token
(the remaining errors are related to this one) indicates that there is a syntax error on line 7 position 23 for(int i;i<=days;i++;). One can only guess that this was a typo as the code could be for(int i;i<=days;i++); infact that trailing semicolin could be omitted (at least in VS2019IDE) (yes, there are other issues here)!

+Correcting this to for(int i;i<=days;i++) creates an infinite loop in that the conditional will always be true in that the variable was set to a null value which will not (in/de)-crement. So let's correct that issue with for (int i = 1; i <= days; i++)

+ So now the code will compile in the debugger and run... wait for it... ; HOWEVER, there are a few improvements.
As written this code is user and compiler unfriendly! When I took my CompSci courses (ages ago, dinosaurs walked the Earth!) we were graded on three points: does the code meet the project rubric, is the code compiler friendly (does not include things that are not needed); is the user feed-back human friendly (could someone execute the program and know what to do during interaction)
> Lets take care of the compiler unfriendly
#include <stdc++.h> includes a lot of stuff that simply isn't needed in this code. All that is needed is #include <iostream>
As for the user unfriendly - break that huge cin appart and instert a few prompts, some user message, format the output a bit and you get something like:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int z_days, z_gold, z_silver, z_copper, z_all, z_sum, z_sumg, z_sums, z_sumc;
  4. //
  5. int main()
  6. {
  7.     z_days = 17;
  8.     cout << "How Much: gold, silver, copper (press enter after each entry)" << endl;
  9.     for (int i = 1; i <= z_days; i++) {
  10.         cout << "Day " << i << " of " << z_days << endl;
  11.         cout << "Gold?  ";
  12.         cin >> z_gold;
  13.         cout << "Silver? ";
  14.         cin >> z_silver;
  15.         cout << "Copper ";
  16.         cin >> z_copper;
  17.         cout << "---------------- " << endl;
  18.         z_sumg = z_gold + z_sumg;
  19.         z_sums = z_silver + z_sums;
  20.         z_sumc = z_copper + z_sumc;
  21.         z_sum = z_sumg + z_sums + z_sumc;
  22.     }
  23.     cout << "---------------- " << endl << "Days:" << z_days << "; SumG:" << z_sumg << "; SumS:" << z_sums << "; SumC:" << z_sumc << "; Total:" << z_sum << endl;
  24.     return 0;
  25. }
with an output (for 3 days...):
Expand|Select|Wrap|Line Numbers
  1. How Much: gold, silver, copper (press enter after each entry)
  2. Day 1 of 3
  3. Gold?  1
  4. Silver? 2
  5. Copper 3
  6. ----------------
  7. Day 2 of 3
  8. Gold?  4
  9. Silver? 5
  10. Copper 6
  11. ----------------
  12. Day 3 of 3
  13. Gold?  7
  14. Silver? 8
  15. Copper 9
  16. ----------------
  17. ----------------
  18. Days:3; SumG:12; SumS:15; SumC:18; Total:45
4 Weeks Ago #3
Stoney L
5 Nibble
@zmbd:I haven't return? […]
3 Weeks Ago #4
zmbd
5,463 Expert Mod 4TB
Stoney

At the time of my post you had not logged into your account. As a moderator I can see when you have logged into your account amongst other stats

You posted an “urgent” request for help with a drop-dead date of BEFORE May28th. Dev7060 posted a timely reply - good manners would be to sign in and thank the person for their help - you did not as of the close of normal business on the 28th

There you have it
2 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.