• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Intro to C Programming-age: WTF am I doing wrong?

Status
Not open for further replies.

variance

Member
I'm taking an Intro to C Programming elective this semester and I just can't figure this problem out. I've done a lot of website coding with PHP so I thought I was good at this haha. Guess not.

Everything works correctly except for some reason the CorrectProblems and IncorrectProblems integers are returning some absurdly high numbers at the end, no matter how many I do in fact get Correct/Incorrect. Am I just missing something obvious?

It prompts the right number of questions, calculates time correct, outputs random numbers fine... it just won't get Incorrect/Correct right, and it's throwing off the score at the end.

Pleaaaaaaase help I can't figure it out. I've tried using CorrectProblems++; / CorrectProblems += 1; / CorrectProblems = CorrectProblems + 1; to increment it, but they all return the same stupidly high numbers.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    //Declare variables
    int NumberofProblems;
    int CorrectProblems, IncorrectProblems;
    int N1, N2;
    int Solution, ProblemX, Guess;
    int Score;
    
    //Seed the random number generator
    srand(time(0));
    
    //Prompt for Number of Problems to answer
    printf("How many problems do you want?\n");
    scanf("%d",&NumberofProblems);
    
    //Start problems counter at one
    ProblemX = 1;
    
    //While loop to present requested # of problems
    int start = time(0);
    while (ProblemX <= NumberofProblems)
    {
          //Select two random numbers
          N1 = rand()%13;
          N2 = rand()%13;
          
          //Prompt question
          printf("Answer: %d x %d = \n",N1,N2);
          scanf("%d",&Guess);
          
          //Find solution
          Solution = N1 * N2; 
          
          //See if user answer equals solution
          if (Guess == Solution)
          {
             //If so, output "Correct" and add one to CorrectProblems
             printf("Correct!\n\n");
             CorrectProblems += 1;
          } else if (Guess != Solution) {
             //If not, output "Incorrect" and give correct answer
             printf("Incorrect, %d x %d = %d\n\n",N1,N2,Solution);
             IncorrectProblems += 1;
          }
    
          //Increment Problem #
          ProblemX++;
    }
    
    //All problems have been answered, stop timer and get total time spent
    int end = time(0);
    int timespent = end - start;
    
    //Output results
    printf("You got %d problems correct and %d problems incorrect in %d seconds.\n",CorrectProblems,IncorrectProblems,timespent);
    
    //Calculate score
    Score = (timespent + 5) * IncorrectProblems;
    
    //Output score
    printf("Your final score is %d!\n",Score);
    
    system("pause");
    return 0;
}
 

Ecrofirt

Member
So you know, the reason you've got to initialize the variables to 0 is because when C allocates the space for the variable it doesn't automatically clear out what was previously in that portion of memory.
 
Ecrofirt said:
So you know, the reason you've got to initialize the variables to 0 is because when C allocates the space for the variable it doesn't automatically clear out what was previously in that portion of memory.

i dont think thats the same case in java/c# right?
 

Ecrofirt

Member
artredis1980 said:
i dont think thats the same case in java/c# right?
C# doesn't let you use things like int unless you initialize them.

For instance, the following code fails in C#:
private void foo()
{
int lol;
lol++;
}

And it fails saying: Use of unassigned local variable 'lol'
 

Chichikov

Member
Most C++ compilers should generate a warning on that code.
Let there be a lesson, you should at the very least look at the warnings you get when writing code, especially in C/C++.
 

variance

Member
I use Dev C++ and I haven't seen any warning messages before... where would those be at?

I'm used to using VB.NET and PHP, and debugging is insanely easier in those programs because they tell you exactly where the issues are.

I'll definitely not forget to initialize variables from this point forward, thanks guys.
 

Cush

Member
Ecrofirt said:
C# doesn't let you use things like int unless you initialize them.

For instance, the following code fails in C#:


And it fails saying: Use of unassigned local variable 'lol'

Same thing for Java with local variables - you'll get a compile error for that.

For instance variables though, ints will be initialized to 0 for you.
 
Chichikov said:
Most C++ compilers should generate a warning on that code.
Let there be a lesson, you should at the very least look at the warnings you get when writing code, especially in C/C++.
But why should he compile his C code with a C++ compiler?
 

tokkun

Member
Chichikov said:
Most C++ compilers should generate a warning on that code.
Let there be a lesson, you should at the very least look at the warnings you get when writing code, especially in C/C++.

gcc does not generate any warnings on that code, even with -Wall.
 
Yeah, not initializing the viable means whatever memory address it ends up using will retain whatever state (value) it had in the past. So you end up with something close to a random number there.

tldr: Initialize your variables.
 
variance said:
We're required to use Dev C++.
It seems like a stupid idea to not use a compiler for the language you are using (even if most, but far from all, C code is valid C++ code). Tell your instructor that I said that!
 

Dr_Cogent

Banned
I'm surprised they are teaching straight C and not C++.

Great thing about C++ is initialization on the same line as declaration.

Code:
int CorrectProblems = 0;
 

Slavik81

Member
Dr_Cogent said:
I'm surprised they are teaching straight C and not C++.

Great thing about C++ is initialization on the same line as declaration.

Code:
int CorrectProblems = 0;
Can't you do that in C?

Gah. C/C++/C#/Java are going to murder me when I go job hunting....
 

Zoe

Member
Dr_Cogent said:
I could have sworn you can't. Perhaps I am thinking of another language.

I'm remembering something along those lines too, but I'm not sure if that's it exactly.

I never used C much :\
 
Status
Not open for further replies.
Top Bottom