Why the program is terminated before the scanf

Asked

Viewed 130 times

1

I have the following code:

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

int main()
{
    float a, b, c, delt, c0, c1, c2;
    char s;
    int op = 1;

    printf("Welcome.\n\n");
    while (op == 1) {
        printf("Please type: a, b c.\n\n");

        printf("a:");
        scanf("%f",&a);
        printf("b:");
        scanf("%f",&b);
        printf("c:");
        scanf("%f",&c);

        delt = b*b;
        printf("%c \n",delt);

        printf("Do you want repeat? (y/n)");
        scanf("%c",&s);

        if (s != "y") {
            op = 0;
        }

    }
    return 0;
}

Why the last scanf() does not wait for the entrance to follow with the if and decide whether the proceedings may be closed?

I am using the GNU C Compiler copier if it is useful.

  • 1

    You are mixing types. If you use quotes ", you are working with string char*, while with apostrophe, the work is done at the character level char

2 answers

4


Double quotes instead of single.

if (s != "y") { op = 0; }

Do it:

if (s != 'y') { op = 0; }

When you want to read one character you can always use the getchar() which is specifically made for these types of cases.

I suggest you change the %c where you print the value of del for %.2f or simply %f because read data numbers like float, real. For the rest, in the part where you read the value of s to avoid capturing the buffer and invalidate the entry of y/n you can proceed as in @Lucas Martes' reply.

  • doubles: (string)
  • simple: (char)

To declare variables of type bool, just import the library, or you can set it manually using any of these examples.

#include <stdbool.h>

#define bool int
#define true 1
#define false 0 

typedef enum {true, false} bool;

#import <boolean.h> // já obsoleto nalguns compiladores

Then you can use bool var normally.

  • Is it possible to use Boolean instead of int variable to make this loop confirmation? I can’t declare it I’m totally used to PHP.

  • @Thomsontorvalds talks about the variable op ? And what would that variable be delt, seems misused too.

  • The Delt is to calculate the delta for Bhaskara. Yes I mean op.

  • 1

    @Thomsontorvalds is possible, but you must first import a file depending on the compiler or so in use as you see in the answer. If you have any other questions I recommend you create another one question, because if you continue to edit the answer, you will already have too much content that does not belong to the question in question.

2

There is an error in your program that Edilson commented on, but it doesn’t solve the problem of early termination. You should add a space to your scanf to ignore line break(and also any space) that was generated before pressing ENTER. Do so:

printf("Do you want repeat? (y/n)");
    scanf(" %c",&s);

Note the white space before the %c.

Also modify:

printf("%c \n",delt);

for

printf("%f \n",delt);

'Cause Delt is the float type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.