user5715195
user5715195

Reputation:

how to check if user input is a certain character

I am trying to check if the user inputs y or something else.

I have tried creating a string and looping through what the user inputs, but that doesn't work.

char answer[] = "n";
for(int i = 0; i < sizeof(answer)/4; i++) {
    if(answer[i] == "y") {
       calculatorPrompt();
    } else if(answer[i] === "n") {
              printf("Okay, bye!");
              System(100);
    }
}

This is my code (I'm sure it crashes on the if statement):

printf("Thanks for that\nDo you want a calculator?(y/n)");
char answer = 'n';
scanf("%s", answer);
    if(answer == 'y') {
        calculatorPrompt();
    } else if(answer == 'n') {
        printf("Okay bye!");
        Sleep(100); //wait for 100 milliseconds
    }

calculatorPrompt() function:

void calculatorPrompt() {
int a = 0;
int b = 0;
int sum = 0;
printf("Enter your first number: ");
if(scanf("%d\n", a) != 1) {
    checkNumber();
} else {
    printf("Enter your second number: ");
    if(scanf("%d\n", b) != 1) {
        checkNumber();
    } else {
        sum = calculate(a, b);
        printf("Your answer is: %d", sum);
    }
}

}

calculate() function:

int calculate(int a, int b) {
    return a + b;
}

checkNumber() function:

void checkNumber() {
    printf("Really? You didn't enter a number... Now exiting..");
    return;
}

I have included <windows.h> <stdio.h> and <stdbool.h>

I'm also confused as to why it crashes.

The return value of the program is -1,073,741,819.

Upvotes: 0

Views: 7006

Answers (4)

Cherubim
Cherubim

Reputation: 5467

You have multiple issues with scanf() statements in the code :

  • in calculatorPrompt() funtion of your code, you use :

    if(scanf("%d\n", a) != 1) //wrong : sending variable as argument
    
  • This is wrong because you need to send address of the variable as the argument not the variable itself as argument.

    if(scanf("%d", &a) != 1) //correct : sending address as argument
    
  • similarly change while scanning other integers in the code.


here,

char answer = 'n';
scanf("%s", answer);
  • As you are using the wrong format specifier, this invokes Undefined behavior.

  • here since answer is a char so, instead use :

    scanf(" %c", &answer); //space to avoid white spaces
    

and as I've already suggested in the comments :

  • You use i < sizeof(answer)/4 in the for loop

No! it must be i < sizeof(answer), as in a string every element occupies only 1 byte not 4 (you are mistaking it for an int array)

by the way you don't have any strings in your code

Upvotes: 2

Twinkle
Twinkle

Reputation: 524

When you scan a character , you just need to use %c. If you are planning to continue with string you must use strcmp() for comparison not ==.

Upvotes: 0

BLUEPIXY
BLUEPIXY

Reputation: 40155

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

void calculatorPrompt(void);

int main(void){
    printf("Thanks for that\nDo you want a calculator?(y/n)");
    char answer = 'n';
    scanf("%c", &answer);//scanf need address of store place
    if(answer == 'y') {
        calculatorPrompt();
    } else if(answer == 'n') {
        printf("Okay bye!\n");
        Sleep(100); //wait for 100 milliseconds
    }
    return 0;
}

void checkNumber(void);
int calculate(int a, int b);

void calculatorPrompt() {
    int a = 0;
    int b = 0;
    int sum = 0;
    printf("Enter your first number: ");
    if(scanf("%d", &a) != 1) {//\n : skip white spaces and wait input not spaces
        checkNumber();//call when invalid input
    } else {
        printf("Enter your second number: ");
        if(scanf("%d", &b) != 1) {
            checkNumber();
        } else {
            sum = calculate(a, b);
            printf("Your answer is: %d\n", sum);
        }
    }
}

void checkNumber(void){//output message and clear input.
    fprintf(stderr, "invalid input!\n");
    scanf("%*[^\n]%*c");//clear upto end of line.
}

int calculate(int a, int b) {
    return a + b;
}

Upvotes: 0

Kevin
Kevin

Reputation: 941

I don't recommend the code you have written for calculator, yet wanted to help you find the working code. Try following code that is based on your own code. Hope you'll see the differences and understand the reasons why the program was crashing in your case.

#include <Windows.h>
#include <stdio.h>
#include <stdbool.h>
bool checkNumber(int num)
{
    return true;
}


int calculate(int a, int b) {
    return a + b;
}

void calculatorPrompt() {
    int a = 0;
    int b = 0;
    int sum = 0;
    printf("Enter your first number: ");
    scanf_s("%d", &a);
    if (checkNumber(a)) {
    }

    printf("Enter your second number: ");
    scanf_s("%d", &b);
    if (checkNumber(b)) {
    }

    sum = calculate(a, b);
    printf("Your answer is: %d", sum);

}

int main()
{
    printf("Thanks for that\nDo you want a calculator?(y/n)");
    char answer = 'n';
    scanf_s("%c", &answer);
    if (answer == 'y') {
        calculatorPrompt();
    }
    else if (answer == 'n') {
        printf("Okay bye!");
        Sleep(100); //wait for 100 milliseconds
    }
}   

Upvotes: 0

Related Questions