14 Replies - 353 Views - Last Post: 02 September 2013 - 11:46 AM Rate Topic: -----

#1 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Weird output from program.

Posted 02 September 2013 - 09:48 AM

All I am working on a small application that takes input continously until the user enters a . on a line by itsself. The problem I run into is the output. I get the following

Your message ╥╙╘╒╓╫╪┘┌█▄▌▐▀└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓≈╪┘┌█▄▌▐ ☺☻♥Test. will be sen
t.

The following code was created on my windows 7 workstation using the latest codeblocks ide. Not sure if this would be the cause but figured I would add it in just in case.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
int main()
{
    char line[999]; /* Holds User Input */
    char msg[999]; /* Holds users message. */
    printf("Enter your message. Enter a . on a line by itself to end.\n");
    while(1)
    {
        fgets(line, sizeof(line), stdin);
        line[strlen(line) - 1] = '\0';
        if(strcmp(line, ".") == 0)
        {
            break;
        }
        /* Currently this line does not seem to work.
         * At the end all I get is your message of . prints out.
         */
        strcat(msg, line);
    }
    printf("\nYour message %s will be sent.\n", msg);
    return 0;
}




Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Weird output from program.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: Weird output from program.

Posted 02 September 2013 - 10:06 AM

You're not initializing msg.
Was This Post Helpful? 1
  • +
  • -

#3 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Weird output from program.

Posted 02 September 2013 - 10:13 AM

Thanks CTphpnwb, That did the trick. Could I trouble you to explain why not initializing the array caused the crazy output? Also should I always initialize an array to keep this from happening? Thanks.

This post has been edited by samuraitux: 02 September 2013 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,268
  • Joined: 31-December 10

Re: Weird output from program.

Posted 02 September 2013 - 10:17 AM

I didn't change a thing and this is the result I get from running your code:

Quote

./strcatTest
Enter your message. Enter a . on a line by itself to end.
hello world
.

Your message hello world will be sent.


View Postsamuraitux, on 02 September 2013 - 01:13 PM, said:

Thanks CTphpnwb, That did the trick. Could I trouble you to explain why not initializing the array caused the crazy output? Also should I always initialize an array to keep this from happening? Thanks.

Here's a neat little trick that clears the array to all zeroes:
char msg[999] = { 0 };


Was This Post Helpful? 1
  • +
  • -

#5 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Weird output from program.

Posted 02 September 2013 - 10:17 AM

Good catch, CT.

My guess is:
When you don't initialize variables, garbage values are placed in them. Thus, those garbage cause weird things to happen like your output.
ie: int variables hold a garbage value of something long like 74593485943
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,268
  • Joined: 31-December 10

Re: Weird output from program.

Posted 02 September 2013 - 10:20 AM

View PostJava Student, on 02 September 2013 - 01:17 PM, said:

Good catch, CT.

My guess is:
When you don't initialize variables, garbage values are placed in them. Thus, those garbage cause weird things to happen like your output.
ie: int variables hold a garbage value of something long like 74593485943

The OP was concatenating onto 'msg'. Like my previous post said, without changing the code, I got correct results. But it's a good idea to remember to always NULL-terminate C-style strings.
Was This Post Helpful? 0
  • +
  • -

#7 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Weird output from program.

Posted 02 September 2013 - 10:24 AM

View Postvividexstance, on 02 September 2013 - 12:17 PM, said:

I didn't change a thing and this is the result I get from running your code:

Quote

./strcatTest
Enter your message. Enter a . on a line by itself to end.
hello world
.

Your message hello world will be sent.


View Postsamuraitux, on 02 September 2013 - 01:13 PM, said:

Thanks CTphpnwb, That did the trick. Could I trouble you to explain why not initializing the array caused the crazy output? Also should I always initialize an array to keep this from happening? Thanks.

Here's a neat little trick that clears the array to all zeroes:
char msg[999] = { 0 };




Thanks. I forgot to clear that note once I got the msg output to work for me.
Was This Post Helpful? 0
  • +
  • -

#8 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Weird output from program.

Posted 02 September 2013 - 10:36 AM

Thanks everyone for your help.

