| captain47 (6) | |||
|
Hey, everyone. I'm having a problem with a code I'm working on. I just started learning C++ about two weeks ago, so bear with the potentially messy and unconventional way of my coding. The program is basically a math sheet where you select the difficulty and amount of questions, and you answer randomly generated questions, and it scores you in the end. After the first question's answer is inputted, I get a "Debug Error", and it says: "Run-Time Check Failure #3 - The variable 'totalscore' is being used without being initialized." Here's a snippet of my code:
I can kind of tell where the problem is. It has to be under the "// Problem" header; something to do with the while code and the variables that are in them, that should be out of them or something. I've messed around with it a bit and couldn't figure it out. Thanks ahead! | |||
|
Last edited on
|
|||
| xhtmlx (222) | |||||
You only need to seed the random number generator once. Just throw it to the top of the main function.
I really hate to comment on the code members post, but I feel like I need to here.
Those two lines are both evil, and I mean just terrible to have in your program. Calling system is very resourceful and leaves a huge security hole in your program. Goto statements and labels are terrible practice and make your code incredibly difficult to understand. Most of the time goto statements and labels can be replaced with loops or functions. Why System is Evil: http://www.cplusplus.com/forum/articles/11153/ | |||||
|
Last edited on
|
|||||
| TheIdeasMan (1564) | ||
The problem is exactly that - you have not set totalscore equal to any thing then tried to print it out. If you get into the practice of initialising variables (even to 0) when you declare them (or in a constructor if using classes), then you won't have this problem - just the answer will be wrong (0 say). | ||
|
|
||
| captain47 (6) | |||||
|
Alright, so the srand goes out of the loop, got that. As far as the system/goto commands, I know they're bad. But, for now since I'm only a beginner and this isn't an official project or anything, just something I'm messing around with for me, I'll keep it. But eventually, I'll rework the code to not include those commands. Thanks! Ah, I get it! So total score is the exact same as questionAmount. But I forgot why I needed to create another variable for questionAmount. I remember I was seriously doing some hard thinking about that, and there was a reason why I needed two variables for the same amount (or maybe there isn't a reason). So I'll just make totalscore equal the questionAmount by doing:
I also noticed my question number wasn't going up, was stuck at question 1, so I added this:
And I just noticed it would stop at question 2/3, rather than 3/3, so I added: while (question < questionAmount + 1) {And it seems to be working just fine now. Thanks for the help, guys! | |||||
|
|
|||||
| TheIdeasMan (1564) | |||||
Consider putting this into a function called ShowMenu()
Now for this part: int score(0), totalscore(questionAmount);Does that even compile? You can't separate statements with commas like that for a start. and int score(0)should be int score = 0;Consider using a switch statement instead of this:
IF you don't know what a switch is then Google C++ switch example also make your variable names shorter - mathSheetDifficulty is too long, why not just Difficulty? question += 1; can be question++ ; (the increment operator.)Hope this helps. | |||||
|
|
|||||
| captain47 (6) | |
|
I'll look into ShowMenu(), never heard of that code before. Sounds interesting. And yes, that does compile just fine. My C++ professor told me that's just not a "proper" way of coding. Doing score(0) works the same as = 0 (at least from what I know, it works just fine). I actually just learned about switch statements last night. It just looks cleaner than all the if's but works the same (right?). I read this: http://www.cplusplus.com/doc/tutorial/control/ and it really helped. Might give it a re-read soon. Names, yes. Need to shorten those, and if that saves two characters, sure why not. I forgot about the ++'s and --'s. Thanks! | |
|
|
|
| TheIdeasMan (1564) | ||
It's not a standard thing - just trying to introduce the idea of using functions is a good practice. | ||
|
|
||