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
+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 Sengngy Kouch · May 13, 2015
Hey there, 

My best guess is that you might open two .cpp files that are not related to each other in the same Project. 

I am assuming you are using Visual Studio from Microsoft.




The following is how to exclude your file from that project:

1. Right click on that xxx.cpp
2. Then choose "Exclude from Project".
3. Now try to build and compile your code again.



How to bring your xxx.cpp file back to the project: 
1. Right Click on the "Source File" 
2. Choose "add"
3. Then choose "Existing item".
4. now you can browse where your xxx.cpp file is.

Note:  You should only run one xxx.cpp file at a time unless they are related. ( Related means they are inherited, included, etc)

/images/forum/upload/2015-05-12/3ac6dd7375039bf0eb1991db5c16a110.jpg
0 Alexandra Rasch · May 8, 2015
These are the errors i'm getting/images/forum/upload/2015-05-08/5f73ef7f21602540969f3c551f0b7df9.png
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.
  • 1
  • 2

C++

107,311 followers
About

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

Links
Moderators
Bucky Roberts Administrator