954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

wrong output in c program

I am very new to c/c++ programming. i was writing a few lines of code and after compiling and running the program i did not receive the desired output. can you please help me in this matter.

/* Test.c */
#include

main()
{
char i[30];
clrscr();
printf("Enter a name: ",i);
scanf("%s",i);
printf("The name is %s",i);
}

When I run the above program It prompts me to enter the name.

If I enter:

Debashish

(Debashish is my name).
it will display:

The name is Debashish

But if I enter:

Debashish Gupta

(My Full Name)
It will display Debashish and not Debashish Gupta.


Can anyone please tell me where am i wrong.

debashish
Newbie Poster
1 post since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

The problem is that "scanf" reads the input until it meets a "\n" or a blank character. Try using a function that reads the whole line, like "gets".

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

If you wanna program in C++ I think cin.getline() will work perfectly fine.

jigvesh
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

just use fgets instead of scanf

jaeSun
Light Poster
31 posts since Oct 2004
Reputation Points: 12
Solved Threads: 0
 
If you wanna program in C++ I think cin.getline() will work perfectly fine.


You're right, but I saw somewhere a comment "Test.c"... ;)

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 
You're right, but I saw somewhere a comment "Test.c"... ;)

OOPS I didn't have a look at that. I am really sorry. But if it was a C++ program wasnt my answer right?

jigvesh
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
But if it was a C++ program wasnt my answer right?


Sure it was...

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You