I made some few tweaks to the final program. I am posting just in case someone is looking for something similar.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
int main()
{
    char line[999] = {0}; /* Holds User Input and Initializes array */
    char msg[999] = {0}; /* Holds users message and Initializes array */
    printf("Enter your message. Enter a . on a line by itself to end.\n");
    while(1)
    {
        fgets(line, sizeof(line), stdin);
        /* This will remove the new line character. and allows me to test for a single period on the screen. */
        /* Look at changing in the future to be just an empty line. */
        line[strlen(line) - 1] = '\0';
        if(strcmp(line, ".") == 0)
        {
            break;
        }
        /* Add newline back in so that when sending as an email it will format correctly. */
        line[strlen(line)] = '\n';
        strcat(msg, line);
    }
    printf("\nYour message %s will be sent.\n", msg);
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Weird output from program.

Posted 02 September 2013 - 10:59 AM

Be careful with that strcat(). The way your program is structured you could quite easily access your msg[] array out of bounds. You need to make sure you have room left in your array to add the other array before you blindly strcat() these two arrays.

Also why are you removing the end of line character on line 16 then overwriting the end of string character with this character on line 22? If your C-string had not been initialized to all zeros you would be in trouble.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Weird output from program.

Posted 02 September 2013 - 11:04 AM

View Postjimblumberg, on 02 September 2013 - 12:59 PM, said:

Be careful with that strcat(). The way your program is structured you could quite easily access your msg[] array out of bounds. You need to make sure you have room left in your array to add the other array before you blindly strcat() these two arrays.

Also why are you removing the end of line character on line 16 then overwriting the end of string character with this character on line 22? If your C-string had not been initialized to all zeros you would be in trouble.

Jim

Thanks for the warning. would I need to subtract maybe sizeof(array) - strlen(array) to get space left or is there a better way for me to do this check?
The reason I removed the end of line character was because it was causing issues with my checks for just a period on a line by itself. But after the check I wanted the formating to be there so that when I truely get it to send out as a message, the message is not on a single line.

I am sure there are several cools ways to get around this but with my limited knowledge (Basically I have been reading on C for only 3 days straight now) I couldn't come up with any. But if you have any suggestions I am ready to learn them and try them out.

Thanks

This post has been edited by samuraitux: 02 September 2013 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,268
  • Joined: 31-December 10

Re: Weird output from program.

Posted 02 September 2013 - 11:07 AM

Check out this page on strcat() and read what the function actually does. You might also want to just bookmark that site as it is a good reference site that's always linked here.
Was This Post Helpful? 1
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Weird output from program.

Posted 02 September 2013 - 11:14 AM

Quote

Thanks for the warning. would I need to subtract maybe sizeof(array) - strlen(array) to get space left or is there a better way for me to do this check?

Check the strlen() of both strings, if you have enough space in your msg[] array to hold these added characters then do the strcat().

Since this is a single function program you can use sizeof() to determine the size of your array, but when you start using functions sizeof only works where the array was defined. I recommend you use a constant for your array sizes.

// Before main
#define MAX_ARRAY 999;

...

char myArray[MAX_ARRAY];



Quote

The reason I removed the end of line character was because it was causing issues with my checks for just a period on a line by itself.

To me it would be easier to just add the new line character to your constant:

        if(strcmp(line, ".\n") == 0)




Jim
Was This Post Helpful? 1
  • +
  • -

#13 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Weird output from program.

Posted 02 September 2013 - 11:32 AM

View Postjimblumberg, on 02 September 2013 - 01:14 PM, said:

Quote

Thanks for the warning. would I need to subtract maybe sizeof(array) - strlen(array) to get space left or is there a better way for me to do this check?

Check the strlen() of both strings, if you have enough space in your msg[] array to hold these added characters then do the strcat().

Since this is a single function program you can use sizeof() to determine the size of your array, but when you start using functions sizeof only works where the array was defined. I recommend you use a constant for your array sizes.

// Before main
#define MAX_ARRAY 999;

...

char myArray[MAX_ARRAY];



Quote

The reason I removed the end of line character was because it was causing issues with my checks for just a period on a line by itself.

To me it would be easier to just add the new line character to your constant:

        if(strcmp(line, ".\n") == 0)




Jim

Thanks Jim. I have attached my new code. Thanks for your explanations, they are really helpful.

Also vividexstance I have bookmarked the site and I read through the strcat(). One thing I just learned from that is that it will actually overwrite the null terminator zero but as Jim pointed out it won't do any bounds checking.

Again this is all really good information.

One more question that I thought of. Is it best practice that if I am going to put a hard coded value like for my array that I should look at making a Constant so that if I choose to extend it in the future I can just use modify the constant and do easier value checks against it?



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ARRAY 999

int i;
int main()
{
    char line[MAX_ARRAY] = {0}; /* Holds User Input */
    char msg[MAX_ARRAY] = {0}; /* Holds users message. */

    printf("Enter your message. Enter a . on a line by itself to end.\n");
    while(1)
    {
        fgets(line, sizeof(line), stdin);
        if(strcmp(line, ".\n") == 0)
        {
            break;
        }

        /* Move to a function after learning how to create my own functions. */
        /* Which will be in 10 more chapters. */
        /* Added the + 1 to take into account the string terminator. */ 
        if((strlen(line) + strlen(msg) + 1) <= MAX_ARRAY)
        {
            strcat(msg, line);
        }
    }
    printf("\nYour message %s will be sent.\n", msg);
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: Weird output from program.

Posted 02 September 2013 - 11:44 AM

Quote

Is it best practice that if I am going to put a hard coded value like for my array that I should look at making a Constant so that if I choose to extend it in the future I can just use modify the constant and do easier value checks against it?
Yes.
Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,268
  • Joined: 31-December 10

Re: Weird output from program.

Posted 02 September 2013 - 11:46 AM

View Postsamuraitux, on 02 September 2013 - 02:32 PM, said:

One more question that I thought of. Is it best practice that if I am going to put a hard coded value like for my array that I should look at making a Constant so that if I choose to extend it in the future I can just use modify the constant and do easier value checks against it?

Definitely, when you use hard-code numbers like that, they're called magic numbers, because in larger programs, it does get out of control and names for the numbers makes the code that much easier to read.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1