Computing.Net > Forums > Programming > a C problem

a C problem

Reply to Message Icon

Original Message
Name: eugensth
Date: August 17, 2005 at 14:32:33 Pacific
Subject: a C problem
OS: Win 2000
CPU/Ram: 450 Mhz 128 MB
Comment:

I have a problem when executing pieces of code of this type:

main(){

char a,b;
a=getchar();
b=getchar();
}

The program waits for the first assignation and after the first character is stored,it exits without waiting
to receive the second character, ignoring the second instruction.
Can somebody tell me how to avoid this, so that both of the instructions can be executed?
I used gcc and Borland C++.

Thanks anticipated.


Report Offensive Message For Removal


Response Number 1
Name: tommycoolman
Date: August 17, 2005 at 21:10:39 Pacific
Reply: (edit)

The problem is that 'a' is assigned the first character you enter, then 'b' is assigned the 'enter' key. You can avoid this by clearing STDIN after you get 'a'. You can do this by having a temporary buffer.

int main()
{
char a,b,buffer[32];
a = getchar();
gets(buffer);
b = getchar();

}

GCC will warn you about using the 'gets()' function, but in this case I think it is harmless.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: a C  problem

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge