2 Replies - 34449 Views - Last Post: 18 September 2008 - 12:52 AM Rate Topic: -----

#1 her_ron_potter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-September 08

C: How to print out string with white space

Post icon  Posted 17 September 2008 - 03:26 AM

I want to print out string input from user with space. Such as:
User input: Nguyen Tien
then output: Nguyen Tien

This is my code:

#include<stdio.h>
void main()
{
	char name[40];
	

	//Prompt user to input
	printf("Enter your name: ");
	scanf("%s", name);
	

	printf("Ur name is %s ", name);

}


But when I input: Nguyen Tien
it showed only: Nguyen

Can u help me to fix it? Thanks :rolleyes:

Is This A Good Question/Topic? 0
  • +

Replies To: C: How to print out string with white space

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: C: How to print out string with white space

Posted 17 September 2008 - 03:32 AM

scanf in C and cin in C++ accepts strings without whitespace.
If you want to accept string upto \n [till user press enter.] Then you have to use gets or getline [in C++].


I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#6 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: C: How to print out string with white space

Posted 18 September 2008 - 12:52 AM

Quote

I want to print out string input from user with space


 
//This is in C++
#include<iostream> 
#include<string>
using namespace std;

int main() //main must return an int. Do not use void.
{
    string name;
    
    cout <<"Enter your name :";
    getline(cin, name);
    cout <<"\nYour is name is : "<<name;  //Printing including space
    
    return 0;
}


I hope this helps.
Was This Post Helpful? 1

Page 1 of 1