Thread: I need help with switch case

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    2

    Question I need help with switch case

    I have the problem that after the first run of the addieren() function the default case is triggered and the result of the calculation disappears.

    Sorry for my bad english.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void wurzel();
    void addieren();
    void subtrahieren();
    void dividieren();
    void multiplizieren();
    void menue();
    void rechenOperator(char rOperator);
    
    
    int main()
    {
        menue();
        char eingabeOperator=NULL;
        printf("\nSchreibe den gewuenschten Rechen-Operator: ");
        eingabeOperator=getchar();
    
    
        rechenOperator(eingabeOperator);
    }
    
    
    void menue(){
        printf("\n----- Taschenrechner-Menue -----\n");
        printf("Schreibe 'S' oder 's' um den Rechner zu verlassen \n");
    
    
        printf("Schreibe + Symbol fuer Addition \n");
        printf("Schreibe - Symbol fuer Subtraktion \n");
        printf("Schreibe * Symbol fuer Multiplikation \n");
        printf("Schreibe / Symbol fuer Division \n");
        printf("Schreibe 'W' oder 'w' fuer Wurzel \n");
    }
    
    
    void rechenOperator(char rOperator) {
    
    
            switch(rOperator) {
                case'+':addieren();
                        break;
    
    
                case'-':subtrahieren();
                        break;
    
    
                case'*':multiplizieren();
                        break;
    
    
                case'/':dividieren();
                        break;
    
    
                case's':exit(0); break;
    
    
                case'S':exit(0); break;
    
    
                case'w':wurzel();
                        break;
    
    
                case'W':wurzel();
                        break;
    
    
                default:system("cls");
                        printf("Du hast keine korrekte Auswahl eingegeben.\n");
                        main();
                         break;
            }
    
    
    }
    
    
    void addieren(){
        float Summand1, Summand2, ergebnis;
        printf("\nErster Summand: ");
        scanf("%f", &Summand1);
        printf("\nZweiter Summand: ");
        scanf("%f", &Summand2);
    
    
        ergebnis = Summand1 + Summand2;
    
    
        system("cls");
        printf("\n%f + %f = %f\n", Summand1, Summand2, ergebnis);
        main();
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    726
    First things first: don't call main(). It's not necessary. If you want to return to the function that called your function, use a return statement.

    Second, what do you see if you print out the character that the user types in (as a debugging technique)? Hint: you should remove (or comment out) the system("cls") statements so you can see the previous output.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    133
    I see it's working in linux environment.

    I made one change of commenting all except addition, to run with out compilation issue.

    Also you make use of return even in main().

    Though main can be called from any where in the program ,best practice is not to use that as user defined function.

    output is as below

    Code:
    ./a.out 
    
    ----- Taschenrechner-Menue -----
    Schreibe 'S' oder 's' um den Rechner zu verlassen
    Schreibe + Symbol fuer Addition
    Schreibe - Symbol fuer Subtraktion
    Schreibe * Symbol fuer Multiplikation
    Schreibe / Symbol fuer Division
    Schreibe 'W' oder 'w' fuer Wurzel
    
    Schreibe den gewuenschten Rechen-Operator: +
    
    Erster Summand: 10.01
    
    Zweiter Summand: 20.02
    sh: cls: command not found
    
    10.010000 + 20.020000 = 30.030001
    
    ----- Taschenrechner-Menue -----
    Schreibe 'S' oder 's' um den Rechner zu verlassen
    Schreibe + Symbol fuer Addition
    Schreibe - Symbol fuer Subtraktion
    Schreibe * Symbol fuer Multiplikation
    Schreibe / Symbol fuer Division
    Schreibe 'W' oder 'w' fuer Wurzel
    
    sh: cls: command not found
    Schreibe den gewuenschten Rechen-Operator: Du hast keine korrekte Auswahl eingegeben.
    
    ----- Taschenrechner-Menue -----
    Schreibe 'S' oder 's' um den Rechner zu verlassen
    Schreibe + Symbol fuer Addition
    Schreibe - Symbol fuer Subtraktion
    Schreibe * Symbol fuer Multiplikation
    Schreibe / Symbol fuer Division
    Schreibe 'W' oder 'w' fuer Wurzel
    
    Schreibe den gewuenschten Rechen-Operator: +
    
    Erster Summand: 30.01
    
    Zweiter Summand: 40.02
    sh: cls: command not found
    
    30.010000 + 40.020000 = 70.029999
    
    ----- Taschenrechner-Menue -----
    Schreibe 'S' oder 's' um den Rechner zu verlassen
    Schreibe + Symbol fuer Addition
    Schreibe - Symbol fuer Subtraktion
    Schreibe * Symbol fuer Multiplikation
    Last edited by vlrk; 02-13-2020 at 05:24 AM.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    One tip not related to your problem: Don't use 'system("cls");'. It is DOS/Windows specific and dangerous (what if someone put a cls.exe in the directory with a virus?)...

    Here's a clearscreen.c for you: Works on Unix terminals who accept ANSI codes and on Windows as well (using GCC):

    Code:
    /* console.h */
    #ifndef CONSOLE_H_INCLUDED__
    #define CONSOLE_H_INCLUDED__
    
    void clearscreen( void );
    void gotoxy( int, int );
    
    #endif
    Code:
    /* console.c */
    #if defined(__linux) || defined(__APPLE__)
    // Linux or MacOS
    
    #include <stdio.h>
    
    /* Uses ANSI codes */
    void clearscreen(void) { fputs("\x1b[2J", stdout); }
    
    // coordinates start at (0,0).
    void gotoxy(int x, int y) { printf("\x1b[%d;%dH", ++x, ++y); }
    
    #else
    
    #include <windows.h>
    /* Uses Windows API */
    
    void clearscreen(void)
    {
      HANDLE hConsole;
      COORD coord = { 0 };
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      DWORD dwConsoleSize, dwWriten;
      
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      GetConsoleScreenBufferInfo(hConsole, &csbi);
      dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
      
      FillConsoleOutputCharacter(hConsole, (TCHAR)' ', 
                                 dwConsoleSize, coord, &dwWriten);
      FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                 dwConsoleSize, coord, &dwWriten);
    
      SetConsoleCursorPosition(hConsole, coord);
    }
    
    void gotoxy(int x, int y)
    {
      HANDLE hConsole;
      COORD coord = { .X = x, .Y = y };
      
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleCursorPosition(hConsole, coord);
    }
    
    #endif
    Now you can use 'clearscreen();'.
    Last edited by flp1969; 02-13-2020 at 06:08 AM.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    269
    Code:
    include <stdio.h>
    #include <stdlib.h>
     
    void addieren();
    void menue();
    void get();
    void rechenOperator(char rOperator);
     
    int main() {
        menue(); get();
    }
     
    void menue(){
        printf("\n----- Taschenrechner-Menue -----\n");
        printf("Schreibe 'S' oder 's' um den Rechner zu verlassen \n");
        printf("Schreibe + Symbol fuer Addition \n");
        printf("Schreibe - Symbol fuer Subtraktion \n");
        printf("Schreibe * Symbol fuer Multiplikation \n");
        printf("Schreibe / Symbol fuer Division \n");
        printf("Schreibe 'W' oder 'w' fuer Wurzel \n");
    }
    
    void get() {
        printf("\nSchreibe den gewuenschten Rechen-Operator: ");
        char eingabeOperator = getchar();
        rechenOperator(eingabeOperator);
    }
     
    void rechenOperator(char rOperator) {
        switch(rOperator) {
          case'+':
            addieren();            
          break;
    
          case's': case 'S':
            exit(0); 
          break;
     
          default:
            printf("\nDu hast keine korrekte Auswahl eingegeben.\n");
            get();
          break;
      }
    }
     
    void addieren(){
        float Summand1, Summand2, ergebnis;
        printf("\nErster Summand: ");
        scanf("%f", &Summand1);
        printf("\nZweiter Summand: ");
        scanf("%f", &Summand2);
        ergebnis = Summand1 + Summand2;
        system("cls");
        printf("\n%f + %f = %f\n", Summand1, Summand2, ergebnis);
        menue();
        get();
    }
    "without goto we would be wtf'd"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    Having a function called 'get' which returns nothing is just weird.

    And just pushing the infinite recursion out of main doesn't really make much progress.

    Code:
    #include <stdlib.h>
      
    void addieren();
    void menue();
    char get();
    void rechenOperator(char rOperator);
      
    int main() {
        while ( 1 ) {
            menue();
            rechenOperator(get());
        }
        return 0;
    }
      
    void menue(){
        printf("\n----- Taschenrechner-Menue -----\n");
        printf("Schreibe 'S' oder 's' um den Rechner zu verlassen \n");
        printf("Schreibe + Symbol fuer Addition \n");
        printf("Schreibe - Symbol fuer Subtraktion \n");
        printf("Schreibe * Symbol fuer Multiplikation \n");
        printf("Schreibe / Symbol fuer Division \n");
        printf("Schreibe 'W' oder 'w' fuer Wurzel \n");
    }
     
    char get() {
        printf("\nSchreibe den gewuenschten Rechen-Operator: ");
        char eingabeOperator = getchar();
        return eingabeOperator;
    }
      
    void rechenOperator(char rOperator) {
        switch(rOperator) {
          case'+':
            addieren();            
          break;
     
          case's': case 'S':
            exit(0); 
          break;
      
          default:
            printf("\nDu hast keine korrekte Auswahl eingegeben.\n");
          break;
      }
    }
      
    void addieren(){
        float Summand1, Summand2, ergebnis;
        printf("\nErster Summand: ");
        scanf("%f", &Summand1);
        printf("\nZweiter Summand: ");
        scanf("%f", &Summand2);
        ergebnis = Summand1 + Summand2;
        printf("\n%f + %f = %f\n", Summand1, Summand2, ergebnis);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with switch case messing up case variables
    By mp59 in forum C Programming
    Replies: 3
    Last Post: 07-02-2018, 12:12 PM
  2. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  3. Help with switch case
    By byebyebyezzz in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2011, 10:18 PM
  4. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  5. Switch case help ?
    By Black&White in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2004, 12:04 PM

Tags for this Thread