Solved

C Noobie Needs Help And Offer Big Points :)

Posted on 2003-11-24
22
347 Views
Last Modified: 2010-04-15
Harlow Every C Seniors and Superiors here.
I got two very basic and question to ask

1st) scanf
 #include <stdio.h>
 #include <conio.h>
 main()
 {
int a;
   char b;

   printf("Enter num: ");
   scanf("%d", &a);
   printf("Enter char: ");
   scanf("%c", &b);

   printf("%d\n",a);
   printf("%c",b);

   getch();
 }

look at this simple coding, after i enter num, i canot enter char.
can some1 tell me the reason why, instead of how cos i know by flushing it, the problem would be solve, and also if i reverse the coding by entering char 1st then entering num, it would also be okay. so what's wrong? pls explain to me generally.
another thing is, if i change the char declartion to string, and i prompt to enter num 1st then enter the string, it works fine too, the reverse, string 1st then num also work fines.

so what wif scanf ?
pls pls pls help very important

 #include <stdio.h>
 #include <conio.h>
 main()
 {
      char a[10];
   char b[10];

   printf("Enter string a: ");
   scanf("%s", a);
      printf("Enter string b: ");
   gets(b);
   printf("%s\n",a);
   printf("%s",b);
   getch();
 }

how come after scanf for string and i use gets , it cant work. but if i gets 1st then scanf, it work. why ? i know flushing can solve this problem but why ? thanks alot

2nd) pointers and array
i guess i will post this later...  THANKS ALL GUYS WHO HELP ME
I REALLY CANT FIND AN ANSWER ON THE NET AND THERE IS NO ONE ARD TO HELP ME
PLS PLS PLS PLS PLS PLS PLS GOD BLESS YOU FOR DOING A KIND DEED TO HELP ME PLS
0
Comment
Question by:CBigNoobie
  • 9
  • 5
  • 4
  • +3
22 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809540
>look at this simple coding, after i enter num, i canot enter char.
the enter key !!! \n

>how come after scanf for string and i use gets , it cant work
gets is not meant to be used .. read the man page ... its man page recommends not to use it
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809546
when you realize, that there are two read requests and you cannot see the second one executing and flushing buffers will help you get rid of the problem .... then the obvious answer is that the second read request is being satisfied by some data from the buffer !!!!
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9809616
u enter num say 10.
These characters and the terminating newline go into the keyboard buffer, scanf() skips over any leading white space and reads characters that form an integer and converts them to the internal form for an integer.It stops reading when the first non-digit is encountered. It stores the integer value, 10, in a and returns the number of items read, i.e. 1. The first integer, 10, is read correctly and printed , followed by a prompt to type in the charecter. but since buffer is not flushed this request is fulfilled already.

program does not wait for the user to type the response. A newline is printed as the next character read, and the program terminates. The reason is that when the user types the integer followed by a RETURN, the digit characters followed by the terminating newline are placed in the keyboard buffer.The function scanf() reads the integer until it reaches the newline character, but leaves the newline in the buffer. This newline character is then read as the next input character into b.
0
 

Expert Comment

by:jinumjoy
ID: 9809868
I want to add to the Question itself.

Is there a problem if ASCII 13(enter, hope i am r8) is also read by the scanf() and omit it while representing the integer/variable. I mean is there some strong reason to leave the "13" in the buffer???? Can anyone give me a scenario where in the \n left behind can be of use later????

The question is if you got to flush the buffer after a scanf() then let shouldn't scanf() be doing it?

-Jinu
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9810109
what if I wanted to read that \n as a char ? then you would complain that it flushes the \n ;o) ... if you are expecting a \n and do not want it to be read into a variable, you can always specify this in format string

scanf ( "%d\n%c",...);
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9813441
did it give u explanation u wanted r u want more clarification.
0
 

Expert Comment

by:JavaNoobie
ID: 9816000
erhmmm i needed more clarifications :/ kinda confuse.

what is whitespace ? what is the man page?

since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it is read into the variable a leaving \n behind?

then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?

i am sorry if i am too stupid about such things, i hope i dun offend or irritate you guys.
i am very sincere in learning from you guys.. pls do help :)
and thanks a million times.
0
 

Expert Comment

by:JavaNoobie
ID: 9816009
CBignoobie is my brother, just helping him to type the question
i am java noob
he is c noob
haha
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9816024
>what is whitespace ?
tab, blanks, newline

>what is the man page?
help pages available on a *nix machine ... if you are using one, type man scanf on command prompt

>since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it
>is read into the variable a leaving \n behind?
yes

>then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
no, the data types are different, it is looking for an int ... in the first case it was looking for a char and could not tell if \n was what user wanted to input as a char

>CBignoobie is my brother, just helping him to type the question
make sure these are not dups, they can land you in trouble
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9816066
whietspace is blank.
man page is manual page. it is help information for any command.
like for command scanf if u want to know more information/detailed information.
type
#man scanf
Q:since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it is read into the variable a leaving \n behind?

