Follow us on twitter

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.


    • CommentAuthorbogcess
    • CommentTimeNov 1st 2009
     
    i hv homework....

    Write a program to define a structure with 5 records named CustomerRecord which contains name, account_number and balance. You should create 2 functions to read and print the content of the structure. Use arrays to save the 5 records.


    and i try to answer...

    #include <iostream>
    using namespace std;

    struct CustomerRecord
    {
    string name;
    int account_number;
    double balance;
    }a[5];

    void read(CustomerRecord);
    void print(CustomerRecord);
    int main ()
    {
    CustomerRecord a[5];
    read(a[5]);
    print(a[5]);
    system("pause");
    return 0;
    }
    void read(CustomerRecord a[])
    {
    for (int i=0; i<5; i++)
    {
    cout << "Name : ";
    getline(cin, a[i].name);
    cout << "Account Number : ";
    cin >> a[i].account_number;
    cout << "Balance : ";
    cin >> a[i].balance;
    }
    }
    void print(CustomerRecord a[])
    {
    for (int i=0; i<5; i++)
    {
    cout << "Name : " << a[i].name << endl;
    cout << "Account Number : " << a[i].account_number << endl;
    cout << "Balance : " << a[i].balance <<endl;
    }
    }



    but when i compile...
    always appear " [Linker error] undefined reference to `read(CustomerRecord)' "


    can u help me to solve my program??
    • CommentAuthorbogcess
    • CommentTimeNov 2nd 2009 edited
     

    For first try use this:
    Code:
    // xxxx.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;


    struct CustomerRecord
       {
       string name;
       int account_number;
       double balance;
       }a[2];

    void read_y(CustomerRecord*);
    void print_y(CustomerRecord*);



    int main(int argc, _TCHAR* argv[])
       {
          int c;
          read_y(a);
          print_y(a);

          cin >> c;
          return 0;
       }


    void read_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : ";
          cin >> a[i].name;
          cout << "Account Number : ";
          cin >> a[i].account_number;
          cout << "Balance : ";
          cin >> a[i].balance;
          }
       }

    void print_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : " << a[i].name << endl;
          cout << "Account Number : " << a[i].account_number << endl;
          cout << "Balance : " << a[i].balance <<endl;
          }
       }


    At least this works...



    what's with you and _TCHAR? that's not a type you should be using.

    and the error in the original program is that the 2 functions do not match signatures

    CustomerRecord is not the same as CustomerRecord[]

    • CommentAuthorCrazyScott
    • CommentTimeNov 2nd 2009 edited
     

    For first try use this:

    Code:
    // xxxx.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;


    struct CustomerRecord
       {
       string name;
       int account_number;
       double balance;
       }a[2];

    void read_y(CustomerRecord*);
    void print_y(CustomerRecord*);



    int main(int argc, _TCHAR* argv[])
       {
          int c;
          read_y(a);
          print_y(a);

          cin >> c;
          return 0;
       }


    void read_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : ";
          cin >> a[i].name;
          cout << "Account Number : ";
          cin >> a[i].account_number;
          cout << "Balance : ";
          cin >> a[i].balance;
          }
       }

    void print_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : " << a[i].name << endl;
          cout << "Account Number : " << a[i].account_number << endl;
          cout << "Balance : " << a[i].balance <<endl;
          }
       }



    At least this works...

    • CommentAuthorBenny6890
    • CommentTimeNov 2nd 2009 edited
     

     

    what's with you and _TCHAR? that's not a type you should be using.

     

    It could be some sort of predefined constant? Though I agree, it doesn't look like anything that I've come across before.

    • CommentAuthorCrazyScott
    • CommentTimeNov 3rd 2009 edited
     

     

     
    For first try use this:
    Code:
    // xxxx.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;


    struct CustomerRecord
       {
       string name;
       int account_number;
       double balance;
       }a[2];

    void read_y(CustomerRecord*);
    void print_y(CustomerRecord*);



    int main(int argc, _TCHAR* argv[])
       {
          int c;
          read_y(a);
          print_y(a);

          cin >> c;
          return 0;
       }


    void read_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : ";
          cin >> a[i].name;
          cout << "Account Number : ";
          cin >> a[i].account_number;
          cout << "Balance : ";
          cin >> a[i].balance;
          }
       }

    void print_y(CustomerRecord* a)
       {
       for (int i=0; i<2; i++)
          {
          cout << "Name : " << a[i].name << endl;
          cout << "Account Number : " << a[i].account_number << endl;
          cout << "Balance : " << a[i].balance <<endl;
          }
       }


    At least this works...


    what's with you and _TCHAR? that's not a type you should be using.

    and the error in the original program is that the 2 functions do not match signatures

    CustomerRecord is not the same as CustomerRecord[]

     



    Hmmm, these things gets automatically generated by Visual Studio...? Why shouldnt that be used? If there some issue in using TCHAR please tell me.
    I know, I altered the solution a bit....but for me it works, I compiled it and so I didnt check it any further. It should only be an very little alternative.

    You can replace *a by a[]...wheres the problem (both dereferences the field a)? Also the name is not important (declaration and definition are in accordance).

    ADD: Ah, one thing: I know that you dont have to use the paramters in main. But I think its helpful to have access to the parameters you start the *.exe with. I usually do not mention this... 

    TCHAR mean CHAR for ANSI and WCHAR for Unicode.

  1.  
    OK, thnx!
    • CommentAuthorbogcess
    • CommentTimeJan 28th 2010 edited
     

     

    Hmmm, these things gets automatically generated by Visual Studio...? Why shouldnt that be used? If there some issue in using TCHAR please tell me.

     

    it's not a standard type, and even so you should be using TCHAR not _TCHAR