# Thread: Errors in Math "tutor"

1. ## Errors in Math "tutor"

Hi!,
I am a beginner and I am trying to create a program that will randomly generate two numbers from 1 to 10, and an operator. The program will ask for the user to enter the answer. The program is giving me these errors:
Code:
``` error C2065: 'num2' : undeclared identifier
error C2143: syntax error : missing ';' before 'type':eek: ```

Here is the Code
Code:
```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

int main(){

int ans;
int ans1;
srand((unsigned int)time(NULL));
int num1 = rand() % 10  + 2;
int num2 = rand() % 10;
int operation = rand() % 4 + 1 ;

printf("\tMATH TUTOR\n");
switch(operation){
case 1:
printf("What is %d + %d ?", num1, num2);
ans = num1 + num2;
scanf_s("%d", &ans1);
if(ans == ans1){
printf("Correct!");
}else{
printf("Incorrect! Try Again! Expected answer %d but you typed %d\n", ans, ans1);
do{
scanf_s("%d", &ans1);
if (ans1==ans){
printf("Correct!");
break;
}
}while( ans1 != ans);
}
break;
case 2:
printf("What is %d - %d ?", num1, num2);
scanf_s("%d",&ans1);
ans = num1 - num2;
if(ans == ans1){
printf("Correct!");
}else{
printf("Incorrect! Try Again! Expected answer %d but you typed %d\n", ans, ans1);
do{
scanf_s("%d", &ans1);
if (ans1==ans){
printf("Correct!");
break;
}
}while( ans1 != ans);
}
break;
case 3:
printf("What is %d * %d ?", num1,num2);
scanf_s("%d",&ans1);
ans = num1 * num2;
if(ans == ans1){
printf("Correct!");
}else{
printf("Incorrect! Try Again! Expected answer %d but you typed %d\n", ans, ans1);
do{
scanf_s("%d", &ans1);
if (ans1==ans){
printf("Correct!");
break;
}
}while( ans1 != ans);
}
break;
case 4:
printf("What is %d / %d ?", num1, num2);
scanf_s("%d",&ans1);
ans = num1 / num2;
if(ans == ans1){
printf("Correct!");
}else{
printf("Incorrect! Try Again! Expected answer %d but you typed %d\n", ans, ans1);
do{
scanf_s("%d", &ans1);
if (ans1==ans){
printf("Correct!");
break;
}
}while( ans1 != ans);
}
break;
default:
printf("nothing");
break;
}
_getch();
return 0;
}```

2. Since you're using the outdated Microsoft compiler, you'll need to insure your variables are all declared before any of them are used. C90 requires all varialbes to be defined at the beginning of the scope and before any calculations or function calls.

Jim

3. Originally Posted by jimblumberg
Since you're using the outdated Microsoft compiler, you'll need to insure your variables are all declared before any of them are used. C90 requires all varialbes to be defined at the beginning of the scope and before any calculations or function calls.

Jim
Is there a way to change the compiler in VS2010? and I think the variable are defined on top. Did it work in your IDE?

4. Originally Posted by eLg
and I think the variable are defined on top.
you are wrong here
Code:
```int ans;
int ans1;
srand((unsigned int)time(NULL));
int num1 = rand() % 10  + 2;
int num2 = rand() % 10;
int operation = rand() % 4 + 1 ;```
This should be
Code:
```int ans;
int ans1;
int num1;
int num2;
int operation;
srand((unsigned int)time(NULL));
num1 = rand() % 10  + 2;
num2 = rand() % 10;
operation = rand() % 4 + 1 ;```

5. Is there a way to change the compiler in VS2010?
No, if you want a different compiler you'll need to download a different IDE, made by someone else. Mircrosoft doesn't support anything other than C90 with their comilers.

Did it work in your IDE?
No, you're program won't compile in my compiler but for other reasons. My compiler doesn't support the non-standard conio.h header file. And it also doesn't support the non-standard scanf_s() function. But after fixing these issues the code does properly complie with my C11 compliant compiler.

Jim

6. Originally Posted by jimblumberg
No, if you want a different compiler you'll need to download a different IDE, made by someone else. Mircrosoft doesn't support anything other than C90 with their comilers.
Strictly speaking - you can use different compilers with Microsoft VS IDE

About compilers -they claim that they do not plan to add support of C99 standard as is, but will support "features of C99 standard that are part of C++11 standard"

Does it mean that you have to compile your C code as C++ to get these features - I have no idea...

7. Originally Posted by vart
you are wrong here
Code:
```int ans;
int ans1;
srand((unsigned int)time(NULL));
int num1 = rand() % 10  + 2;
int num2 = rand() % 10;
int operation = rand() % 4 + 1 ;```
This should be
Code:
```int ans;
int ans1;
int num1;
int num2;
int operation;
srand((unsigned int)time(NULL));
num1 = rand() % 10  + 2;
num2 = rand() % 10;
operation = rand() % 4 + 1 ;```
What is wrong in this part?
If I removed unsigned int, it will give the same number.
If I did not removed unsigned int, it will say undeclared variables.

8. Originally Posted by jimblumberg
No, if you want a different compiler you'll need to download a different IDE, made by someone else. Mircrosoft doesn't support anything other than C90 with their comilers.

No, you're program won't compile in my compiler but for other reasons. My compiler doesn't support the non-standard conio.h header file. And it also doesn't support the non-standard scanf_s() function. But after fixing these issues the code does properly complie with my C11 compliant compiler.

Jim
What changes did you do? What is your IDE? Which IDE do you suggest for beginners in C programming?
And about the scanf_s, VS 2010 won't allow me to compile the program if it is scanf().

9. Originally Posted by eLg
What is wrong in this part?
Not in this part. In the following statement

and I think the variable are defined on top.
You have variable definitions after the part marked in red.
And so they are not defined "on top" but after some other code.

10. Originally Posted by eLg
VS 2010 won't allow me to compile the program if it is scanf().
It will - if you read the error statement and define the suggested macro in your project settings

11. Originally Posted by vart
Strictly speaking - you can use different compilers with Microsoft VS IDE

About compilers -they claim that they do not plan to add support of C99 standard as is, but will support "features of C99 standard that are part of C++11 standard"

Does it mean that you have to compile your C code as C++ to get these features - I have no idea...
if I will compile my code as C++, does that mean that I have to change the name of the libraries I included?

12. What changes did you do?
I removed the conio.h include file and getch(). Then I also changed the non-standard scanf_s() to the standard scanf().

What is your IDE? Which IDE do you suggest for beginners in C programming?
For only C on Windows Pelles C is a good choice. If you need both C and C++ I recommend Code::Blocks with gcc. If you only need C++ then Visual C++ is an acceptable choice in my opinion.

Jim

13. Originally Posted by vart
Not in this part. In the following statement

You have variable definitions after the part marked in red.
And so they are not defined "on top" but after some other code.
oh ok. If I place the srand() above all the codes, all the variable will be undefined, according to VS

14. Originally Posted by eLg
oh ok. If I place the srand() above all the codes, all the variable will be undefined, according to VS
So you should (to be compliant with C90)