Jump to content

string interrupted when there is a space

- - - - -

  • Please log in to reply
3 replies to this topic

#1
nerio

nerio

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi.

I study C. I wrote the following program:

main()

{

char yourname[20];

printf("Enter your name: ");

scanf("%s", &yourname);

printf("The name you entered is %s",yourname);

getch();

}


But when I enter eg: 'Peter South', it only displays 'Peter'. I am used from Pascal, that the whole string is considered not to end when there is a space. How can I correct the program that it will display whole name user enters?

I have also try to correct it, here is the result:

main()

{

char yourname[40];

printf("Enter your name: ");

scanf("%s", &yourname);

int i;

printf("The name you entered is:\n");

for(i=0;i<40;i++){

printf("%s",yourname[i]);

}

getch();

}


But this doesn't work. Could you please help?

Thank you.
I am a student of C language.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
There are methods with scanf to scan a whole string, however scanf is mainly a function to scan tokens (i.e. a single string) and not a whole line and is unsafe to use as a function to retrieve a line.

Try to look at functions such as fgets():
fgets - C++ Reference
//prototype: char * fgets ( char * str, int num, FILE * stream );
fgets ( yourname, sizeof( yourname ), stdin );

Also a few notes:

1: on your scanf, &yourname is not required as "yourname" should already hold the location of the beginning of the array.
2: Your for loop, you should use "%c" as you are only printing a character, yourname[0..39] and not a string.
3. Fgets will only accept length(yourname)-1 characters, the last one being NULL as per what a C string really is. Your scanf will accept as many or more and may not qualify to be read as a string ,i.e. "%s", as the bounds may not be known.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
If you're using C++, you can use:
#include <iostream>
...
std::string myline;
std::getline(std::cin, myline);

Of course, you'd put that in your main()​.
sudo rm -rf /

#4
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
scanf() reads one word at a time from the file descriptor stdin. stdin is basically like a file that includes all user input to a program during its execution. If you want it to read up to the carriage return, you should use gets() or fgets().
Programming is a journey, not a destination.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users