0

I don't know why the code

scanf("%c",&eingabe);

everytime overleaps.

i try it with getchar too but same problem again.

I use linux but execute the code with xterm.

Anyone can help me?

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

int main()
{
    int z1,z2,erg=0;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    fflush(stdin); //clear

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    fflush(stdin);//clear

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");
    scanf("%c",&eingabe); //this is the line with the problem
    fflush(stdin); //clear

    switch(eingabe){
        case 'w':
        system("clear");
        break;
        case 'p':
        system("exit");
        break;
        default:
        printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }


    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
MAXLooW
  • 17
  • 3

3 Answers3

0

As @codaddict said here,

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a char from standard input there will be a newline ready to be read.

A simple solution is to exclude the newline character like this:

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

int main()
{
    int z1,z2,erg=0;
    char buf;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    scanf("%c", &buf);

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    scanf("%c", &buf);

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");
    scanf("%c",&eingabe); 

    switch(eingabe){
        case 'w':
            system("clear");
            break;
        case 'p':
            system("close");
            break;
        default:
            printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }  


    return 0;
}

OR replacing the ints scanfs with something like this: scanf("%d%*c",&n);,

THIS WAY:

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

int main()
{
    int z1,z2,erg=0;
    char buf;
    char eingabe;

    while(1){

        printf("Geben Sie die erste Zahl an: ");
        scanf("%d%*c",&z1);    //%*c removes the newline from the buffer

        printf("\nGeben Sie die zweite Zahl an: ");
        scanf("%d%*c",&z2);    //%*c removes the newline from the buffer

        erg=z1*z2; //works
        printf("\n%d * %d = %d",z1,z2,erg); //works

        printf("\n");
        printf("#######################");
        printf("\n");

        printf("Weiter = W\n");
        printf("Stop = P\n");

        printf("Eingabe: ");
        scanf("%c",&eingabe);    //no problem with this.

        switch(eingabe){
            case 'w':
            system("clear");
            break;
        case 'p':
            system("close");
            break;
        default:
            printf("\nEingabe Unbekannt");
        }

        printf("\n");

    }

    return 0;
}

And that's it my friend!

Community
  • 1
  • 1
UrbiJr
  • 133
  • 1
  • 2
  • 20
  • i try to replace the line with the error with your suggeestion "scanf("%d%*c",&n);" it dont work, possible i understand it false sry. – MAXLooW Dec 28 '16 at 14:08
  • @MAXLooW I updated the answer with the definitive code in order to avoid possible misunderstandings. Just copy and paste it. – UrbiJr Dec 28 '16 at 15:42
0
  1. as @UrbiJr rightly mentioned, solution would be to exclude your newline character. In order to do that, that you could also use getchar(). I ran your code with getchar() and it works, i have a Linux machine too, compiled using GCC and ran the binary in GNOME Terminal.
  2. Also, assuming you want to exit the program when you type the character 'p', are you sure case 'p': system("exit"); works on your machine? It did not work for me, so i used case 'p': exit(EXIT_SUCCESS); instead, and it worked.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int z1,z2,erg=0;
       char buf;
       char eingabe;
    
       while(1)
       {
        printf("Geben Sie die erste Zahl an: ");
        scanf("%d",&z1); //works
        getchar();
    
        printf("\nGeben Sie die zweite Zahl an: ");
        scanf("%d",&z2); //works
        getchar();
    
        erg=z1*z2; //works
        printf("\n%d * %d = %d",z1,z2,erg); //works
    
        printf("\n");
        printf("#######################");
        printf("\n");
    
       printf("Weiter = W\n");
       printf("Stop = P\n");
    
      printf("Eingabe: ");
      scanf("%c",&eingabe); //works 
    
       switch(eingabe){
       case 'w':
          system("clear");
          break;
       case 'p':
          exit(EXIT_SUCCESS);
          break;
       default:
          printf("\nEingabe Unbekannt");
            }
    
      printf("\n");
    
       }  
            return 0;
       }
    
Abel Tom
  • 145
  • 2
  • 8
  • with getchar before scanf it work thanks! system("exit") not work, but exit(EXIT_SUCCESS); works perfect. – MAXLooW Dec 28 '16 at 14:11
0

It causes your program will get '\n' ("Enter" or "newline" from the previous input) as the input. So, put "getchar();" before "scanf("%c",&eingabe);".

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

int main()
{
    int z1,z2,erg=0;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    fflush(stdin); //clear

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    fflush(stdin);//clear

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");

    getchar(); // this is the solution

    scanf("%c",&eingabe); //this is the line with the problem
    fflush(stdin); //clear

    switch(eingabe){
        case 'w':
        system("clear");
        break;
        case 'p':
        system("exit");
        break;
        default:
        printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }


    return 0;
}
Shiki-kan
  • 1
  • 2