A: yes.  \n is left behind.
Q: then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
A: this time scanf is looking for char, scanf() skips over any leading white space and reads characters that form an char, It stops reading when the first non-char is encountered. that will be your number. since \n is also a charecter.
0
 

Author Comment

by:CBigNoobie
ID: 9816082
haha ya that my brother. thanks for all the help. i am quite clear already

as for scanf for char
let say you enter "a" and you press the enter key \n
the buffer contain a\n ?
and onli the a is put into variable? then how abt the \n?

does that means if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear?

:) u got clear explainatations
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9816094
>does that means if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear?

never assume such things ... it is bad programming ... always flush buffers ...

data types can be mixed to a certain extent .... you may be having a scanf() for a float while there is an int in the buffer ... you might assume that buffer is clear since there is no float in it but in fact the int will be read into the float ...

so, dont depend on it ... you will end up pulling your hair out ;o)
0
 
LVL 1

Expert Comment

by:Centeris
ID: 9817435
scanf problem:

this must be
scanf("%s", &a);
this works fine whether you declared the variable 'a' to be a string or a char.
i also had this problem and this made the trick.
0
 

Author Comment

by:CBigNoobie
ID: 9819083
Q: then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
A: this time scanf is looking for char, scanf() skips over any leading white space and reads characters that form an char, It stops reading when the first non-char is encountered. that will be your number. since \n is also a charecter.

i thought \n was also consider as an whitespace, then since scanf is looking for char
why do u say it skips over any leading whitespace(isnt \n a whitespace too)?
---------------------------------------------------------------------------------------
and i still dont understand
if i scanf for character 1st, then i scanf for integer, doesnt the \n after the character goes into the integer? since sunnycoder say i cant assume that if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear

sorry if i am slow. i am kinda getting the whole situtation clear, but still abit here and there of uncertainty thanks again for all the trouble though.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9823015
you might need an application where a space or a tab or a newline may be valid inputs "press enter to continue" ... how will you read them ? ofcourse using scanf !!! that is why, when you are trying to read in a char and there is a \n in the buffer, the \n will be assigned to that char
0
 

Author Comment

by:CBigNoobie
ID: 9825631
yea ha, but then how abt the part
whereby u scanf for char, press enter \n, then u scanf for number,why the \n is not put into the integer variable, becos of the diff datatype?
but then again you say cant assume that the buffer is clear when the next scanf request is of different datatype,

so how do i actually determine when the content in a buffer will be taken by the next scanf request or not?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9829609
>so how do i actually determine when the content in a buffer will be taken by the next scanf request or not?
depends on the contents of the buffer ... if the buffer has an int and you read a float, it will be read from the buffer as a float...
It is best to flush buffers before scanf and that is what you should be doing
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9829636
> whereby u scanf for char, press enter \n, then u scanf for number,why the \n is not put into the integer variable, becos of the diff datatype?

yes, scanf looks for integar and it can not make the request fulfilled with \n.
0
 

Author Comment

by:CBigNoobie
ID: 9831732
hmm ok I AM CLEAR!!!! Hurray!!!

just two last doubts i have now ;) sorry if i am long winded,
Q1)when will the scenario happen, when you scanf for an int, then you scanf for a float
how is it possible for that int to be in the buffer? after you scanf for that int?

what i mean is, when you scanf for a int number, let say the user enter 5,
5 is inside the buffer, then it is put into a int variable, how is it possible for a int value to remain in the buffer?

Q2)
errhmm remember if i scanf for a char, then i scanf for the int
the \n after i enter the char value is inside the buffer, then i scanf for the int, then become \n doesn satisfy the int value, so it will allow me to enter an int value, what happens to the \n? is it still inside the buffer?

now i really start to understand it is realli a must to flush, so troublesome!

haha

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 140 total points
ID: 9835964
>Q1)when will the scenario happen, when you scanf for an int, then you scanf for a float
>how is it possible for that int to be in the buffer? after you scanf for that int?

scanf ( "%s%f", str, f );

suppose user entered string "street 5" and 3.0 for float f
scanf will read only street into str (breaks on white space) and 5 will be read into float, 3.0 will remain in the buffer...
you will come across lots of combinations when you start programming ... wait until you get to feel the joy of discovery ;o)

>what happens to the \n? is it still inside the buffer?
Depends on the implementation, but even if it is in the buffer, it wont satisfy the next read request... they do not rewind buffers
0
 

Author Comment

by:CBigNoobie
ID: 9850508
hurray hurray !!! finallly i got the answer so happi !!!!!

anywhere where i can know when scanf keeps scanning or stop scanning for %i %c  and %s

what I is mean is for %i %s %c, does scanf ignore whitespaces, and when does it stop scanning?
 
just hoping for some kind help since i already awarded marks :)

thanks all pple who reply to this post.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9855634
>anywhere where i can know when scanf keeps scanning or stop scanning for %i %c  and %s
None that I know of ... you will learn it as you go along coding
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now