473,422 Members | 1,935 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,422 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 16235
dev7060
638 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.