I'm making this program using switch statements that will assign letter grades based on if the user enters numbers 0 - 10. If the user enters a number that is not 0-10, the program outputs an error message and has the user re-enter. However, if the user enters a character the program will loop at the default case. I want it to output the error message from the default case once, and have them re-enter if they enter a character. I'm not sure as to why it loops the default case when a character is entered though.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
}
}
return 0;
}
Try something like:
#include <stdio.h>
int main()
{
int grade;
int r=0;
while(r != 1)
{
scanf("%i", &grade);
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
r=1
break;
case 8:
printf("Your grade is a B\n");
r=1
break;
case 7:
printf("Your grade is a C\n");
r=1
break;
case 6:
printf("Your grade is a D\n");
r=1
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
r=1
break;
default:
printf("Invalid score, please re-enter\n");
break;
}
}
return 0;
}
This will clear the input buffer on an invalid input and allow a retry.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
if ( r != 1) {//r == 1 is successful input of integer
grade = -1;//reset grade on invalid input
}
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
while ( getchar() != '\n');//clear input buffer
}
}
return 0;
}
The reason your code always loops is because there is no way to exit out of your while other than to kill the program. Remember that break only breaks out of the inner-most switch or loop.
The cleanest way to break out of multiple levels is to use a flag. One way to do what you want is like this:
bool valid_grade = false;
while(!valid_grade && (r = scanf("%i", &grade)) != EOF)
{
valid_grade = true;
switch(grade)
{
case 10:
// unchanged from your code
default:
valid_grade = false;
printf("Invalid score, please re-enter\n");
}
}
Related
When I enter the letter 'q' as grade, it runs infinitely.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int grade;
bool flag = true;
while (flag) {
puts("-----------------------------"); // comment
printf("What's your grade out of 10? ");
scanf(" %d", &grade);
switch (grade) {
case 10:
case 9:
case 8:
case 7:
case 6:
printf("Pass\n");
break;
case 5:
printf("Fail\n");
break;
case 4:
printf("Fail\n");
break;
case 3:
printf("Fail\n");
break;
case 2:
printf("Fail\n");
break;
case 1:
printf("Fail\n");
break;
case 0:
printf("Fail\n");
break;
default:
printf("Illegal Grade\n");
flag = false;
break;
}
}
return 0;
}
scanf(" %d",&grade);
It scans int in string. "q" is not int. When you enter "q", value of the variable grade left unchanged. You must check returned value of scanf to validate number of filled placeholders.
if (scanf(" %d",&grade) != 1) {
printf("Illegal Grade\n");
exit(1); // or break
}
Other parts are ok.
scanf(" %d", &grade); fails when you type something that cannot be parsed as into an integer. grade is not modified, so it is uninitialized if the conversion error happens immediately and the behavior is undefined, otherwise you get the same value and behavior as the previous time.
The offending input stays in the input stream, so the same thing happens when the code executes again in the while loop, hence the infinite loop.
You want to test if the conversion was successful and discard the input if not:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int res, c, grade;
bool flag = true;
while (flag) {
puts("-----------------------------"); // comment
printf("What's your grade out of 10? ");
res = scanf("%d", &grade);
if (res == EOF)
break;
if (res == 0) {
printf("Invalid input\n");
/* discard the offending line of input */
while ((c = getchar()) != EOF && c != '\n')
continue;
/* try again */
continue;
}
switch (grade) {
case 10:
case 9:
case 8:
case 7:
case 6:
printf("Pass\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Fail\n");
break;
default:
printf("Illegal Grade\n");
flag = false;
break;
}
}
return 0;
}
It was challenging to word the title for this question, but I hope you can forgive me as I will elaborate here.
The problem i'm having is my C program floods the console with "Enter the student's grade: F" if I enter anything other than an integer. I'm new to C, so I don't understand how to check if the input is of the valid type.
int main() {
int grade; //number 0-10 associated with the letter grade
while (1) {
printf("Enter the student's grade: ");
scanf("%i", &grade);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}
Thanks for the help!
Check if scanf succeeded.
int nread = scanf("%i", &grade);
if (nread != 1) // scanf failed, start cleanup
{
scanf("%*[^\n]%*c");
}
Look for any book and you'll know that scanf returns number of elements that were successfully read, so if you type a letter, it will fail to read an integer and return 0 (nothing read), then you can know there's something wrong and discard wrong stuffs.
Since scanf don't read anything if it encounters an error, the wrong stuff will remain in the input buffer, and badly it'll break the next scanf, leading to an infinite output flushing.
P.S. You don't need to repeat the statements after case 5 4 3..., just merge them into one:
case 5: // Remove these and leave the last one there
case 4:
case 3:
case 2:
case 1:
case 0: printf("F \n"); break;
Check the return of scanf. 1 means an integer was successfully scanned. 0 means the input was not an integer. Clean the input stream and try again.
#include <stdio.h>
int main ( void) {
int grade; //number 0-10 associated with the letter grade
int valid = 0;
while (1) {
do {
printf("Enter the student's grade: ");
if ( 1 != ( valid = scanf("%i", &grade))) {// 1 is success
if ( EOF == valid) {
printf ( "found EOF\n");
return 0;
}
while ( '\n' != getchar ( )) {//clear input stream
}
}
} while ( !valid);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}
From the book "Programming in C"
Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display
nine three two
Remember to display “zero” if the user types in just a 0.
Its been hours and its still cant be solved.. Do anyone know how to? This is the code so far
#include <stdio.h>
int right_digit,number;
int main ()
{
scanf("%i",&number);
right_digit = number % 10;
switch (right_digit)
{
case '0':
printf("0");
break;
case '1':
printf("one");
break;
case '2':
printf("two");
break;
case '3':
printf("three");
break;
case '4':
printf("four");
break;
case '5':
printf("five");
break;
case '6':
printf("six");
break;
case '7':
printf("seven");
break;
case '8':
printf("eight");
break;
case '9':
printf("nine");
break;
default:
break;
}
number = number / 10;
return 0;
}
The first problem here is, you're (wrongly) trying to use the character representation of the integer numbers. In your code, right_digit is supposed to represent an integer digit, not a character literal.
You must not to use the ''s, just write
case 0:
...
case 1:
and so on.
Just to add a bit on your mistake, it was considering the corresponding integer values of the character literal '0', '1' and so on. For ASCII, they are equivalent to
case 48:
case 49:
.
.
which is not what you intended.
That said,
You need to put the modulo calculation and switch-case inside a loop and carry out the conversion for all the digits of the input integer.
You need to start printing from the beginning (MSB), currently , you're printing from LSB. (Hint: Start printing the result of the modulo operation)
printf("0"); should be printf("Zero ");, as per the requirement.
/*USING SWITCH CASE ...ALSO YOU CAN USE '0' and negative numbers */
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int rem,num,sum=0,rem1,num1,add;
printf("enter the number:\n");
scanf("%i",&num);
if(num<0)
{
printf("minus ");
num=-num;
}
if(num==0)
{
printf("zero");
}
while(num!=0)
{
rem=num%10;
num=num/10;
sum=sum*10 +rem;
}
/*printf("%i\n",sum);*/
while(sum!=0)
{
rem1=sum%10;
sum=sum/10;
switch(rem1)
{
case 0:
printf("zero ");
break;
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
default:
printf("invalid no");
}
}
return 0;
}
Hope this program will help you to understand the logic and I am also posting the solution for same problem using switch case....
/* Write a program that takes an integer keyed in from
* the terminal and extracts and displays each digit of the
* integer in English. So, if the user types in 932, the
* program should display >>> nine three two <<<.
* (Remember to display “zero” if the user types in
* just a 0.)
*/
/*USING IF-ELSE IF*/
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int rem,num,sum=0,rem1;
printf("enter the number:\n");
scanf("%i",&num);
if(num<0)
{
printf("minus ");
num=-num;
}
if(num==0)
{
printf("zero");
}
while(num!=0)
{
rem=num%10;
num=num/10;
sum=sum*10 +rem;
}
/*printf("%i\n",sum);*/
while(sum!=0)
{
rem1=sum%10;
sum=sum/10;
if(rem1==0)
{
printf("zero ");
}
else if(rem1==1)
{
printf("one ");
}
else if(rem1==2)
{
printf("two ");
}
else if(rem1==3)
{
printf("three ");
}
else if(rem1==4)
{
printf("four ");
}
else if(rem1==5)
{
printf("five ");
}
else if(rem1==6)
{
printf("six ");
}
else if(rem1==7)
{
printf("seven ");
}
else if(rem1==8)
{
printf("eight ");
}
else if(rem1==9)
{
printf("nine ");
}
else
{
printf("invalid no");
}
}
return 0;
}
newcomer to C here.
I seem to have run afoul on a problem that I have to do. The goal here is to make a program that writes out a two-digit number in words. Alternatively, the user can enter "Q" to quit. This is the bit that I am having trouble with. Can anyone point out what I am doing wrong?
#include <stdio.h>
int main(void)
{
int digit_one;
int digit_two;
enum state {fail, quit};
int status = fail;
printf("Enter a two-digit number or press Q to quit: ");
scanf("%1d%1d",&digit_one,&digit_two);
if(digit_one == 'Q'){
status = quit;
}
else {
if (digit_one == 1) {
switch(digit_two % 10) {
case 0: printf("You entered: Ten"); break;
case 1: printf("You entered: Eleven"); break;
case 2: printf("You entered: Twelve"); break;
case 3: printf("You entered: Thirteen"); break;
case 4: printf("You entered: Fourteen"); break;
case 5: printf("You entered: Fifteen"); break;
case 6: printf("You entered: Sixteen"); break;
case 7: printf("You entered: Seventeen"); break;
case 8: printf("You entered: Eighteen"); break;
case 9: printf("You entered: Ninteen"); break;
}
return 0;
}
switch(digit_one % 10) {
case 2: printf("You entered: Twenty-"); break;
case 3: printf("You entered: Thirty-"); break;
case 4: printf("You entered: Forty-"); break;
case 5: printf("You entered: Fifty-"); break;
case 6: printf("You entered: Sixty-"); break;
case 7: printf("You entered: Seventy-"); break;
case 8: printf("You entered: Eighty-"); break;
case 9: printf("You entered: Ninety-"); break;
}
switch(digit_two % 10) {
case 0: break;
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
case 4: printf("Four"); break;
case 5: printf("Five"); break;
case 6: printf("Six"); break;
case 7: printf("Seven"); break;
case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
}
return 0;
}
}
You can't read Q into an integer variable. Read the input into a character one.
Problem is that your scanf reads a string and converts it to integer immediately (thats what %d does).
You need to first read only a string (with %s or similar) to a buffer and then check that it is not 'Q'. After that you can do the conversion to integer with sscanf.
See http://en.cppreference.com/w/c/io/fscanf
Edit: Also, your code completely ignores the quit status.
What I am trying to do is to write a program wherein you input two digits and
then they are converted into words which gets printed. The problem is that this program
hangs after you input the two digits and I have no idea why. Any and all help is appreciated.
I am a beginner and all I can use to solve this is basically if and switch. Thanks again.
#include <stdio.h>
int main (void)
{
int firstNum, secondNum;
printf("Enter a two digit number: ");
scanf("%d%d", &firstNum,&secondNum);
if (firstNum == 1 && secondNum == 0){
printf("You entered the number ten\n");}
if (firstNum == 1 && secondNum == 1){
printf("You entered the number eleven\n");}
if (firstNum == 1 && secondNum == 2){
printf("You entered the number twelve\n");}
if (firstNum == 1 && secondNum == 3){
printf("You entered the number thirteen\n");}
if (firstNum == 1 && secondNum == 4){
printf("You entered the number forteen\n");}
if (firstNum == 1 && secondNum == 5){
printf("You entered the number fifteen\n");}
if (firstNum == 1 && secondNum == 6){
printf("You entered the number sixteen\n");}
if (firstNum == 1 && secondNum == 7){
printf("You entered the number seventeen\n");}
if (firstNum == 1 && secondNum == 8){
printf("You entered the number eighteen\n");}
if (firstNum == 1 && secondNum == 9){
printf("You entered the number nineteen\n");}
switch(firstNum){
case 2: printf("You entered the number twenty-");break;
case 3: printf("You entered the number thirty-");break;
case 4: printf("You entered the number forty-");break;
case 5: printf("You entered the number fifty-");break;
case 6: printf("You entered the number sixty-");break;
case 7: printf("You entered the number seventy-");break;
case 8: printf("You entered the number eighty-");break;
case 9: printf("You entered the number ninty-");break;
}
switch (secondNum){
case 1: printf("one.\n");break;
case 2: printf("two.\n");break;
case 3: printf("three.\n");break;
case 4: printf("four.\n");break;
case 5: printf("five.\n");break;
case 6: printf("six.\n");break;
}
return 0;
}
Your program "hangs" because it is waiting for a second number.
Instead of 42ENTER, type 42fooENTER.
You need to verify the return value from scanf()
if (scanf("%d%d", &firstNum, &secondNum) != 2) {
fprintf(stderr, "Oops, the scanf didn't read 2 numbers.\n");
} else {
/* continue with program */
/* you might as well see what scanf got from the input */
printf("scanf got the values %d and %d.\n", firstNum, secondNum);
}
A very simple code (Specially for beginners).
#include <stdio.h>
#include <conio.h>
int main()
{
int num,n,r;
printf("Enter a two-digit number: ");
scanf("%d",&num);
n = num/10;
r = num%10;
switch(n)
{
case 1: switch(r)
{
case 0: printf("Ten");
break;
case 1: printf("Eleven");
break;
case 2: printf("Twelve");
break;
case 3: printf("Thirteen");
break;
case 4: printf("Fourteen");
break;
case 5: printf("Fifteen");
break;
case 6: printf("Sixteen");
break;
case 7: printf("Seventeen");
break;
case 8: printf("Eighteen");
break;
case 9: printf("Nineteen");
}
break;
case 2: printf("Twenty-");
break;
case 3: printf("Thirty-");
break;
case 4: printf("Fourty-");
break;
case 5: printf("Fifty-");
break;
case 6: printf("Sixty-");
break;
case 7: printf("Seventy-");
break;
case 8: printf("Eighty-");
break;
case 9: printf("Ninety-");
break;
}
if(n != 1)
{
switch(r)
{
case 1: printf("one");
break;
case 2: printf("two");
break;
case 3: printf("three");
break;
case 4: printf("four");
break;
case 5: printf("five");
break;
case 6: printf("six");
break;
case 7: printf("seven");
break;
case 8: printf("eight");
break;
case 9: printf("nine");
break;
default: ;
}
}
getch();
}
Your program reads data from the console and the console is in "cooked" mode. In this mode, the console collects the input and allows the user to edit it. Data is sent to the program after you press Return or Enter.
The mode you want is "raw" mode. There are various ways to enter raw mode but that depends on your OS.
The quick fix is to enter the two digits and press Return