Thread: Errors in Math "tutor"

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Unhappy 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{
                    printf("Enter answer: ");
                    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{
                        printf("Enter answer: ");
                        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{
                        printf("Enter answer: ");
                        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{
                    printf("Enter answer: ");
                    scanf_s("%d", &ans1);
                    if (ans1==ans){
                        printf("Correct!");
                        break;
                    }
                    }while( ans1 != ans);
                }
                break;
        default:
            printf("nothing");
            break;
        }
            _getch();
            return 0;
    }

    Can someone please help me?please
    Last edited by eLg; 07-12-2013 at 10:14 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    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. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    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. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by eLg View Post
    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 ;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    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. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by jimblumberg View Post
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by vart View Post
    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. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    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. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by eLg View Post
    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.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by eLg View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  11. #11
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by vart View Post
    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. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    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. #13
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by vart View Post
    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. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by eLg View Post
    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)
    1. define your vars
    2. call srand
    3. populate your vars with random data

    See my sample in post#4
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  15. #15
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by vart View Post
    It will - if you read the error statement and define the suggested macro in your project settings
    which macro should I use? The error statement is showing that scanf_s should be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  2. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  3. Replies: 3
    Last Post: 08-31-2005, 12:41 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread