wrong output in c program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 1
Reputation: debashish is an unknown quantity at this point 
Solved Threads: 0
debashish debashish is offline Offline
Newbie Poster

wrong output in c program

 
-1
  #1
Nov 5th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: wrong output in c program

 
0
  #2
Nov 5th, 2004
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".
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 44
Reputation: jigvesh is an unknown quantity at this point 
Solved Threads: 0
jigvesh jigvesh is offline Offline
Light Poster

Re: wrong output in c program

 
0
  #3
Nov 5th, 2004
If you wanna program in C++ I think cin.getline() will work perfectly fine.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: jaeSun is an unknown quantity at this point 
Solved Threads: 0
jaeSun jaeSun is offline Offline
Light Poster

Re: wrong output in c program

 
0
  #4
Nov 5th, 2004
just use fgets instead of scanf
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: wrong output in c program

 
0
  #5
Nov 5th, 2004
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"...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 44
Reputation: jigvesh is an unknown quantity at this point 
Solved Threads: 0
jigvesh jigvesh is offline Offline
Light Poster

Re: wrong output in c program

 
0
  #6
Nov 5th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: wrong output in c program

 
0
  #7
Nov 5th, 2004
Originally Posted by jigvesh
But if it was a C++ program wasnt my answer right?
Sure it was...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC