Thread: Need help starting a program with functions

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Question Need help starting a program with functions

    So basically, one of my homeworks is to make a program that will ask arithmetic questions and some factoring questions to the user. The functions' purpose is to calculate how long it took the user to answer the questions and will return a certain number of points (Ill ask that question later on).

    I just want to start the arithmetic questions for now.


    So this is what I have so far:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define ADD 1
    #define MULT 2
    
    //These are the pre-functions, given from the homework outline. 
    
    double arithGAME (int max, int quantity, int op);
    double factorGAME (int max, int quantity);
    int numPoints ( double timesec);
    
    
    int main () {
         
        int game;
        int choice, game_type;
        int max_value, max_numb, numb_prob;
        int max_questions;
        
        
        while (game != 4){
        
            printf("Please make a selection from the following:\n");
            printf("1.  Play Arithmetic Game.\n");
            printf("2.  Play Factoring Game.\n");
            printf("3.  Print Score.\n");
            printf("4.  Quit.\n");
      
    //This is the part where the user will choose what type of game    
       
            scanf("%d", &game);
    
    //This is Game 1, which is the arthimetic game
    
            if (game==1){
                printf("Would you like, 1)Addition or 2) Multiplication?\n");
                scanf("%d", &choice);
                
                if (choice==1)
                    game_type= ADD;
                else
                    game_type = MULT;
             printf("What is the maximum number you would like?\n");
             scanf("%d", max_numb);
             
             printf("How many problems do you want?\n");
             scanf("%d", max_questions);      
                    
             arithGAME (max_numb, max_questions, game_type);
                
             }                 
    //This is Game 2, which is the factoring game        
            else if (game==2){
                 printf("Enter the maximum value of a factor");
                 scanf("%d", &max_value);
                 
                 printf("How many factoring problems do you want?");
                 scanf("%d", &numb_prob);
                 
                 factorGAME( max_value, numb_prob);
                 
             }      
    
    //This choice 3, which prints out the score
             else if (game==3){
                  
             }
         }
        system("PAUSE");
        return 0;
        
    }
    
    
    //These are the Function's presets
    
    //This the Arthicmetic function
    double arithGAME (int max, int quantity, int op) {
          srand(time(0));
         
    }
    
    
    //This is the Factoring function
    double factorGAME (int max, int quantity) {
          // srand(time(0));
    }
    Mainly, I need help setting up the questions.


    I want the outline to start something like:

    Please make a selection from the following:
    1. Play Arithmetic Game.
    2. Play Factoring Game.
    3. Print Score.
    4. Quit.
    1


    Would you like, 1)Addition or 2)Multiplication?
    1
    What is the maximum number you would like?
    100
    How many problems do you want?
    4
    What is 21+86?
    107
    Correct, great job!
    What is 87+96?
    173
    Sorry, that's incorrect, the answer is 183.
    What is 86+70?
    156
    Correct, great job!
    What is 55+4?
    59
    Correct, great job!

    So my question is, when setting up the questions, do I put in the main function, or separate function?

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    If it is in the main function, this is what I wrote, under Game 1:

    Code:
             srand(time(0));
             numb_prob= 1 + max_questions;
             
                   for(i=0; i<= numb_prob; i++){
                      
                      
                   }
    How would you differentiate from using Addition and Multiplication?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There a couple of things you should fix, although I understand you are not completely finished with some functions, so you can ignore some of the errors and warnings:
    Warning 1 warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data 80
    Warning 2 warning C4100: 'op' : unreferenced formal parameter 79
    Warning 3 warning C4100: 'quantity' : unreferenced formal parameter 79
    Warning 4 warning C4100: 'max' : unreferenced formal parameter 79
    Warning 5 warning C4100: 'quantity' : unreferenced formal parameter 86
    Warning 6 warning C4100: 'max' : unreferenced formal parameter 86
    Warning 7 warning C6031: Return value ignored: 'scanf' 32
    Warning 8 warning C6031: Return value ignored: 'scanf' 38
    Warning 9 warning C6031: Return value ignored: 'scanf' 45
    Warning 10 warning C6066: Non-pointer passed as parameter '2' when pointer is required in call to 'scanf' 45
    Warning 11 warning C6031: Return value ignored: 'scanf' 48
    Warning 12 warning C6066: Non-pointer passed as parameter '2' when pointer is required in call to 'scanf' 48
    Warning 13 warning C6031: Return value ignored: 'scanf' 56
    Warning 14 warning C6031: Return value ignored: 'scanf' 59
    Warning 15 warning C6001: Using uninitialized memory 'game': Lines: 16, 17, 18, 19, 22 22
    Warning 16 warning C6001: Using uninitialized memory 'max_numb': Lines: 16, 17, 18, 19, 22, 24, 25, 26, 27, 28, 32, 36, 54, 66, 22, 24, 25, 26, 27, 28, 32, 36, 54, 66, 22, 24, 25, 26, 27, 28, 32, 36, 37, 38, 40, 43, 44, 45 45
    Warning 17 warning C6001: Using uninitialized memory 'max_questions': Lines: 16, 17, 18, 19, 22, 24, 25, 26, 27, 28, 32, 36, 54, 66, 22, 24, 25, 26, 27, 28, 32, 36, 54, 66, 22, 24, 25, 26, 27, 28, 32, 36, 37, 38, 40, 43, 44, 45, 47, 48 48
    Warning 18 warning C4700: uninitialized local variable 'game' used 22
    Warning 19 warning C4700: uninitialized local variable 'max_numb' used 45
    Warning 20 warning C4700: uninitialized local variable 'max_questions' used 48
    Error 21 error C4716: 'arithGAME' : must return a value 82
    Error 22 error C4716: 'factorGAME' : must return a value 88

    The warnings in red are critical to fix.
    You can ignore the scanf return value warnings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    I'm sorry, but I'm not sure what I need to fix, based on what you've posted

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    So far, I've written this, under Game 1:

    Code:
    //This is Game 1, which is the arthimetic game
    
            if (game==1){
                printf("Would you like, 1)Addition or 2) Multiplication?\n");
                scanf("%d", &choice);
                
                if (choice==1)
                    game_type= ADD;
                else
                    game_type = MULT;
             printf("What is the maximum number you would like?\n");
             scanf("%d", &max_numb);
             
             printf("How many problems do you want?\n");
             scanf("%d", &max_questions);      
                    
             arithGAME (max_numb, max_questions, game_type);
             
             srand(time(0));
             numb_prob= 1 + max_questions;
             
                   for(i=0; i<= numb_prob; i++){
                            
                                if (game_type==ADD){
                                    type_question_1 = 1 + rand()%max_numb; 
                                    type_question_2 = 1 + rand()%max_numb; 
                                    type_question_1 + type_question_2 = correct_ans;
                                    
                                printf("What is %d + %d?\n", type_question_1, type_question_2);
                                scanf("%d", &user_ans);
                                       if (user_ans==correct_ans)
                                          printf("Correct, great job!\n");
                                       else 
                                          printf("Sorry, that's incorrect. The correct answer is %d\n", correct_ans);             
                                 }
                      
                   }
             
             
                
             }

    However, the bolded line is saying that is "invalid Ivalue in assignment.

    What I am doing wrong?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The last numbers, ie 45 points to the specific lines. For example, Non-pointer passed as parameter '2' when pointer is required in call to 'scanf' means that you did not pass a pointer to scanf. Since scanf requires a pointer, this would probably result in an instant crash.
    The "Using uninitialized memory" problem means you try to use unallocated memory, quite simply. You must allocate what you use.
    "uninitialized local variable 'x' used" - You are using an uninitialized variable. Variables contain garage unless initialized, so initialize them.

    Unlike math, the variable you assign to must be on the left side of the = sign.
    The error in itself says:
    "invalid" - like it says, invalid.
    "lvalue" - left-hand side expression, eg what is left of the assignment.
    "in assignment" - telling us there was an error in the assignment.
    Last edited by Elysia; 11-01-2009 at 02:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    I was introduce to pointer on Friday, so I'm still confused about it, but thanks. Ill take a look at it again.

    I fixed the latter problem and it works fine now

    Code:
    for(i=1; i<= max_questions; i++){
                            
                                if (game_type==ADD){
                                    type_question_1 = 1 + rand()%max_numb; 
                                    type_question_2 = 1 + rand()%max_numb; 
                                    correct_ans= type_question_1 + type_question_2;
                                    
                                     printf("What is %d + %d?\n", type_question_1, type_question_2);
                                     scanf("%d", &user_ans);
                                        if (user_ans==correct_ans)
                                           printf("Correct, great job!\n");
                                        else 
                                           printf("Sorry, that's incorrect. The correct answer is %d\n", correct_ans);             
                                 }
                                else if (game_type==MULT){
                                    type_question_1 = 1 + rand()%max_numb; 
                                    type_question_2 = 1 + rand()%max_numb;
                                    correct_ans= type_question_1 * type_question_2;
                                    
                                    printf("What is %d * %d?\n", type_question_1, type_question_2);
                                     scanf("%d", &user_ans);
                                        if (user_ans==correct_ans)
                                           printf("Correct, great job!\n");
                                        else 
                                           printf("Sorry, that's incorrect. The correct answer is %d\n", correct_ans);
                                 }
                   }


    As for the function, it says that I need to return the number (in seconds) the user took to play the entier game, and then divide by the number of problems they solved (I have the scoring sheet to indicate how many points they earned, based on how long it took the user to answer).

    How can I start a timing and end it while the user is answering the questions?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Timing can be done by using the include file time.h.

    You'll want two variables of type "clock_t". I usually call them start and stop.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    void endIt(clock_t start);
    
    int main(void) {
      clock_t start;
      start = clock();    
      sleep(10);
      endIt(start);
      
      printf("\n\n\t\t\t     press enter when ready");
      start = getchar();
      return 0;
    }
      
    void endIt(clock_t start) {
      clock_t stop;
      float duration;
      stop = clock();
      duration = (stop - start)/CLOCKS_PER_SEC; 
      printf("\n\n Time required: %.2f seconds", duration);
    }
    The value "CLOCKS_PER_SEC;" may be different for your compiler. I've seen this as CLK_TCK, or TICKS_PER_SEC, on other compilers. It will be in the header file time.h, and should be mentioned in your help files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use windows API functions in a GTK+ program?
    By Jake.c in forum Windows Programming
    Replies: 19
    Last Post: 01-23-2009, 06:40 AM
  2. Need some help with a beginner functions program!
    By nelledawg in forum C Programming
    Replies: 5
    Last Post: 03-03-2008, 07:05 AM
  3. Starting a new program
    By histevenk in forum C++ Programming
    Replies: 28
    Last Post: 10-15-2007, 04:11 PM
  4. Replies: 10
    Last Post: 02-17-2005, 09:27 PM
  5. Starting the program
    By vasanth in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 04:51 AM