LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-08-2002, 06:48 PM   #1
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Rep: Reputation: 15
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
 
Old 05-08-2002, 09:39 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Try:

Code:
while (ch = getchar())
{
   /* switch or if condition */

}
Use a while loop. Using a goto is nothing but trouble.
 
Old 05-09-2002, 09:06 AM   #3
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Useless observation:

The user has the BASIC fever? Y | y | yes | yessirebob | si



vfs.
 
Old 05-09-2002, 04:42 PM   #4
Sfin
Member
 
Registered: Apr 2002
Location: Michigan
Distribution: Slackware 8.0
Posts: 197

Rep: Reputation: 30
maybe try this:

char cfever;

cfever = getchar();
 
Old 05-10-2002, 03:37 PM   #5
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Original Poster
Rep: Reputation: 15
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
 
Old 05-10-2002, 11:10 PM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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 );
}
 
Old 05-11-2002, 02:49 AM   #7
wwnn1
Member
 
Registered: Apr 2002
Location: PRC
Posts: 32

Rep: Reputation: 15
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;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() satellite Programming 10 12-24-2017 02:47 PM
getchar() question h/w Programming 3 12-23-2003 05:53 PM
about getchar() captainstorm Programming 6 10-11-2003 05:14 AM
getchar() NSKL Programming 18 01-11-2003 11:19 AM
getchar() not working? lackluster Programming 3 06-24-2002 09:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration