8 Replies - 1757 Views - Last Post: 19 April 2013 - 11:31 AM Rate Topic: -----

#1 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Printing array in between text on same line.

Posted 18 April 2013 - 02:01 PM

Here's the code:

 
printf("You entered ");
    for (int i = 0; i < strlen(key); i++)
    {
        printf("%c", key[i]);
    }
    printf(" as your key\n");



the last printf function prints on a new line. Is there anyway i can get this all on the same line? I tried nesting the for loop inside the printf function but that doesn't seem to be allowed.
Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Printing array in between text on same line.

#2 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Re: Printing array in between text on same line.

Posted 18 April 2013 - 03:28 PM

Also, what exactly is causing the newline? Is there automatically a new line after a loop?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Printing array in between text on same line.

Posted 18 April 2013 - 08:41 PM

Please show how you defined key[] and how you inputted the value.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Re: Printing array in between text on same line.

Posted 18 April 2013 - 09:04 PM

//Array that will store unencrypted text
    char text[1024];

    //get key
    printf("Enter your desired key: ");
    char key[30];
    fgets(key, 30, stdin);

    printf("You entered ");
    for (int i = 0; i < strlen(key); i++)
    {
        printf("%c", key[i]);
    }
    printf(" as your key\n");


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Printing array in between text on same line.

Posted 18 April 2013 - 09:15 PM

As jimblumberg pointed out to me about a week ago, fgets() includes the newline.
See: http://www.cplusplus...e/cstdio/fgets/

You'll need to remove the newline from your string, or skip it when printing out the characters.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Printing array in between text on same line.

Posted 18 April 2013 - 09:19 PM

Quote

Also, what exactly is causing the newline?

The fgets() function extracts the new line from the stream and inserts it into the string, if the size of the string is less than the second parameter.

The easiest way to "fix" this problem is to test the last character and if it's the new line character remove it.

    char key[30];
    fgets(key, 30, stdin);
    if(key[strlen(key)-1] == '\n')
       key[strlen(key) -1 = '\0'; 

   printf("You entered %s as your key.\n", key); // You could do this instead of the loop.



Also since key is a C-string you could use the "%s" format specifier for printf() instead of the loop and the "%c" format specifier.



Jim
Was This Post Helpful? 0
  • +
  • -

#7 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 13

Re: Printing array in between text on same line.

Posted 19 April 2013 - 11:01 AM

isn't the size of the string and second parameter of fgets the same? Unless by size you actually mean how much of the array is filled up with elements.
char key[30]; fgets(key, 30, stdin);

thanks for the info on fgets!

This post has been edited by gordlonious: 19 April 2013 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Printing array in between text on same line.

Posted 19 April 2013 - 11:22 AM

If your string looks like this: sometext\n\0 then what do you get from strlen()? Do you think that's right?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Printing array in between text on same line.

Posted 19 April 2013 - 11:31 AM

What I'm trying to explain, badly I guess, is that the string you are trying to retrieve can be larger than your buffer, in this case your buffer will not contain the end of line character. For example you enter, or the file contains the following:

"My big string contains 39 characters."

So when you try the following:
char key[30];
fgets(key, 30, stdin);


The variable key will contain these 30 characters, "My big string contains 30 cha\0", 29 characters plus the end of string character. Notice the end of line character is not in this string. The following characters are left in the input buffer "racters\n".

But if your key was declared with a size of 50 and you used this size in your fgets() key would contain "My big string contains 38 characters.\n\0". Notice the end of string character is included in your string.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1