Storing in an Array

This is a discussion on Storing in an Array within the C++ Programming forums, part of the General Programming Boards category; yeah i got that but it is the last name that messes up when you output it all. the first ...

  1. #16
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i got that but it is the last name that messes up when you output it all. the first name comes out fine but the last name dosnt it comes out in odd ascii charactures.

  2. #17
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    My code works perfectly well. If you please post yours it will be easier to find what you are doing wrong.

  3. #18
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    my code is the same as yours i think but here it is anyway...

    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    
    using namespace std;
    
      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];
              
              j ++;
          }
    
      cout << Fullname << endl;
       
     system("pause");
    }
    it also comes up with the first letter of the first name as the second name
    Last edited by peckitt99; 10-09-2006 at 03:13 PM.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    Oh, I had done a bracket typo in my code. It is functional now.

    The reason it outputted strange characters was that I forgot to terminate the string with a '\0'. And I had forgot to increment i in the last loop. It is getting late.

  5. #20
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    so what is needed in the code?

  6. #21
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i think i got it thanx

  7. #22
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    Well I did update my code, so just check it. It should be really simple to understand as well.

  8. #23
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    now i need to re-do the program to show i can use strcyp() n strncat(), all fun

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,038
    Quote Originally Posted by peckitt99
    now i need to re-do the program to show i can use strcyp() n strncat(), all fun
    Why are you using c strings? Use std::string and all your problems will go away. With strcyp() and strncat() anyway. That's what it's there for.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I think he needs to do it with strncpy() and strncat() not with strings.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Page 2 of 2 FirstFirst 12
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, 02: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, 06:49 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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