Error from "expected Expression before ːint'"

Asked

Viewed 1,172 times

0

I was following this example, but it was done on Windows, so it was used <conio.h>. When I try to run on Linux I get 2 errors: an *h=int(tempo);, that I don’t understand why, and the other in the getch. How can I replace them?

#include <stdio.h>

void horario(float tempo, int *h, int *m)
{
    *h=int(tempo);
    *m=(tempo-*h)*60;
}

int main ()
{
    int hora,minuto;
    char resp;
    float t;

    do
    {
        printf("Digite o horario na forma centesimal: ");
        scanf("%f",&t);
        horario(t,&hora,&minuto);
        printf("Horario: %02d:02%d\n",hora,minuto);
        printf("Quer calcular outro horario? (S/N):");
        resp=getch();
    } while (resp=='S');
    getch();
    return 0;
}
  • 2

    This is the Stackoverflow in Portuguese. Please use this language when asking questions, not English.

2 answers

1

To make a "cast" use the desired type between parentheses followed by the value

(int)tempo // cast de tempo para tipo int: correcto
int(tempo) // incorrecto: aparenta ser a chamada da funcao int com argumento tempo

You can replace the getch() for getchar() (prototype in <stdio.h> that you’ve already included).

  • a yes, I confused even, thank you very much. you know a function that replaces getch on linux?

  • 1

    Note that int(valor) works in C++. Maybe the code has been compiled in C++ mode in Windows?

  • no, I think it was just a typo, this was an example given in class, and we used C.

1


You had two problems:

  1. Typecast badly done.
  2. getchar instead of getch

    void horario(float tempo, int *h, int *m){
        *h=(int)tempo;
        *m=(tempo-*h)*60;
    }
    
    int main(){
        int hora,minuto;
        char resp;
        float t;
        do
        {
            printf("Digite o horario na forma centesimal: ");
            scanf("%f",&t);
            horario(t,&hora,&minuto);
            printf("Horario: %02d:02%d\n",hora,minuto);
            printf("Quer calcular outro horario? (S/N):");
            getchar();
            resp = getchar();
        } while (resp=='S');
        getchar();
        return 0;
    }
    
  • When I run the program, "do ... while" is not starting again, it will be some problem with getchar?

  • 1

    Yes. The getchar() gets the ENTER from scanf() previous. A rough solution will be doings 2 getchar() followed: one to consume ENTER and one to catch the answer.

  • 1

    Or use a scanf("%c", &Resp);

  • got it, I think for this example I’ll use the same scanf, thank you very much.

  • I’ve already edited my code, as @pmg said it has to use two getchar() or else a scanf("%s") I think it also works.

  • In the scanf put a space to ignore any n existing in the input buffer: scanf(" %c", &Resp);

Show 1 more comment

Browser other questions tagged

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