944,124 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2487
  • C++ RSS
Nov 5th, 2004
-1

wrong output in c program

Expand Post »
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 <stdio.h>

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
debashish is offline Offline
1 posts
since Nov 2004
Nov 5th, 2004
0

Re: wrong output in c program

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".
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 5th, 2004
0

Re: wrong output in c program

If you wanna program in C++ I think cin.getline() will work perfectly fine.
Reputation Points: 10
Solved Threads: 0
Light Poster
jigvesh is offline Offline
44 posts
since Oct 2004
Nov 5th, 2004
0

Re: wrong output in c program

just use fgets instead of scanf
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004
Nov 5th, 2004
0

Re: wrong output in c program

Quote originally posted by jigvesh ...
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"...
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 5th, 2004
0

Re: wrong output in c program

Quote originally posted by frrossk ...
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?
Reputation Points: 10
Solved Threads: 0
Light Poster
jigvesh is offline Offline
44 posts
since Oct 2004
Nov 5th, 2004
0

Re: wrong output in c program

Quote originally posted by jigvesh ...
But if it was a C++ program wasnt my answer right?
Sure it was...
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help me out............
Next Thread in C++ Forum Timeline: [Q] Generating compile time error when sizeof(int) != 4





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC