473,289 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Help Debug Stoney L's CPP Code...

11 Byte
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.
May 27 '22 #1
4 16205
dev7060
626 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?
May 28 '22 #2
zmbd
5,501 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
May 28 '22 #3
Stoney L
11 Byte
@zmbd:I haven't return? […]
Jun 5 '22 #4
zmbd
5,501 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
Jun 10 '22 #5

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.