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.
Related
Hello I have a program in C that prints just two digit numbers to words but I have to modify this to make it print numbers between -99999,99999 to words without using any loops. I can easily do it by using a loop but I cannot use loops in this case. I am sharing my code for two digit numbers and I'd be glad if you can help me.
#include <stdio.h> // include stdio.h library
int main (void)
{
int num1, num2;
printf ("Enter a two-digit number: ");
scanf ("%1d%1d", &num1, &num2);
printf ("You have entered: ");
// print word for the first digit
switch (num1)
{
case 1:
// special case for numbers between 11-19
switch (num2)
{
case 0:
printf ("ten");
return 0;
case 1:
printf ("eleven");
return 0;
case 2:
printf ("twelve");
return 0;
case 3:
printf ("thirteen");
return 0;
case 4:
printf ("fourteen");
return 0;
case 5:
printf ("fifteen");
return 0;
case 6:
printf ("sixteen");
return 0;
case 7:
printf ("seventeen");
return 0;
case 8:
printf ("eigthteen");
return 0;
case 9:
printf ("nineteen");
return 0;
}
case 2:
printf ("twenty");
break;
case 3:
printf ("thirty");
break;
case 4:
printf ("forty");
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;
}
// print word for the second digit
switch (num2)
{
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;
}
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;
}
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");
}
}
When I compile this program, it outputs both the corresponding switch case and the default tag contents, the only value it doesn't print out for is January, any help would be great!
Sample Input: 4
Sample Output:
April
Error
Though I could make the program better, I need to have it this specific way.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
}
You just need an extra case to handle the stray '\n' that comes from you pressing enter at your terminal. January should work if you just type 1
Edit:Actually I just tested it on Linux and it works flawlessly, there might be some minor differences on Windows, though.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
// Filter out stray \n
case '\n':
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
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