character array problem .. please help :((

This is a discussion on character array problem .. please help :(( within the C++ Programming forums, part of the General Programming Boards category; Question is : Get two strings from user. Join first string with the second string with a space. Store in ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Unhappy character array problem .. please help :((

    Question is :
    Get two strings from user. Join first string with the second string with a space. Store in another string and display it.
    Example:
    Enter first string: Hello
    Enter second string: World
    Hello World
    (built in function for string concatenation is not allowed)


    im newbie in c++ and arrays please tell me whats the problem in my code

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    
    void main()
    
    {
    clrscr();
    
    char a[100];
    char b[100];
    char c[200];
    
    int s;
    
    cin.getline(a,100);
    
    cin.getline(b,100);
    
    
    for (s=1;c[s]!=NULL;s++)
    {
    c[s]=a[s]+b[s];
    }
    
    
    puts(c);
    
    
    
    getch();
    }

  2. #2
    Registered User Mirage's Avatar
    Join Date
    Dec 2010
    Posts
    6
    you need to use 2 loops, right now you are copying 2 characters into 1 place on the array,which you can't do.
    And even if you could it would do something you don't want it to do

    example of what you are doing
    a= STR
    b= str
    proposed result of your code
    SsTtRr

    also array indexes start at 0 not 1

    finally I don't think you can stop at NULL, rather it would be the null character '\0'
    Last edited by Mirage; 12-20-2010 at 12:04 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    thanks for your response.. i still have a problem... in the following code it is storing a chracters not the b chracters into c.. please tell me where im wrong :


    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    
    void main()
    
    {clrscr();
    
    char a[100];
    char b[100];
    char c[200];
    
    int s, i;
    
    int count=1;
    int countb=0;
    
    cin.getline(a,100);
    
    cin.getline(b,100);
    
    
    
    for (i=0;a[i]!='\0';i++)
    {count++;}
    
    countb=count;
    
    for (i=0;b[i]!='\0';i++)
    {countb++;}
    
    
    cout<<count<<endl;
    
    cout<<countb<<endl;
    
    
    for (i=0;i<count;i++)
    {c[i]=a[i];}
    
    
    int r=0;
    
    for (i=count;i<countb;i++)
    {
    c[i]=b[r++];
    }
    
    puts(c);
    
    getch();
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Code:
    cout<<c[6]<<c[7];
    it is displaying 2nd array..not by puts(c)

    i.e cout of c[i] in loop... why not it is showing through puts?
    Last edited by amerjamil; 12-21-2010 at 04:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-23-2010, 03:19 PM
  2. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 08:27 AM
  3. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 06:07 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

Tags for this Thread


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