help with struct program

This is a discussion on help with struct program within the C++ Programming forums, part of the General Programming Boards category; i made this program to have the user choose from a menu what they want to do then enter information ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    help with struct program

    i made this program to have the user choose from a menu what they want to do then enter information about a baseball card and view the data if they want but it doesnt work right and I cant figure out whats wrong

    #include <iostream.h>


    struct BaseballType
    {
    string name;
    string name2;
    int year;
    string company;
    int number;
    double price;


    };
    int Entry(BaseballType baseball);
    void Display(BaseballType baseball);

    int main()

    {

    int choice;

    BaseballType baseball;

    cout<<"Press the number of you choice"<<endl;
    cout<<"1) Enter card information"<<endl;
    cout<<"2)Print card data"<<endl;
    cout<<"3)Exit program"<<endl;
    cin>>choice;

    if(choice==1)
    Entry(BaseballType &baseball);
    else if(choice==2)
    void Display(BaseballType baseball);
    else
    cout<<"End of Program";




    }

    int Entry(BaseballType &baseball)
    {
    cout<<"Enter player name"<<endl;
    cin>>baseball.name>>baseball.name2;
    cout<<"Enter year of card"<<endl;
    cin>>baseball.year;
    cout<<"Enter company"<<endl;
    cin>>baseball.company;
    cout<<"Enter the card number"<<endl;
    cin>>baseball.number;
    cout<<"Enter the price of the card"<<endl;
    cout<<"$";
    cin>>baseball.price;



    }

    void Display(BaseballType baseball)
    {

    cout<<baseball.name<<" "<<baseball.name2<<endl;
    cout<<baseball.year<<endl;
    cout<<baseball.company<<endl;
    cout<<baseball.number<<endl;
    cout<<baseball.price<<endl;



    }
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    There are a few things wrong. First of all you don't need to declare the function return type when you call it. Take out the void before Display. Also, you don't need to declare your struct baseball everytime you pass it to a function. Take out the BaseballType in both of your funtion calls in main(). Also in your funtion declaration and prototype for Entry you need it to be BaseballType *baseball, then make the function call with the & operator before baseball. That is all the errors I see. Good luck!

    EDIT: You need to call the structure members with a * before baseball in the Entry function.
    Last edited by Crossbow; 01-20-2002 at 09:23 PM.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    i think i might have found ur problem....

    int main is a function. therefore, when other functions are called, the values in main go out of scope. therfore, the baseball instance of baseball type does not exist when u call the set function. on the other hand, if u used a pointer baseballtype, ur program should work.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    i changed the program to this but it still wont work

    #include <iostream.h>


    struct BaseballType *baseball
    {
    string name;
    string name2;
    int year;
    string company;
    int number;
    double price;


    };
    int Entry(BaseballType *baseball);
    void Display(BaseballType *baseball);

    int main()

    {

    int choice;

    //BaseballType baseball;

    cout<<"Press the number of you choice"<<endl;
    cout<<"1) Enter card information"<<endl;
    cout<<"2)Print card data"<<endl;
    cout<<"3)Exit program"<<endl;
    cin>>choice;

    if(choice==1)
    Entry(&baseball);
    else if(choice==2)
    Display(&baseball);
    else
    cout<<"End of Program";




    }

    int Entry(BaseballType *baseball)
    {
    cout<<"Enter player name"<<endl;
    cin>>baseball.name>>baseball.name2;
    cout<<"Enter year of card"<<endl;
    cin>>baseball.year;
    cout<<"Enter company"<<endl;
    cin>>baseball.company;
    cout<<"Enter the card number"<<endl;
    cin>>baseball.number;
    cout<<"Enter the price of the card"<<endl;
    cout<<"$";
    cin>>baseball.price;



    }

    void Display(BaseballType *baseball)
    {

    cout<<baseball.name<<" "<<baseball.name2<<endl;
    cout<<baseball.year<<endl;
    cout<<baseball.company<<endl;
    cout<<baseball.number<<endl;
    cout<<baseball.price<<endl;



    }
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    some more changes

    okay, i basically rewote it and pointed out the changes

    #include <iostream.h>

    struct BaseballType <-- I took out the asterik and the name
    {
    string name;
    string name2;
    int year;
    string company;
    int number;
    double price;
    };
    int Entry(BaseballType &baseball); <-- Change the asterik to a &
    void Display(BaseballType &baseball); <-- Change the asterik to a &

    int main()

    {

    int choice;

    BaseballType baseball; <-- Comment this back in

    cout<<"Press the number of you choice"<<endl;
    cout<<"1) Enter card information"<<endl;
    cout<<"2)Print card data"<<endl;
    cout<<"3)Exit program"<<endl;
    cin>>choice;

    if(choice==1)
    Entry(&baseball);
    else if(choice==2)
    Display(&baseball);
    else
    cout<<"End of Program";




    }

    int Entry(BaseballType &baseball) <-- Change the asterik to a &

    {
    cout<<"Enter player name"<<endl;
    cin>>baseball.name>>baseball.name2;
    cout<<"Enter year of card"<<endl;
    cin>>baseball.year;
    cout<<"Enter company"<<endl;
    cin>>baseball.company;
    cout<<"Enter the card number"<<endl;
    cin>>baseball.number;
    cout<<"Enter the price of the card"<<endl;
    cout<<"$";
    cin>>baseball.price;



    }

    void Display(BaseballType &baseball) <-- Change the asterik to a &

    {

    cout<<baseball.name<<" "<<baseball.name2<<endl;
    cout<<baseball.year<<endl;
    cout<<baseball.company<<endl;
    cout<<baseball.number<<endl;
    cout<<baseball.price<<endl;



    }

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    wrap your code in [ code ] tags

    Code:
    #include <iostream.h> 
    
    typedef struct _BaseballType
    { 
    	string name; 
    	string name2; 
    	int year; 
    	string company; 
    	int number; 
    	double price; 
    } BaseballType; 
    
    int Entry(BaseballType &baseball);
    void Display(BaseballType &baseball);
    
    int main(void) 
    { 
    	int choice; 
    
    	BaseballType baseball;
    
    	cout << "Press the number of you choice\n"; 
    	cout << "1) Enter card information\n"; 
    	cout << "2)Print card data\n"; 
    	cout << "3)Exit program\n"; 
    	cin >> choice; 
    
    	if(choice == 1) 
    	{
    		Entry(baseball); 
    	}
    	else if(choice == 2) 
    	{
    		Display(baseball); 
    	}
    	else 
    	{
    		cout << "End of Program"; 
    	}
    
    	return(0);
    } 
    
    int Entry(BaseballType &baseball)
    { 
    	cout << "Enter player name\n"; 
    	cin >> baseball.name >> baseball.name2; 
    	cout << "Enter year of card\n"; 
    	cin >> baseball.year; 
    	cout << "Enter company\n"; 
    	cin >> baseball.company; 
    	cout << "Enter the card number\n"; 
    	cin >> baseball.number; 
    	cout << "Enter the price of the card\n$; 
    	cin >> baseball.price; 
    
    	return(0);
    } 
    
    void Display(BaseballType &baseball)
    { 
    	cout << baseball.name << " " << baseball.name2 << endl; 
    	cout << baseball.year << endl; 
    	cout << baseball.company << endl; 
    	cout << baseball.number << endl; 
    	cout << baseball.price << endl; 
    }
    edit: i haven't compiled this code... but i'm sure it's fine.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Unregistered
    Guest

    string

    C++ does not recognize string as a type....

    To create a string you must use an array of type Char


    so instead of

    string name1;

    use

    char name1[15];

    This will allocate an array of 15 characters with the name name1
    from there you can call *name1 this will be the local pointer to name1[0] wich is the first character in your string

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    When talking about using a pointer, I was trying to tell u to declare the instance of baseballtype as a pointer on line 2 inside main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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