Thread: Need some help with functions.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    25

    Need some help with functions.

    I am trying to make a menu driven program that asks the user if they want to purchase new games, used games, or quit the program. It then will ask how many they would like and then it randomly generates a price for each game. I am having trouble with this:
    /* Pre-conditions: This function takes in a pointer to the variable
    * “total_games”. This variable should keep track of the total number
    * of games purchased.
    *
    * Post-conditions: This function asks the user for how many new games
    * they want to purchase. This function will print a random price
    * between 25.00 and 50.00 dollars for each new game. This function
    * should also keep track of the total cost of new games. This total
    * cost should be returned to main after all new games are purchased.
    */
    double buy_new(int *total_games);


    Not sure what to do with this. Here is my code thus far

    Code:
    #include <stdio.h>
    
    
    void menu();
    
    
    int main () {
        int ans, newgames, usedgames;
    
    
       printf("\n");
       menu();
       scanf("%d", &ans);
    
    
       while (ans != 3) {
    
    
           if (ans == 1) {
            printf("How many new games would you like to buy?\n");
            scanf("%d", &newgames);
           }
           else if (ans == 2) {
            printf("How man used games would you like to buy?\n");
            scanf("%d", usedgames);
           }
    
    
           else if (ans != 3)
            printf("Sorry, invalid option. Please try again.\n");
    
    
            printf("\n");
            menu();
            scanf("%d", &ans);
       }
       return 0;
    }
    
    
    void menu() {
        printf("1.Buy new games.\n");
        printf("2.Buy used games.\n");
        printf("3.Quit.\n");
    }
    Can anyone nudge me in the correct direction?

    Its the random number generator that is hurting me
    Last edited by Jamie Rico; 04-13-2012 at 04:31 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    We have a wonderful FAQ article on just that topic, including a nice, simple example.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    I looked through the C tuts and I could not find one for random number generator. I saw it under c++, is it the same?

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Even after reading the C++ tut, I am still unsure of how to do this

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Not the tutorial, the FAQ. Here: FAQ > Generate random numbers? - Cprogramming.com.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Ahh thank you! Now I just put that into the function I a creating?

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    I am still a bit unsure on how to build this function correctly.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Unsure about what? Why not give it your best effort, and if you're still stuck, post back your attempt and a more specific question, then I can give you some better help.

    Your textbook, class notes, and Google should turn up some basic tutorials and examples. We also have a quick primer on functions in our C tutorial section.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Well I read the FAQ about random numbers and my textbook about random numbers. I understand how to make it a random number between 0 and a number, but I don't know how to increase the minimum still. I've been reading the chapter in my book about functions and pointers for the last hour or so and class notes, but just not understanding it. I know how they work because I built the menu function, just the concept of declaring this one before main and using it has got me stumped.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void menu();
    void buy_new (*total_games, newgames);
    
    
    int main () {
        int ans, newgames, usedgames, total_games;
        total_games = newgames + usedgames;
        
    
    
       printf("\n");
       menu();
       scanf("%d", &ans);
    
    
       while (ans != 3) {
    
    
           if (ans == 1) {
            printf("How many new games would you like to buy?\n");
            scanf("%d", &newgames);
           }
           else if (ans == 2) {
            printf("How man used games would you like to buy?\n");
            scanf("%d", usedgames);
           }
    
    
           else if (ans != 3)
            printf("Sorry, invalid option. Please try again.\n");
    
    
            printf("\n");
            menu();
            scanf("%d", &ans);
       }
       return 0;
    }
    
    
    void menu() {
        printf("1.Buy new games.\n");
        printf("2.Buy used games.\n");
        printf("3.Quit.\n");
    }
    
    
    void buy_new () {
        printf("How many new games would you like to buy?\n");
        scanf("%d", &newgames);
        
    
    
    }
    Last edited by Jamie Rico; 04-13-2012 at 06:21 PM.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    I have to do exactly the same thing for used games, so once I understand how to do it for new I will be set.
    I'm not looking for an answer, just a nudge in the right direction so I can figure it out myself and learn how to do it

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Just to try it basically I did this, but it is returning that buy_new is not a function
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void menu();
    void buy_new(double* ptrnewgames);
    
    
    int main () {
        int ans, newgames, usedgames, total_games, buy_new;
        total_games = newgames + usedgames;
    
    
    
    
       printf("\n");
       menu();
       scanf("%d", &ans);
    
    
       while (ans != 3) {
    
    
           if (ans == 1) {
            buy_new();
           }
           else if (ans == 2) {
            printf("How man used games would you like to buy?\n");
            scanf("%d", usedgames);
           }
    
    
           else if (ans != 3)
            printf("Sorry, invalid option. Please try again.\n");
    
    
            printf("\n");
            menu();
            scanf("%d", &ans);
       }
       return 0;
    }
    
    
    void menu() {
        printf("1.Buy new games.\n");
        printf("2.Buy used games.\n");
        printf("3.Quit.\n");
    }
    
    
    void buy_new (double* ptrnewgames) {
        int newgames;
        printf("How many new games would you like to buy?\n");
        scanf("%d", &newgames);
    
    
    
    
    }

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Jamie Rico View Post
    Well I read the FAQ about random numbers and my textbook about random numbers. I understand how to make it a random number between 0 and a number, but I don't know how to increase the minimum still. I've been reading the chapter in my book about functions and pointers for the last hour or so and class notes, but just not understanding it. I know how they work because I built the menu function, just the concept of declaring this one before main and using it has got me stumped.
    To "increase the minimum", you just need to add to whatever you have. So if I want to generate random numbers between, say, 3 and 10, I can generate random numbers between 0 and 7, and just add 3 to whatever random numbers I generate. Something like:
    Code:
    x = rand() % (10 - 3 + 1) + 3
    So apply that to your situation. One thing to remember, rand() produces integers, but you need doubles, to contain the fractional part (pennies). So generate your random numbers for how many pennies you want, i.e. between 2500 and 5000, then just divide by 100.0 to get dollars. Note, I left it as (10 - 3 + 1). I could have just used % 7, but that only works if your range is fixed. If the min and max for your range are variables (called, say, high and low), you want rand() % (high - low + 1) + low.

    You were given the declaration of buy_new, which is
    Code:
    double buy_new(int *total_games)
    So you know you need that as a prototype at the top (put it on a line by itself with a semicolon after it, just like the menu function). You also know that the buy_new function you wrote in post 10 is wrong, since it doesn't match the requirements, so fix that to match (no semicolon after that one).

    You were also told that you have to generate a random price for each game they want, so you need a loop that runs newgames times. Put your code to generate a price in that loop.

    Is that enough of a nudge for now?

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    I think so, I'll give it a try and post my results.
    Thank you

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    This is what I came up with, I am still being told buy_new is not a function. Also, did I do my loop right at the bottom. For loops have been an issue for me
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void menu();
    double buy_new(int *total_games);
    
    
    int main () {
        int ans, newgames, usedgames, total_games, buy_new, i;
        total_games = newgames + usedgames;
        srand(time(0));
    
    
    
    
       printf("\n");
       menu();
       scanf("%d", &ans);
    
    
       while (ans != 3) {
    
    
           if (ans == 1) {
    
    
            buy_new(int *total_games);
           }
           else if (ans == 2) {
            printf("How man used games would you like to buy?\n");
            scanf("%d", usedgames);
           }
    
    
           else if (ans != 3)
            printf("Sorry, invalid option. Please try again.\n");
    
    
            printf("\n");
            menu();
            scanf("%d", &ans);
       }
       return 0;
    }
    
    
    void menu() {
        printf("1.Buy new games.\n");
        printf("2.Buy used games.\n");
        printf("3.Quit.\n");
    }
    
    
    double buy_new (int *total_games) {
        int newgames, price, i;
        printf("How many new games would you like to buy?\n");
        scanf("%d", &newgames);
        for (i=0; i<newgames; i++)
        {
            price = rand() % ((5000-2500+1) + 2500) / 100;
        }
    
    
    
    
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM