How do you add a char at the end of a string?

This is a discussion on How do you add a char at the end of a string? within the C++ Programming forums, part of the General Programming Boards category; I have a code where it says if it has an odd number of chars, add a random char A-Z ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    How do you add a char at the end of a string?

    I have a code where it says if it has an odd number of chars, add a random char A-Z heres the code for that:

    Code:
    if(strlen(message) % 2 == 1) 
    	{	while(message[i] != '\0')
    		{	i++;
    		}
    		message[i] = char((rand() % 26) + 65);
    	}
    When I display that string, it gives me the chars I entered along with the random one but this is what comes out... ABC is the message i entered and R is the random char... I dont know what the other stuff is. This only happens when I add a character.

    A B C R ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ♥ Press any key to continue


    If you need it here is the entire function...

    Code:
    void main()
    {
    	int d = 157, n = 2773, e = 17, i = 0, encoded[100];
    	char message[50];
    
    	srand(time(NULL));
    
    	for(int trash = 0; trash <= 10; trash++)
    		rand();
    	
    
    	cout<<"Enter a message (no spaces): ";
    	cin>>message;
    	cout<<"You entered: "<<message<<endl;
    	
    	if(strlen(message) % 2 == 1) 
    	{	while(message[i] != '\0')
    		{	i++;
    		}
    		message[i] = char((rand() % 26) + 65);
    	}
    	display(message);
    
    }
    And here is the function I use to display the string:

    Code:
    void display(char m[])
    {	int i = 0;
    	while(m[i] != '\0')
    	{	cout<<m[i]<<" ";
    		i++;
    	}
    }
    The int variables are for later in the program so they dont mean anything now, including the encoded[] array.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    message[i] = char((rand() % 26) + 65);//overwriting here
    What you are doing is overwriting the '\0' terminator after the ABC with R, so it's no more a string but simply a chunk of characters, and when you display it, with the condition m[i]!='\0', it gives you random chars as no '\0' is found.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by BEN10 View Post
    Code:
    message[i] = char((rand() % 26) + 65);//overwriting here
    What you are doing is overwriting the '\0' terminator after the ABC with R, so it's no more a string but simply a chunk of characters, and when you display it, with the condition m[i]!='\0', it gives you random chars as no '\0' is found.
    Thank you!

    I changed the code to

    Code:
    if(strlen(message) % 2 == 1) 
    	{	while(message[i] != '\0')
    		{	i++;
    		}
    		message[i] = char((rand() % 26) + 65);
    		message[i + 1] = '\0';
    	}
    and it worked perfectly!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Use std::string instead. It can append characters using +=.
    And don't use void main! SourceForge.net: Void main - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    char((rand() % 26) + 65)
    Change to:
    Code:
    char((rand() % 26) + 'A')
    And use std::string as Elysia suggested. What you have will only work until someone overflows your buffer. std::string will avoid this, and it'll be easier to code with.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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