Confused myself more than ever

This is a discussion on Confused myself more than ever within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm in the process of trying to code a program that catalogues baseball cards. The information for the ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Unhappy Confused myself more than ever

    Hello everyone, I'm in the process of trying to code a program that catalogues baseball cards. The information for the cards is populated through user input. After 10 cards have been populated the user has the option to print the cards to the console, or quit. I've confused myself more than ever on this one now and I can't seem to get the populate function working correctly. I'm not looking for a solution, but more for someone to steer me in the right direction.
    Code:
    #include"stdafx.h"
    #include<stdlib.h>
    #include<stdio.h>
    typedefstruct{
    
    char lName[20], fName[20], bats, throws, team[30];
    
    int weight, height, year, games, runs, hits, RBI;
    
    double BA;
    
    }Player;
    
    
    Player p;
    
    
    void populatePlayer (Player p);
    
    void askPrint (Player p);
    
    void printPlayer (Player p);
    
    
    int_tmain(intargc, _TCHAR* argv[])
    
    {
    
        populatePlayer(p);
    
    }
    
    
    void populatePlayer(Playerp){
    
    
        printf("Enter last name: ");
    
        scanf_s("%c", &p.lName);
    
        printf("Which hand does player bat? ");
    
        scanf_s("%c", &p.bats);
    
    
        printPlayer(p);
    
    
    }
    
    
    /*void askPrint(Player p){
    
        char choice;
    
    
        printf("Print data? (y/n) ");
    
        scanf_s("%c", &choice);
    
    
        switch(choice)
    
        {
    
            case 'y':
    
                printPlayer(p);
    
            case 'n':
    
                break;
    
        }
    
    }*/
    
    
    void printPlayer(Playerp){
    
    
        printf("last name is %s\n", p.lName);
    
    
    if(p.bats == 'l')
    
            printf("player bats lefty\n");
    
    elseif (p.bats == 'r')
    
            printf("player bats righty\n");
    
    else
    
            printf("player uses some odd hand\n");
    
    }
    
    I appreciate any tips or guidance. Thanks in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    You need an array of your card objects (struct Player?) to accommodate that. Then you need a loop to call the populate function 10 times, once for each card you have. The populate function needs to accept a pointer to a card object otherwise the function will just be operating on a copy of the passed in structure and any changes made locally to the object within the function will only affect the values in the object within said function and not back in the calling function. Your ask code will need to loop through and call the print function on each item in the array to print all the cards. For this to happen, the ask function will need to be passed the entire array from main and not just a single item. Either that, or the print function itself can do the looping over the array if you pass it (the array received from main) through the ask function and into the print function.

    Globals should be avoided where possible, create the array of 10 objects in main function and pass that into the functions as appropriate.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    hk, I believe that clears things up a lot! I'm going to lunch at the moment and I'll give it a shot when I return. thanks for the quick reply

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,194
    Code:
    printf("last name is %s\n", p.lName);  // ...
    if(p.bats == 'l')
    You seem to be mixing up char and char*. In your scanf_s lines you are using %c when I think it would be better to use %s. I personally don't think it would hurt to do the same for bats:

    Code:
    if (p.bats[0] == 'l')
    Also scanf_s (at least as defined by Microsoft) requires you to specify the size of each character and string arguments, even for "%c" types, and obviously for %s types:

    Code:
    scanf_s("%s", p.bats, 12345);
    Replace 12345 with the size of the p.bats variable, or 1 if you leave it a char.

    See scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l (CRT)
    Last edited by c99tutorial; 12-07-2012 at 12:03 PM.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Question

    Is this looking any better?
    Code:
    #include"stdafx.h"
    
    #include<stdlib.h>
    
    #include<stdio.h>
    
    
    typedefstruct{
    
    char *lName[20], *fName[20], *bats, *throws, *team[30];
    
    int weight, height, year, games, runs, hits, RBI;
    
    double BA;
    
    }Player;
    
    
    Player populatePlayer(Playerp[]){
    
    
    for(int i = 0; i < 10; i++){
    
            printf("Enter last name: ");
    
    for(int j = 0; j < 20; j++){
    
                scanf_s("%s", &p[i].lName[j]);
    
            }
    
            printf("Which hand does player bat? ");
    
            scanf_s("%s", &p[i].bats[0]);
    
        }
    
    }
    
    
    void printPlayer(Playerp[]){
    
    
    for(int i = 0; i < 10; i++){
    
    for(int j = 0; j < 20; j++){
    
                printf("last name is %s\n", p[i].lName[j]);
    
            }
    
    if(p[i].bats[0] == 'l')
    
                printf("Player bats lefty\n");
    
    elseif (p[i].bats[0] == 'r')
    
                printf("Player bats righty\n");
    
    else
    
                printf("Player uses some odd hand\n");
    
        }
    
    }
    
    
    void askPrint(Playerp[]){
    
    char choice;
    
    
        printf("Print data? (y/n) ");
    
        scanf_s("%c", &choice);
    
    
    switch(choice)
    
        {
    
    case'y':
    
                printPlayer(p);
    
    case'n':
    
    break;
    
        }
    
    }
    
    
    int_tmain(intargc, _TCHAR* argv[])
    
    {
    
    Player p[10];
    
        
    
        populatePlayer(p);
    
        askPrint(p);
    
    }
    
    
    Also, what should populatePlayer be returning? if I try to return p, it throws an error. In the state the code is in the program crashes when I run it. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very Confused about this....
    By Matty_Alan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2010, 08:28 PM
  2. Really confused. ):
    By Rawr in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 06:44 PM
  3. I'm confused!
    By Newbeee in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2006, 01:59 AM
  4. confused with cin.get()
    By Shal in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2006, 07:27 AM
  5. Very confused
    By Aerie in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21