beginner function problem

This is a discussion on beginner function problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a student who has just started using C++. I have a program that works but I have ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    beginner function problem

    Hi,
    I am a student who has just started using C++. I have a program that works but I have to take parts of the code and turn them into functions. I really have no idea how to do this.
    Here is an example of what I am trying to do. I want to make a void function to read in some info from a file because I have to read this same info in a few times within the program.

    this is the code I want to put in a function:

    getline(In, CustName);
    In >> AccountNum; In >> Date; In >> SName;
    In >> Shares;

    Any help would be appreciated.
    Thanks!

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Really simple...

    void getFromFile(){
    getline(In, CustName);
    In >> AccountNum; In >> Date; In >> SName;
    In >> Shares;

    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Here, hope this wil help :

    Code:
    #include <iostream.h>
    
    
    void getData()
    {
       char custName[21], sName[21], date[9];
       int shares, accountNum;
    
       cout << "Please enter the customer name : ";
       cin >> custName;
       cout << endl;
       cout << "Please enter the customer account number : ";
       cin >> accountNum;
       cout << endl;
       cout << "Please enter the date : (yyy/mm/dd)";
       cin >> date;
       cout << endl;
       cout << "Please enter the stock name : ";
       cin >> sName;
       cout << endl;
       cout << "Please enter the customer shares : ";
       cin >> shares;
       cout << endl;
    }
    
    int main()
    {
       getData();
       return 0;
    }
    Last edited by biosninja; 10-30-2002 at 06:47 PM.

  4. #4
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    getline(In, CustName);
    In >> AccountNum; In >> Date; In >> SName;
    In >> Shares;
    Okay, lets see what you have...
    getline is the name of the function you want to write?
    And it's arguments are In and CustName?
    I assume In is an input stream from the way you are using it.
    What do you want to do with CustName?

    It looks to me like you want a few different customers, each with their own name, and then get details about whatever one you give the name for.

    Give me more details and I can help more...
    - Well that's my 4c Australian.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    I am using getline to read the Customer Name from an input file. And then I am using In to read the rest of the data. I just want to read Customer Name, Account Number, Stock Name, and Stock Shares from a file and output them.
    I have a loop later in my program that needs to read this info in again and I am supposed to write a function for this input.

  6. #6
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    Ah okay.
    OneStiffRod's example is the most accurate.
    functions are easy, you just name a function then put the code you want inside { }
    - Well that's my 4c Australian.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    I did that but now I get an error that 'In' is an undeclared identifier.

  8. #8
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    In is your input stream?
    Yeah sorry I forgot, you need to pass into your function anything you want to use, so In needs to be passed in, and the variables you want to fill with data have to be passed in by reference so that they can be filled and returned
    Code:
    void getFromFile(ifstream In, string &CustName, string &AccountNum, string &Date, string &SName, string &Shares)
    {
     getline(In, CustName); 
     In >> AccountNum; 
     In >> Date; 
     In >> SName; 
     In >> Shares; 
    }
    Call it with getFromFile(In, CustName, AccountNum, Date, SName, Shares) inside your loop in the main program.
    - Well that's my 4c Australian.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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