Thread: Storing in an Array

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Storing in an Array

    i would like to know if i am on the right tracks or completely off the rails with this program. i need to Store the name in an array. Then ask for their surname. The program should declare a third array large enough to hold both names including a space between them. The names should both be copied to the third array (with a space between them) and the resultant array output to the screen. i amnot use any string library functions; manage the arrays yourself using loops to do the name copying and concatenation.

    This is what i have got soo far and am stuck with the output the final array

    Code:
    char Fname[10];
    char Lname2[10];
    char FullName;
    int i;
    
    cout <<"enter your first name";
    
    for (i=0; i<3; i++)
        {
        cin >> Fname[i];
        }
        
    cout<<"Please enter you Second Name";
    
    for (i=0; i<3; i++)
        {
        cin >>Lname2[i];
        }
        
    for (i=0; i<20; i++)
    {
    cout << Fname[i] <<Lname2[i];
     }      
    system("pause");
    }

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Read it in with cin.getline().
    Printf is a bit more comfortable to use.
    Do not use system("pause"), look what I've used
    Code:
    #include <iostream>
    
    int main(){
        char Fname[10];
        char Lname[10];
        char FullName;
        int i;
        printf("Enter your first name: ");
        std::cin.getline(Fname,10,'\n');
        printf("Enter your second name: ");
        std::cin.getline(Lname,10,'\n');
        printf("%s %s\n",Fname,Lname);    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    Last edited by maxorator; 10-09-2006 at 12:37 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    with that code, is it copying it to array1 and array2 to array3? and i thought there were loops to get it to work then way the problem has been set out?

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    No, there are 2 arrays.
    It reads the first name to the first array and the last name to the second array.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    ah right, but i need it to check the length cause i needed summat like
    Code:
     for(i=0; i<10; i++)
    or something like that. once it has checked the first array it stores it and then asks for the second name does the same thing, Then i need The names to both be copied to the third array (with a space between them) and the resultant array output to the screen.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    Yes, but if you had taken the time to read his message, you would have known that was not what he wanted to do.

    peckitt99:
    In your code, Fullname cannot possibly store the concatenated string, since it is a char, not a char[]. You will need to copy the characters from the first string, until you encounter the null character, '\n'. Then you copy the white space, and then copy the characters from the second name until you encounter the null character.

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    KAISER is it possible for some example code to get me started on it?

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Now it does that:
    Code:
    #include <iostream>
    
    int main(){
        char Fname[10];
        char Lname[10];
        char FullName[20];
        printf("Enter your first name: ");
        std::cin.getline(Fname,10,'\n');
        printf("Enter your second name: ");
        std::cin.getline(Lname,10,'\n');
        sprintf(FullName,"%s %s",Fname,Lname);
        printf("%s\n",FullName);    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    First of all, you need to define Fullname as an array of 20 characters (including null termination), as 'char fullname[20];'. The loop(s) could be something like:

    Code:
    int i = 0;
    while (Fname[i] != '\0') {
      Fullname[i] = Fname[i];
      i ++;
    }
    Fullname[i] = ' ';
    i ++;
    int j = 0;
    while (Lname[j] != '\0') {
      Fullname[i] = Lname[j];
      j ++;
    }

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah that sound more familiar than maxorators code. ill give it a go n see how i go along

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    His code is perfectly correct, but as you said in the first post that you are not to use any built in string handling, it is not what you asked for.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i have got all the code working with all the other bits but am unsure how to input it all. i tried
    Code:
    Fullname[20] = Fname[i] + Lname[i]
    the output i got from that was just the first name then a bunch of other characters.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    I don't get the question. I though you wanted to 'cin >> Fname;' and 'cin >> Lname;', and then do the copying.

    When working with arrays, the assignment operator does not do what you might think. When you learn about pointers you will know why.
    Last edited by Kaiser; 10-09-2006 at 01:30 PM.

  14. #14
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well when i got the 2 names in the arrays i need to get the third array to output both the names and put in the space inbetween the Fname and Lname. if this makes any more sence?

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18

    Wink

    The copying is already done, so you just output it. The entire program would look something like this:

    Code:
    int main() {
      char Fname[10];
      char Lname[10];
      char Fullname[20];
      cout << "First name: ";
      cin >> Fname;
      cout << "Family name: ";
      cin >> Lname;
      int i = 0;
      while (Fname[i] != '\0') {
        Fullname[i] = Fname[i];
        i ++;
      }
      Fullname[i] = ' ';
      i ++;
      int j = 0;
      while (Lname[j] != '\0') {
        Fullname[i] = Lname[j];
        i ++;
        j ++;
      }
      Fullname[i] = '\0';
      cout << Fullname << endl;
      return 0;
    }
    Last edited by Kaiser; 10-09-2006 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing an array of structures?
    By Sparrowhawk in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 03:07 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Storing multiple string in array
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-15-2004, 07:49 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM