Help Debug Stoney L's CPP Code...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stoney L
    New Member
    • May 2022
    • 11

    Help Debug Stoney L's CPP Code...

    Could anyone tell me what's happening?I wrote:
    Code:
    #include<bits/stdc++.h>
    using namespace std;
    int days,gold,silver,copper,all,sum,sumg,sums,sumc;
    int main(){
    	days<=17;
    	cin>>days;
    	for(int i;i<=days;i++;){
    		cin>>gold>>silver>>copper;
    		sumg=gold+sumg;
    		sums=silver+sums;
    		sumc=copper+sumc;
    		sum=sumg+sums+sumc;
    	}
    	cout<<days<<" "<<sumg<<" "<<sums<<" "<<sumc<<" "<<sum;
    	return 0;
    }
    And it says:
    C:\Documents and Settings\Admini strator\My Documents\Untit led1.cpp In function 'int main()':
    7 23 C:\Documents and Settings\Admini strator\My Documents\Untit led1.cpp [Error] expected ')' before ';' token
    7 24 C:\Documents and Settings\Admini strator\My Documents\Untit led1.cpp [Error] expected primary-expression before ')' token
    7 24 C:\Documents and Settings\Admini strator\My Documents\Untit led1.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.
    Last edited by zmbd; May 28 '22, 07:08 PM. Reason: [Z{Please use the [CODE/] button to format posted script and formatted text - Please read the FAQ}{Updated the title}]
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    Code:
    days<=17;
    <= is a relational operator.

    Code:
    for(int i;i<=days;i++;){
    There should be no semicolon after the update expression. What is the initial value of i?

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Debug Stoney L's Code

      @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\Admini strator\My Documents\Untit led1.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:
      Code:
      #include <iostream>
      using namespace std;
      int z_days, z_gold, z_silver, z_copper, z_all, z_sum, z_sumg, z_sums, z_sumc;
      //
      int main()
      {
          z_days = 17;
          cout << "How Much: gold, silver, copper (press enter after each entry)" << endl;
          for (int i = 1; i <= z_days; i++) {
              cout << "Day " << i << " of " << z_days << endl;
              cout << "Gold?  ";
              cin >> z_gold;
              cout << "Silver? ";
              cin >> z_silver;
              cout << "Copper ";
              cin >> z_copper;
              cout << "---------------- " << endl;
              z_sumg = z_gold + z_sumg;
              z_sums = z_silver + z_sums;
              z_sumc = z_copper + z_sumc;
              z_sum = z_sumg + z_sums + z_sumc;
          }
          cout << "---------------- " << endl << "Days:" << z_days << "; SumG:" << z_sumg << "; SumS:" << z_sums << "; SumC:" << z_sumc << "; Total:" << z_sum << endl;
          return 0;
      }
      with an output (for 3 days...):
      Code:
      How Much: gold, silver, copper (press enter after each entry)
      Day 1 of 3
      Gold?  1
      Silver? 2
      Copper 3
      ----------------
      Day 2 of 3
      Gold?  4
      Silver? 5
      Copper 6
      ----------------
      Day 3 of 3
      Gold?  7
      Silver? 8
      Copper 9
      ----------------
      ----------------
      Days:3; SumG:12; SumS:15; SumC:18; Total:45
      Last edited by zmbd; May 28 '22, 07:22 PM.

      Comment

      • Stoney L
        New Member
        • May 2022
        • 11

        #4
        @zmbd:I haven't return? […]
        Last edited by zmbd; Jun 10 '22, 05:03 AM. Reason: [z{do not use profanity it will get you banned}]

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          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

          Comment

          Working...