I cant work out what ive done wrong with my programme

+1 Alexandra Rasch · May 8, 2015
creating my first project with help from a book - but its coming up with about 20 errors - could somebody please help me?


/images/forum/upload/2015-05-08/1a66309964e9385d30b6da154868a655.png

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Alexandra Rasch · May 8, 2015
These are the errors i'm getting/images/forum/upload/2015-05-08/5f73ef7f21602540969f3c551f0b7df9.png
+1 Sharon Fabin · May 8, 2015
i have no idea whats the problem, i copied the same code and it works fine. Maybe you missed something little, try rewrite the code or copy the code to this forum (not as a pic) so it will be easier for us to spot the errors.
0 Michael Bradford · May 8, 2015
Yeah I just wrote this program myself as you show it and it worked just fine, I'd ask if your correctly saving as .cpp but you must be to include iostream.. a picture of the lines would be nice since at some point on line 16 it claims there is a constant, I'm not sure from IDE to IDE what line is what since sometimes they do or don't count whitespace. There's a red squiggly after the second for loop so I assume that's line 16 or 17 where it's happening but everything there appears to be fine, the only real advice I can give is check for random characters you might've typed off screen on line 16 or 17 that might be being interpretted as a constant.

[Begin Obligatory Style Nazi Rant]
Also this is not to do with your actual problem but generally for style I would avoid putting multiple statements on the same line (especially for someone who is learning) so things like k=nums[50]; nums = nums[j]; nums[j] = k; should be seperated to their own lines, it will save you a whole lot of trouble later in more complex programs. 
[End Style Nazi Rant]
0 Brian Reed · May 9, 2015
Alexandra,

I just typed in the code as pictured and it ran with no debug errors.
However, the expected results were out of whack!

I noticed that the function to fill in the array elements was formatted incorrectly.

The function's {Braces} are missing!   The correct code should look like below..
for(x=1; x<50; x++) {
nums[x]=x;

}

NOTE: For this post I had to substitute the i with an x,
Otherwise, the post conversion deletes both the i and [ ] brackets.

I ran the code it generated the winning $1,000,000 ticket, and I won!:)

I hope this was the issue.
Cheers,
Brian
0 Michael Bradford · May 9, 2015
Brian, just to clarify a couple things, One the for loop that fills the array is NOT a function (we all knew that, I know I know,but I had to say it anyway). Second the way Alexandra (and presumably the book she got this from) has that loop one hundred percent valid. It would be the same as using an if() statement as long as there is only ONE statement, it's perfectly valid and legal to omit the braces (though some would stylistically push against it).

But 
for(*whatever) x=i;  <--- perfectly valid
for(*whatever*)
0 Michael Bradford · May 9, 2015
Brian, just to clarify a couple things, One the for loop that fills the array is NOT a function (we all knew that, I know I know,but I had to say it anyway). Second the way Alexandra (and presumably the book she got this from) has that loop one hundred percent valid. It would be the same as using an if() statement as long as there is only ONE statement, it's perfectly valid and legal to omit the braces (though some would stylistically push against it).

But 
for(*whatever) x=i;  <--- perfectly valid
for(*whatever*)
0 Michael Bradford · May 9, 2015
Woops cut my own comment off there, 

for (*whatever*) 
     x=i;                         <---- perfectly valid

also 

if(*whatever)
   do x;                 <----- perfectly valid

or 

if(*whatever*) do x;      <--- perfectly valid

some people dislike this style but it's not in anyway invalid syntax.
0 c student · May 9, 2015
maybe there's something up with another file?  perhaps one which was compiled with this one or one of your headers is messed up?
0 Michael Bradford · May 9, 2015
This is the only file c student.
0 Brian Reed · May 9, 2015
Ouch!

Thanks Michael for pointing that out.

Not enough sleep I guess, but the funny thing is it worked out to get the right six numbers, whereas before the output was strings of about 8 random numbers for each expected 1-49. I'll want to figure out that anomaly out.
Anyway, once It was doing what it was supposed to, I just convinced myself I had figured it out. Bad move:pinch:

Cheers,
  • 1
  • 2

C++

107,338 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator