Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-08-2002, 06:48 PM
|
#1
|
LQ Newbie
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22
Rep:
|
getchar()
how am I supposed to implement this function?
I used
getchar(cfever);
but it wont get a character to cfever.. and the complier spits this out:
exercise01.c: In function `main':
exercise01.c:70: too many arguments to function `getchar'
this is my code..
printf("\n Which of the following symptoms does"
"\n the patient have (Y for yes, N for no)"
"\n");
fever:
printf("\n Fever -------- ");
getchar(cfever)
switch (cfever) {
case 'y':
case 'Y':
fever = 1;
break;
case 'n':
case 'N':
fever = 0;
break;
default:
printf("\n Invalid Entry.");
goto fever;
break;
}
How can I get this to work? It is required to use getchar for character input..
~Winter
|
|
|
05-08-2002, 09:39 PM
|
#2
|
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821
Rep:
|
Try:
Code:
while (ch = getchar())
{
/* switch or if condition */
}
Use a while loop. Using a goto is nothing but trouble.
|
|
|
05-09-2002, 09:06 AM
|
#3
|
Member
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184
Rep:
|
Useless observation:
The user has the BASIC fever? Y | y | yes | yessirebob | si
vfs.
|
|
|
05-09-2002, 04:42 PM
|
#4
|
Member
Registered: Apr 2002
Location: Michigan
Distribution: Slackware 8.0
Posts: 197
Rep:
|
maybe try this:
char cfever;
cfever = getchar();
|
|
|
05-10-2002, 03:37 PM
|
#5
|
LQ Newbie
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22
Original Poster
Rep:
|
thats cool!
I need to clear the buffer though right?
while( (getchar()) != '\n');
how soon is it possible for me to do this?
as soon as cfever has the char stored in it?
does get char only retrieve 1 character and store it in cfever, or will it take the whole YES, YuP no and NOpE and store that in cfever?
heres a few questions for now..
me
|
|
|
05-10-2002, 11:10 PM
|
#6
|
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821
Rep:
|
getchar() will only get one character at a time. You will need to call it in a loop to get more characters.
Unless you really want to use getchar() it may be easier to a string input function like fgets().
Code:
#include <stdio.h>
#define BUFF_SIZE 256
main()
{
char szInputBuffer[BUFF_SIZE + 1];
int iMatch = 0;
fgets( szInputBuffer, BUFF_SIZE, stdin );
if (( strncasecmp( "y", szInputBuffer, 1 ) == 0) ||
( strncasecmp( "yes", szInputBuffer, 3 ) == 0))
{
iMatch = 1;
}
printf("String is [%s]; iMatch is [%d]\n", szInputBuffer, iMatch );
}
|
|
|
05-11-2002, 02:49 AM
|
#7
|
Member
Registered: Apr 2002
Location: PRC
Posts: 32
Rep:
|
Re: getchar()
I try to modify you code.
Maybe it can work correctly!
/*********************** code ********************/
printf("\n Which of the following symptoms does"
"\n the patient have (Y for yes, N for no)"
"\n");
fever:
printf("\n Fever -------- ");
/* getchar(cfever) */
cfever = getchar(); /* input cfever */
getchar(); /* eat the "ENTER" character, this is important! */
switch (cfever) {
case 'y':
case 'Y':
fever = 1;
break;
case 'n':
case 'N':
fever = 0;
break;
default:
printf("\n Invalid Entry.");
goto fever;
break;
}
|
|
|
All times are GMT -5. The time now is 01:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|