-5
int main()
{
int program = 0;
while (program >= 0)
{
    printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");

    scanf("%d", &program);

    if (program == 0)
    {
        printf("Quitting the program...\n\n");
        return 0;
    }

    else if(program==1)
    {
        printf ("FIBONACCI SEQUENCE CALCULATOR");
    }
    else if(program==2)
    {
        printf("DECIMAL AND BINARY CALCULATOR");
    }

    else if(program==3)
    {
        printf("PRIME NUMBER CALCULATOR");
    }

    else
    {
        printf("ERROR");
    }
}

I want to print "ERROR" when the user input anything except 0,1,2 and 3, but this is the result when I input:

  • any number except 0,1,2,and 3: "ERROR" (this is correct)
  • any letter/symbol: "Quitting the program..."(this should be "ERROR"!)

Maybe this can help me to figure out the answer to my problem: What I've already know that %d is for scanning the integer and %c for scanning the character. But what do I have to use when I want to scan both of them?

Any help would be appreciated :)

hello
  • 13
  • 1
  • 4
  • `scanf` returns the number of matched parameters, in your case 0 or 1. – ext Jun 03 '16 at 20:40
  • Since you're using `%d`, if you enter anything that can't be interpreted as an integer (except whitespace) `scanf` will not update `program` and the old value will remain. The bad input will also remain in the stream to be read again later. – Dmitri Jun 03 '16 at 20:42
  • `scanf()` returns the number of fields successfully scanned, and leaves input it couldn't interpret in the stream so you can read it later... so you can try with `%d`, and if it doesn't return `1` you can try reading the input again with a different function or a different specifier. – Dmitri Jun 03 '16 at 21:29

2 Answers2

1

Exapmple One of the ways.

#include <stdio.h>
#include <stdlib.h>

void end_proc(void){
    printf("Quitting the program...\n\n");
    exit(0);
}

void input_error_proc(void){
    int ch;
    printf("ERROR\n");
    while((ch = getchar()) != '\n' && ch != EOF);//clear input
}

int main(void){
    int program;

    while(1){
        printf("\n"
            "Choose one of the following programs: \n\n"
            " (1) Fibonacci Sequence Calculator \n"
            " (2) Decimal and Binary Calculator \n"
            " (3) Prime Number Calculator \n"
            " \n"
            "If you want to exit the program, press (e or 0).\n"
            "Your choice: ");

        if(1==scanf("%d", &program)){//input number 
            if (program == 0){
                end_proc();
            } else if(program == 1){
                printf ("FIBONACCI SEQUENCE CALCULATOR");
            } else if(program==2) {
                printf("DECIMAL AND BINARY CALCULATOR");
            } else if(program==3) {
                printf("PRIME NUMBER CALCULATOR");
            } else {
                input_error_proc();
            }
        } else {//input not number
            char ch, check;
            if(2 == scanf(" %c%c", &ch, &check) && ch == 'e' && check == '\n')
                end_proc();
            else
                input_error_proc();
        }
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

I added a test for digit or char and I changed your program flow.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
    int program = 0;
    char c;
    for (; ;) {
        printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");

        scanf(" %c", &c);

        if (isalpha(c)) {
            printf("ERROR");
            continue;
        }

        else if (isdigit(c)) {

            program = atoi(&c);

            if (program == 0) {
                printf("Quitting the program...\n\n");
                return 0;
            }

            else if (program == 1) {
                printf("FIBONACCI SEQUENCE CALCULATOR");

            }
            else if (program == 2) {
                printf("DECIMAL AND BINARY CALCULATOR");
            }

            else if (program == 3) {
                printf("PRIME NUMBER CALCULATOR");
            }

            else {
                printf("ERROR");
            }

        }

    }
}

Test

Debug/gnu

Choose one of the following programs: 

 (1) Fibonacci Sequence Calculator 
 (2) Decimal and Binary Calculator 
 (3) Prime Number Calculator 

If you want to exit the program, press (e).
Your choice: 1
FIBONACCI SEQUENCE CALCULATOR
Choose one of the following programs: 

 (1) Fibonacci Sequence Calculator 
 (2) Decimal and Binary Calculator 
 (3) Prime Number Calculator 

If you want to exit the program, press (e).
Your choice: q
ERROR
Choose one of the following programs: 

 (1) Fibonacci Sequence Calculator 
 (2) Decimal and Binary Calculator 
 (3) Prime Number Calculator 

If you want to exit the program, press (e).
Your choice: 
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424