Thread: Newbie(please help)

  1. #1
    Unregistered
    Guest

    Newbie(please help)

    Hi,
    The question is to input 3 names from user by char array and display names.
    the 2 funcns r must:
    a)getnames()
    b)display array

    As a selflearner,find this forum really helpful. Please look my code,and let me know where I am going wrong.

    Code:
    #include <iostream>
    using namespace std;
    
    const int rows =3;
    const int col=6;
     
    
    char getname();
    void displayarray(const char [][6],int, int);
    
    int main()
    {
     
    char arrays[3][6];
    getname();
    displayarray(arrays,rows,col);
    
    return 0;
    }
    
    char getname()
    {
      char arraynames[3][6];
      cout<<"enter 3 names"<<endl;
      for (int j=0;j<rows;j++)
      {
          for (int k=0;k<col;k++)
           {
             cin>>arraynames[j][k];
              return arraynames[j][k];
           }
      }
    }
    
    
    void displayarray(const char array[3][6],int r,int c)
    {
    
       for(int x=0;x<r;x++)
         {
           cout<<"name["<<x<<"]";
             for(int i=0;i<c;i++)
                   {
    	   cout<<getname[x][i];
    			
                    }
         }
    
    
    }
    thanks in advance.
    Thanks

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Code:
    char getname()
    {
      char arraynames[3][6];
      cout<<"enter 3 names"<<endl;
      for (int j=0;j<rows;j++)
      {
          for (int k=0;k<col;k++)
           {
             cin>>arraynames[j][k];
              return arraynames[j][k];
           }
      }
    }

    This function only returns one character because once return is reached, the function is exited. Try making it return an array of characters. Such as return[j]

  3. #3
    Unregistered
    Guest
    Since you go to all the trouble to declare const variables to represent the number of rows and cols I would use them wherever you can. It makes changing code later easier. However, as you are going to store strings in the 2D char array I would use more descriptive terms like MAX_NAMES and MAX_LENGTH rather than rows and cols.

    When using strings you can do char by char manipulation as you do with your loops, but it isn't user (or programmer) friendly. Instead read in a whole string at a time using >>. If you need to then use the functions in the standard string.h or cstring (depending on your compiler) file. Here's an example:

    bool valid = false;

    char array[MAX_NAMES][MAX_LENGTH + 1];
    for(int i = 0; i < MAX_NAMES; i++)
    {
    valid = false;
    while(!valid)
    {
    cout << "enter a name" << endl;
    char buffer[256];
    cin >> buffer;
    int len = strlen(buffer);
    if(len <= MAX_LENGTH)
    {
    valid = true;
    strcpy(array[i], buffer);
    }
    else
    cout << "name too long " << endl;
    }
    }

    for(int i = 0; i < MAX_NAMES; i++)
    {
    cout << array[i] << endl;
    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    Hi,
    Let's suppose user enters: John Jim Bob and hits enter
    here's how I read your getname() function

    char getname() {
    char arraynames[3][6]; //declare array
    cout<<"enter 3 names"<<endl;
    for (int j=0;j<rows;j++) { //j=0
    for (int k=0;k<col;k++) { //k=0
    cin>>arraynames[j][k]; //read John Jim Bob as the first letter of A[0][0]
    return arraynames[j][k]; //exit func; output the first letter
    //obviously you don't want to exit here
    }
    }
    }

  5. #5
    Unregistered
    Guest

    But still...(help a newbie)

    Thanks for replying,and pointing the flaws.
    But still ....its showing compile errors.

    error C2109: subscript requires array or pointer type.(on the last line).
    I have not started with chapter pointera yet.

    the modified code:

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    
    const int MAX_NAMES =3;
    const int MAX_LENGTH=6;
     
    
    char getname();
    void displayarray(const char [][6],int, int);
    
    int main()
    {
     
    	char arrays[3][6];
    	getname();
    	displayarray(arrays, MAX_NAMES,MAX_LENGTH);
    
    return 0;
    }
    
    char getname()
    {
    bool valid = false; 
    
    char array[MAX_NAMES][MAX_LENGTH + 1]; 
    for(int i = 0; i < MAX_NAMES; i++) 
    { 
    valid = false; 
    while(!valid) 
    { 
    cout << "enter a name" << endl; 
    char buffer[256]; 
    cin >> buffer; 
    int len = strlen(buffer); 
    if(len <= MAX_LENGTH) 
    { 
    valid = true; 
    strcpy(array[i], buffer); 
    } 
    else 
    cout << "name too long " << endl; 
    } 
    } 
    
    for(int j = 0; j < MAX_NAMES; j++) 
    { 
    cout << array[j] << endl; 
    }
    
    }
    
    
    void displayarray(const char array[3][6],int r,int MAX_LENGTH)
    {
    
    for(int x=0;x< MAX_NAMES;x++)
       {
          cout<<"name["<<x<<"]";
    
    	for(int i=0;i<MAX_LENGTH;i++)
                     {
    	     cout<<getname[x][i];///displaying error on thisline///
    	}
        }
    
    }
    please help !!!

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You cannot use getname like that. You have declared it as a function yet are trying to use it as an array.

    In addition, you have declared getname to return a char yet it has no return statement (returns nothing).

    Thirdly, the array you declared in main is never touched by char getname because it goes out of scope. To solve this problem, try declaring the array in main as a pointer then it should be accessible throughout the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie!!please help
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 06:05 PM