0

I've been trying to make a small calculator program to test my own skills, due to me being pretty green when talking about coding. Anyways I'm trying to make a function with a loop that will ask for a number until a number is inputted and reject letters.

This is what my limited understanding/googling produced:

int is_number(int num){
    do {
        printf("\n:");
        scanf("%d",&num);
        if( isalpha(num)){
            printf("\nYou entered a letter, please input a number\n");
            continue;
        } else {
            printf("Number accepted...\n");
            break;
        }
    } while(isalpha(num));
    return num;
}

The only problem is that it crashes if given a letter, but there is progress... no infinitive spam of same message.

The next code is the whole code not just a snippet:

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

//Records the answer for Y/N question and checks if answer is Y or No

char check_yesno_question(char a){

    do{
        printf("\n:");
        scanf(" %c",&a);
        if((a=='Y')||(a=='N')||(a=='y')||(a=='n')){
            break;
        }
        else{
            printf("\nIncorrect answer, please input Y or N (Y = Yes, N = No)\n");
            continue;
        }
    }while((a!='Y')&&(a!='N')&&(a!='y')&&(a!='n'));

    return a;

}
//My sad attempt to test if number given is a letter

int is_number(int num){

    do{
        printf("\n:");
        scanf("%d",&num);
        if( isalpha(num)){
            printf("\nYou entered a letter, please input a number\n");
            continue;
        }
        else{
            printf("Number accepted...\n");
            break;
        }
    }while(isalpha(num));
    return num;
}

int main(){

    char YesNo1;
    int start;
    int StartWait=500;
    //char Choice;
    int number1;
    int number2;
    int number3;
    int number4;

    printf("Welcome to simple calculator 0.01\n");
    printf("At this stage calculator supports only 4 numbers\n");
    printf("Would you like to start simple calculator?\n");
    printf("Y/N?\n");                                            

    //Calls for check_yesno_question

    YesNo1=check_yesno_question(YesNo1);

    //Checks if answer is yes proceed, if answer is no, return 0;

    if((YesNo1=='y')||(YesNo1=='Y')){
        printf("Awnser = Yes\n");
        printf("Starting up the calculator");
        for(start=0;start<3;start++){
            printf(".");
            Sleep(StartWait);
        }
        printf("\n");
    }
    else{
        printf("Awnser = No\n");
        printf("Turning of the calculator");
        for(start=0;start<3;start++){
            printf(".");
            Sleep(StartWait);
        }
        printf("\n");
        return 0;
    }
    //Me wanting to make things look bit more clean

    system("cls");

    printf("---------------CALCULATOR---------------\n");
    //Calls for is_number
    number1=is_number(number1);

    return 0;
}

As you seen it is unfinished. I'm basically done looking for answers myself. When this was posted my time was 4:04am

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 2
    You should study [the `scanf` function (and family)](http://en.cppreference.com/w/c/io/fscanf) more carefully. Like what it *returns*. – Some programmer dude Jul 31 '18 at 01:08
  • 1
    Also, if I understand your problem correctly, perhaps something like this would help: https://stackoverflow.com/questions/1478932/check-if-user-inputs-a-letter-or-number-in-c – Alerra Jul 31 '18 at 01:10
  • @Someprogrammerdude maybe I actually do ;D –  Jul 31 '18 at 01:11
  • Anyways it's 4am I should go to bed and try this tomorrow on a fresh head, because right now... I can't even read my own code well... –  Jul 31 '18 at 01:16
  • 1
    Need to study or already check? In the code you show you do not check, and `num` will *never* be an alphabetic character (unless the input number actually matches the encoding of one (for example you input `97` which happens to match the [ASCII](http://en.cppreference.com/w/c/language/ascii) encoded character `'a'`), or you passed a character into the function, which reminds me: Why do you pass `num` as an argument? It could be just a local variable). – Some programmer dude Jul 31 '18 at 01:26
  • 3
    Why do you specifically want to know if it is a letter? It seems like what you actually want to know if it is a number or not, and if it is not you don't care if it is a letter, special character or something else? – klutt Jul 31 '18 at 01:47
  • regarding: `scanf("%d",&num);` this will NEVER input anything but a leading `-` or `+` and some number of digits. So you should: check the returned value (not the parameter value) to assure the operation was successful And if not successful, then call `getchar()` to consume the offending letter and repeat until successful – user3629249 Aug 01 '18 at 00:03

1 Answers1

2

From the context it seems pretty obvious that you're not interested in whether it is a letter or not. You're only interested in knowing if it is a number or not. If it is not a number, you don't care if it is a letter, special character or something else.

Assuming above, you are taking the wrong approach and you are also using scanf incorrectly. scanf("%d", &num) will always read a number into num, provided that the reading succeeded. What you need to do is check if the reading succeeded.

do{
    printf("\n:");
    if(scanf("%d",&num) != 1) { // If we did not perform a successful read
        printf("\nRead failed. Please input a number\n");
        continue;
    } else {
...
klutt
  • 30,332
  • 17
  • 55
  • 95
  • After implementing this code it gave me infinitive spam of _Read failed. Please input a number_ when non number was inputted. I have no clue what to put in to the `while()`. I tried `while(scanf("%d",&num) != 1);`, but that didn't work. There where other attempts for what to put into `while()`, but they all failed in the same way. Also I really must have it as a loop. Other thing is that I have no idea why that ´!= 1´ is there... –  Jul 31 '18 at 14:41
  • I found that this works if it's not a loop, but if I try to make it a loop it fails and does infinitive spam. I tried different ways to make a loop out of it, `do...while`, `while` and `goto` and none of the succeeded. They either crash, loop until infinity –  Jul 31 '18 at 15:47
  • @Potato Sorry to hear that. In that case, please post a new question about it, but make sure to make the question completely clear. Especially include the input that causes a fail. – klutt Aug 01 '18 at 04:47