![]() |
| | #1 |
| Registered User Join Date: Apr 2004
Posts: 19
| Code: #include <stdio.h>
#include <math.h>
main()
{
int count;
float ctr,a;
char x,s,S,c,C,q,Q;
do{
printf("(S)ine, (C)osine, or (Q)uit: ");
scanf("%c", &x);
if(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q')
printf(" -- Illegal Input!\n");
} while(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q');
}
Code: (S)ine, (C)osine, or (Q)uit: x -- Illegal Input! (S)ine, (C)osine, or (Q)uit: Code: (S)ine, (C)osine, or (Q)uit: x -- Illegal Input! (S)ine, (C)osine, or (Q)uit: -- Illegal Input! (S)ine, (C)osine, or (Q)uit: |
| n00by is offline | |
| | #3 |
| Registered User Join Date: Apr 2004
Posts: 19
| i still don't get it. what can i do/change/add to the code? |
| n00by is offline | |
| | #4 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| Well, a quick fix would be: Code: {
printf(" -- Illegal Input!\n");
while (getchar() != '\n');
}
Code: if (scanf("%c", &x) != 1)
{
/* Error */
}
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #5 |
| Gawking at stupidity Join Date: Jul 2004
Posts: 2,324
| Read the FAQ that viaxd linked for you. The first scanf() is leaving \n on the buffer so the next time through the loop it thinks you entered a blank line.
__________________ If you understand what you're doing, you're not learning anything. Ignore any "advice" esbo tries to give you. It's wrong. |
| itsme86 is offline | |
| | #6 |
| Registered User Join Date: Jul 2004
Posts: 101
| The problem is not necessarily with scanf, but character input in general. When you ask an interactive user to enter a single character, two are actually placed on the input stream. The first is the character that the user typed and the second is the newline character created when the user pressed Enter. Your code does not account for the newline character and simply treats it as normal input. A quick and dirty solution is to simply ask for another character from the stream and discard it. Code: #include <stdio.h>
#include <math.h>
int main()
{
char x;
do{
printf("(S)ine, (C)osine, or (Q)uit: ");
scanf("%c", &x);
getchar();
if(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q')
printf(" -- Illegal Input!\n");
} while(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q');
return 0;
}
Code: #include <stdio.h>
#include <math.h>
int main()
{
char x[BUFSIZ];
do{
printf("(S)ine, (C)osine, or (Q)uit: ");
if (fgets(x, sizeof x, stdin) == NULL) {
break;
}
if(x[0]!='s' && x[0]!='S' && x[0]!='c' && x[0]!='C' && x[0]!='q' && x[0]!='Q')
printf(" -- Illegal Input!\n");
} while(x[0]!='s' && x[0]!='S' && x[0]!='c' && x[0]!='C' && x[0]!='q' && x[0]!='Q');
return 0;
}
|
| Princeton is offline | |
| | #7 |
| Registered User Join Date: Apr 2002
Posts: 463
| to make the large if() and while() statements easier to read and work with, you may want to change them to a switch statement for x. |
| Draco is offline | |
| | #8 | |
| Registered User Join Date: Jul 2004
Posts: 101
| Quote:
![]() Code: #include <ctype.h>
#include <math.h>
#include <stdio.h>
#define PROMPT "(S)ine, (C)osine, or (Q)uit: "
int getOpt(const char *prompt);
int isValid(char item, const char *options);
int main()
{
int x;
while ((x = getOpt(PROMPT)) != EOF && !isValid(x, "SCQ")) {
printf(" -- Illegal Input!\n");
}
return 0;
}
int getOpt(const char *prompt)
{
char buffer[BUFSIZ];
fputs(prompt, stdout);
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
return EOF;
}
return buffer[0];
}
int isValid(char item, const char *options)
{
while (*options != '\0') {
if (*options == toupper((unsigned char)item)) {
return 1;
}
++options;
}
return 0;
}
| |
| Princeton is offline | |
| | #9 |
| Registered User Join Date: Apr 2004
Posts: 19
| Code: #include <stdio.h>
#include <math.h>
main()
{
int count;
float ctr,a;
char x,s,S,c,C,q,Q;
do{
do{
printf("(S)ine, (C)osine, or (Q)uit: ");
scanf("%c", &x);
if(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q')
printf(" -- Illegal Input!\n");
while(getchar() != '\n');
} while((x!='s') && (x!='S') && (x!='c') && (x!='C') && (x!='q') && (x!='Q'));
switch(x){
case 's':
case 'S':
printf(" Enter starting value: ");
scanf("%f", &a);
for(count = 0; count < 10; count++)
{
for(ctr = a; ctr < a+.09; ctr += 0.01)
printf("%1.3f ", sin(ctr));
a += .1;
printf("\n");
}
printf("%1.3f\n", sin(a));
break;
case 'c':
case 'C':
printf(" Enter starting value: ");
scanf("%f", &a);
for(count = 0; count < 10; count++)
{
for(ctr = a; ctr < a+.09; ctr += 0.01)
printf("%1.3f ", cos(ctr));
a += .1;
printf("\n");
}
printf("%1.3f\n", cos(a));
break;
}
} while(x!='q' && x!='Q');
printf(" -- Goodbye\n");
}
|
| n00by is offline | |
| | #10 |
| Registered User Join Date: Jul 2004
Posts: 101
| Try using the same solution after every call to scanf and see if the problem persists. scanf is a very complex function that is difficult to use properly even for experienced programmers. Once again I highly recommend tossing it in favor of fgets and a parsing scheme such as sscanf. |
| Princeton is offline | |
| | #11 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| Code: if(x!='s' && x!='S' && x!='c' && x!='C' && x!='q' && x!='Q')
{
printf(" -- Illegal Input!\n");
while(getchar() != '\n');
} [edit] Actually, that doesn't make a lot of difference. Here's the output I get from your code: Code: (S)ine, (C)osine, or (Q)uit: x -- Illegal Input! (S)ine, (C)osine, or (Q)uit: x -- Illegal Input! (S)ine, (C)osine, or (Q)uit: a -- Illegal Input! (S)ine, (C)osine, or (Q)uit: q -- Goodbye
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #12 | |
| Registered User Join Date: Apr 2004
Posts: 19
| Quote:
| |
| n00by is offline | |
| | #13 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| Do this after every scanf(): while(getchar() != '\n');
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #14 | |
| Registered User Join Date: Oct 2003
Posts: 719
| Quote:
PHP Code: cheers.
__________________ When the concrete cases are understood the abstractions are readily made Last edited by caroundw5h; 08-10-2004 at 08:46 PM. Reason: forgot to put an ex. | |
| caroundw5h is offline | |
| | #15 | |
| Im a Capricorn Join Date: Feb 2002
Posts: 192
| Quote:
Hi, this might be another alternative... Code: #include<stdio.h>
int main()
{
char ch;
do
{
printf("(S)ine (C)os (Q)uit : ");
scanf("%c%*c",&ch);
switch(ch)
{
case 's':
case 'S':
printf("Sine Function\n");
break;
case 'c':
case 'C':
printf("Cos Function\n");
break;
case 'q':
case 'Q':
exit(0);
default:
printf("Invalid Input\n");
}
}while(1);
return 0;
}
-Harsha.
__________________ Help everyone you can | |
| vsriharsha is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what is wrong in this simple code | vikingcarioca | C Programming | 4 | 04-23-2009 07:10 AM |
| what is wrong with this code please | korbitz | Windows Programming | 3 | 03-05-2004 10:11 AM |
| I cant find what is wrong with this code | senegene | C Programming | 1 | 11-12-2002 06:32 PM |
| Anyone see what is wrong with this code? | Wise1 | C Programming | 2 | 02-13-2002 02:01 PM |
| very simple code, please check to see whats wrong | Unregistered | C Programming | 3 | 10-10-2001 12:51 AM |