Thread: Struct into function

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

    Struct into function

    Ive a struct TypeData, that is called sTicket, how do i pass this struct into my function, PrintTicket()

    Any help appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    You pass it like a normal variable ie

    void PrintTicket (TypeData sTicket)
    {
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    /me goes away with tail between legs

    Cheers

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Ooooh just tried that im still getting errors. Ill paste in my prototype, definition and function call and see if you can spot something.

    Prototype:

    void PrintaTicket(struct TypeData sTicket);

    Function Call:

    PrintaTicket(TypeData sTicket);

    Definition:

    void PrintaTicket(struct TypeData sTicket)
    {
    int iReference;
    int iCustomer;

    cout<<"Please type in your reference number";
    cin>>iReference;
    clrscr();
    cout<<sTicket.sNameArray[iReference]<<endl;
    cout<<"Destination - "<<sTicket.sDestination[iReference]<<endl;
    cout<<sTicket.sNumber[iReference]<<" "<<sTicket.sStreetName[iReference]<<endl;
    cout<<sTicket.sCity[iReference]<<endl;
    cout<<sTicket.sRegion[iReference]<<endl;
    cout<<"Please enter your customer number";
    cin>>>iCustomer;
    }



    i get these errors

    Error: PROJECTb.CPP(211,22):Improper use of typedef 'TypeData'
    Error: PROJECTb.CPP(211,30):Function call missing )

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    Prototype should be:

    void PrintaTicket(TypeData);

    Function Call should be:

    PrintaTicket(sTicket);

    and Function Definition should be:

    void PrintaTicket(TypeData sTicket)
    {
    //your stuff here
    }


    cheers

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Im getting a crazy error about expecting a {

    Error: project.cpp(11,24):{ expected

    thats in my prototype

    and this

    Error: project.cpp(211,13):Call to undefined function 'PrintTicket'


    ////////////////////////////////////////////////////////////////////////////////
    //C++ Coursework Assesment
    //Program Created by Ryan Pincher
    //Date 27/04/02
    ////////////////////////////////////////////////////////////////////////////////
    #include <iostream.h>
    #include <cstring.h>
    #include <conio.h>
    ////////////////////////////////////////////////////////////////////////////////
    //Function Prototypes
    void PrintTicket(TypeData);
    ////////////////////////////////////////////////////////////////////////////////
    struct DataType
    {
    int iPrice;
    int iDays;
    int iNumber;
    int iYoung;
    };
    struct TypeData
    {
    string sNumber[25];
    string sStreetName[25];
    string sCity[25];
    string sRegion[25];
    string sNameArray[125];
    string sDestination[25];
    };

    int main()
    {

    DataType iFlight;
    TypeData sTicket;

    int iDestination;
    string sDestination;
    int iStop;
    int iChoice=0;
    int iDate;
    int iChoose;
    int iPrice;
    float fPCost;
    float fCost;
    float fTax=5.5;
    char sCheck;
    int iTotal;
    int iCount=2;
    int iArrayCount=0;
    char sFlight='y';
    char sTicketPrint;

    while (sFlight=='y')
    {
    iArrayCount++;

    while (iChoice !=1)
    {

    cout<<"Where are you travelling to?"<<endl;
    cout<<"1-Aberdeen"<<endl;
    cout<<"2-Chicago"<<endl;
    cout<<"3-Paris"<<endl;
    cout<<"4-Tokyo"<<endl;
    cout<<"5-Tenerife"<<endl;

    cin>>iDestination;


    switch(iDestination)
    {
    case 1: cout<< "You selected Aberdeen"<<endl;
    sDestination="Aberdeen";
    iChoice=1;
    break;

    case 2: cout<< "You selected Chicago"<<endl;
    sDestination="Chicago";
    iChoice=1;
    break;

    case 3: cout<<"You selected Paris"<<endl;
    sDestination= "Paris";
    iChoice=1;
    break;

    case 4: cout<<"You selected Tokyo"<<endl;
    sDestination= "Tokyo";
    iChoice=1;
    break;

    case 5: cout<<"You selected Tenerife"<<endl;
    sDestination="Tenerife";
    iChoice=1;
    break;

    default: cout<< "Please make another choice"<<endl;
    }

    }

    while (iChoose != 1)
    {
    cout<<"When do you want to travel?"<<endl;
    cout<<"1 Day - Press 1"<<endl;
    cout<<"2 Days - Press 2"<<endl;
    cout<<"3 Days - Press 3"<<endl;
    cout<<"4 Days - Press 4"<<endl;
    cout<<"5 Days - Press 5"<<endl;

    cin>>iDate;



    switch(iDate)
    {
    case 1: cout<<"You chose to travel in 1 day, the charge for this will be £9 per person"<<endl;
    iFlight.iDays=1;
    iChoose=1;
    iFlight.iPrice=9;
    break;

    case 2: cout<<"You chose to travel in 2 days, the charge for this will be £29 per person"<<endl;
    iFlight.iDays=2;
    iChoose=1;
    iFlight.iPrice=29;
    break;

    case 3: cout<<"You chose to travel in 3 days, the charge for this will be £49 per person"<<endl;
    iFlight.iDays=3;
    iChoose=1;
    iFlight.iPrice=49;
    break;

    case 4: cout<<"You chose to travel in 4 days, the charge for this will be £69 per person"<<endl;
    iFlight.iDays=4;
    iChoose=1;
    iFlight.iPrice=69;
    break;

    case 5: cout<<"You chose to travel in 5 days, the charge for this will be £99 per person"<<endl;
    iFlight.iDays=5;
    iChoose=1;
    iFlight.iPrice=99;
    break;

    default: cout<<"Please choose again, you entered incorrectly."<<endl;
    break;
    }


    }

    cout<<" "<<endl;

    cout<<"How many paying customers are there?(Including children over 12 months)"<<endl;
    cin>>iFlight.iNumber;
    cout<<" "<<endl;

    cout<<"How many children under the age of 12 months are travelling"<<endl;
    cin>>iFlight.iYoung;
    cout<<" "<<endl;

    iTotal=iFlight.iNumber+iFlight.iYoung;
    cout<<iTotal<<endl;
    cout<<"You will travel to "<<sDestination<<endl;
    cout<<"You will be travelling in "<<iFlight.iDays<<" days time"<<endl;
    cout<<"There will be "<<iFlight.iNumber<<" paying travellers and "<<iFlight.iYoung<<" children"<<endl;

    fPCost=iFlight.iNumber*iFlight.iPrice;
    fCost=(iFlight.iNumber*fTax)+(iFlight.iYoung*fTax) +(fPCost);

    cout<<"The price pre-tax will be "<<fPCost<<" Pounds"<<endl;
    cout<<"After tax it will be "<<fCost<<" Pounds"<<endl;

    cout<<"Are these prices and details satisfactory?, Press y if yes, any other key for no";
    cin>>sCheck;

    if (sCheck=='y')
    {
    cout<<"Enter your address"<<endl;
    cout<<"What is your House Number?"<<endl;
    cin>>sTicket.sNumber[iArrayCount];
    cout<<"Whats your street name?"<<endl;
    cin>>sTicket.sStreetName[iArrayCount];
    cout<<"City?"<<endl;
    cin>>sTicket.sCity[iArrayCount];
    cout<<"Region?"<<endl;
    cin>>sTicket.sRegion[iArrayCount];
    cout<<"What is the name of the person paying the bill/main person in party?"<<endl;
    cin>>sTicket.sNameArray[iArrayCount];
    clrscr();
    cout<<"YOUR REFERENCE NUMBER IS "<<iArrayCount<<" WRITE THIS DOWN NOW!"<<endl;
    sTicket.sDestination[iArrayCount]=sDestination;
    while (iCount<=iTotal)
    {
    cout<<"What is the next passengers name?";
    cin>>sTicket.sNameArray[iCount];
    cout<<"This customer's number is "<<iCount<<endl;
    iCount++;
    }
    }
    else
    {
    cout<<"Go back to the start";
    }
    cout<<"Do you want to print a ticket?";
    cin>>sTicketPrint;
    if (sTicketPrint=='y')
    {
    PrintTicket(sTicket);
    }
    cout<<"Do you want to book another flight?";
    cin>>sFlight;
    iChoice=0;
    iChoose=0;
    }

    }//End Main
    /////////////////////////////////////////////////////////////////////////////////
    void PrintTicket(TypeData sTicket)
    {
    int iReference;
    int iCustomer;

    cout<<"Please type in your reference number";
    cin>>iReference;
    clrscr();
    cout<<sTicket.sNameArray[iReference]<<endl;
    cout<<"Destination - "<<sTicket.sDestination[iReference]<<endl;
    cout<<sTicket.sNumber[iReference]<<" "<<sTicket.sStreetName[iReference]<<endl;
    cout<<sTicket.sCity[iReference]<<endl;
    cout<<sTicket.sRegion[iReference]<<endl;
    cout<<"Please enter your customer number";
    cin>>iCustomer;
    }


    sorry to keep bothering but the program is sort of paramount to this working.

    source file attached if you feel like helping alot.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    look at the this code (copied from the above message but with some slight changes *stated in code* ) and btw next time use code tags.

    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    I cannot thank you enough, and by code tags do you mean commentated code?

    Thanks again......
    .....and again.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    [ code ] [ /code ] (with no spaces) when you post

    ie

    Code:
    //This is a code fragment
    if (toupper(key)=='K')
      cout <<"Going to continue";
    else
      cout <<"Not going to continue";
    //notice how the spaces are kept
    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM