Problem

This is a discussion on Problem within the C++ Programming forums, part of the General Programming Boards category; can someone tell me what is wrong: Code: void input(char& *first,char& *last,double& winning_amount) { cout <<"What is your name( first ...

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

    Problem

    can someone tell me what is wrong:

    Code:
    void input(char& *first,char& *last,double& winning_amount)
    {
    
       cout <<"What is your name( first last): " ;
       cin >>first;
       cin >> last;
       cout <<"How much won in contest: ";
       cin >> winning_amount;
       center("Press Any Key To Continue...",13);
    
       getch();
       clrscr();
    }
    
    int main(void)
    {
    
       char *first, *last;
       char name[80];
       double winning_amount;
    
    
    
       input(first,last,winning_amount);
       strcpy(name, first);
       strcat(name," ");
       strcat(name,last);
    
       cout << winning_amount << endl;
       cout <<name;
       getch();
    
    }
    I have not idea why this isnt working. All help is greatly appreciated. Oh ya, im using Borland C++ 5.02

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >char& *first
    this is saying you want a pointer to a char reference
    what you need is 'char *&first'.

    Also, you are not allocating memory for char *first in your main and you try store info in it in your input function (cin >> first)
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    could you post the editted code please, i'm a beginner so a really need to see what the code looks like

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    allocating memory for char *first in your main
    how would i do this

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >void input(char& *first,char& *last,double& winning_amount)
    void input (char* &first, char* &last, double &winning_amount)

    >char *first, *last;
    char first[80], char last[80];

    or

    first = new char[80];
    last = new char[80];

    in this case, you dont even need to pass first and last by reference to input
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    void input(char * first, char * last, double  & winningNumber);
    
    int main()
    {
       char first[80];
       char last[80];
       double winningNumber;
     
       input(first, second, winningNumber);
    
      //etc.
    This should work just fine. Passing by reference can be done either by using a reference as a paramenter or by using a pointer as a parameter. An arrays name is (for intents and purposes) a pointer, so it works just fine in the above. You could also do this:

    Code:
    void input(char * first, char * last, double  & winningNumber);
    
    int main()
    {
       char * first = new char[80];
       char * last = new char[80];
       double winningNumber;
     
       input(first, second, winningNumber);
      
      //etc.
      //not a bad idea to use the delete [] operator here but
      //not absolutely necessary if the program ends here.
    as ClownPrince indicated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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