CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 42

Threaded View

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    okay i decided to take a different approach with globals instead of pointers.
    Why?

    The way you learn something is to write a small program instead of trying to shoehorn "learning" code into a larger, more complex program.
    Code:
    #include <stdio.h>
    
    void ptrTest(int *p)
    {
        *p = 20;
    }
    
    int main()
    {
       int x = 10;
       printf( "The value of x = &#37;d\n", x );
       ptrTest( &x );
       printf( "The value of x = %d\n", x );
    }
    OK, so what don't you understand about the above program? Do you see that the value of x changes to 20?

    That's why I pointed you to your own code with scanf() as a usage of pointers. Look at the call to ptrTest -- does it look familiar?

    Also, global variables should be avoided.
    It compiles, but the screen is black. Any input in this case?
    Please format your code a little better than you have now.
    Code:
    int menu();
    int addition(int reward); // function to return truth values
    int subtraction(int reward);
    
    int main()
    {
        int Result, Finished = 0;
        int choice, score = 0;
        int reward = 0;
    
        while(Finished != 1); // while finished is not true
        {
            printf("\nscore: %d", score); // print the score
            {
                choice = menu(); // calls for user input and enters it to switch
                switch  (choice){ // accesses the functions
                case 0: Finished = 1;                     break;
                case 1: Result = addition(reward);           break;
                case 2: Result = subtraction(reward);        break;
                default : printf("Error in input\n\n");      break;
                }
                if(Result == 1){
                    score += reward; // score = score + reward
                }
            }
        }
    }
    
    int menu() //main menu that gets called and allows fr user input
    {
        int MenuChoice;
        printf("\n");
        printf("Enter 1 for addition\n");
        printf("Enter 2 for Subtraction\n");
        printf("Enter 0 to quit\n");
        printf("-----------------\n");
        printf("Choice: ");
        scanf("%d", &MenuChoice); // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    int addition(int reward) //function that is called by the users selection
    {
        int answer;
        int random1, random2;
        srand(time(NULL)); //generates fresh numbers each time.
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
        printf("\n%d - %d = ", random1, random2); //code to format the random math problem
        scanf("%d", &answer); // user input of answer
        {
            if (answer == (random1+random2)){
                printf("Congratulations\n\n");
                return reward = 1;// tellsesult whether to add a pt or not
            }   //if user is correct than the score is incremented
            else{
                printf("Incorrect\n\n");} //if not than the score is not incremented
            return 0;
        }
    }
    
    int subtraction(int reward) // follows same model as previous function, but instead subtraction.
    {
        int answer;
        int random1, random2;
    
        srand(time(NULL));
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
        printf("\n%d - %d = ", random1, random2);
        scanf("%d", &answer);
    
        if (answer == (random1-random2)){
            printf("Congratulations\n\n");
            return reward = 1;// tells result whether to add a pt or not
        }
        else{
            printf("Incorrect\n\n");
        }
        return 0;
    }
    Also, are you using a debugger to debug your code?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 19th, 2012 at 05:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured