Thread: Switch case, switch between case

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    8

    Switch case, switch between case

    Hello
    I excuse my bad english.

    I am trying to make a program which manages the data of 5 different patients (its an uni assignment), and i want to use a switch case as a menu to switch between the patients. All other functions (as for example putting the infromation on a file) work, but i cant figure out to bring the switch to work. First it asks for the number of the patient which should be worked with, this works perfectly, but afterwards changiung between the persons doesnt work as thought. It should ask everytime after it switches to one patient (i removed some functions to make it easier to read) and then asks to which it should jump next. If i put the number of one case (lets say 3) it just stops the program.

    ********Example:
    user@pc ~/wherever $ ./program
    current variables:
    jo = 2
    a = 0

    1 //the entered number
    The variable a = 1
    patient 1
    Enter the number of the next patient2
    // and then it closes
    **************


    The same thing happens if i compile an example code from a book, it writes the first case and then stops.

    I looked already through the forum but didnt find a person with a similar problem, maybe i didnt dig deep enough.

    I am running Linux Mint 17 and use gcc as compiler.

    Code:
    include <stdio.h>
    #include <stdlib.h>
    
    int a=0;
    int jo=2;
    
    int main(void){
    printf("current variables:\n jo = %d\n a = %d\n", jo, a);
    scanf("%d", &a);
    printf("The variable a = %d\n", a);
        switch ( a ) {
        case 1:
            printf("patient 1 \n");
            ChooseA();
        break;
        
        case 2:
            printf("patient 2 \n");
            ChooseA();
        break;
        
        case 3:
            printf("patient 3 \n");
            ChooseA();
        break;
        
        case 4:
            printf("patient 4 \n");
            ChooseA();
        break;
    
        case 5:
            printf("patient 5 \n");
            ChooseA();
        break;
    
        case 6:
            printf("Do you want to delete the collected data? 1 for yes/0 for no \n");
            jo=2;
            scanf("%d", &jo);
                if(jo == 1){
                    FILE *fp;
                    fp=fopen("/home/jpreissler/Skrivebord/ImperativProgrammering/database.txt", "w");
                    fclose(fp);
                    printf("Data deleted\n");
    
                }
                if(jo == 0){
                    printf("No data deleted\n");
                }
                else{
    
                }
        break;
    
        default:
        printf("NUMBER NOT VALID\n");
    }
    return(0);
    }
    int ChooseA(){
        printf("Enter the number of the next patient");
        scanf("%d", &a);
    }
    Last edited by HetzerDK; 12-30-2014 at 09:52 AM.

  2. #2
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    As far as I can see, looks like there are not any statements that take you back to the beginning of the switch. I think you could use while, do, for or even goto to "jump back to the beginning".

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    Quote Originally Posted by aldo_baldo View Post
    As far as I can see, looks like there are not any statements that take you back to the beginning of the switch. I think you could use while, do, for or even goto to "jump back to the beginning".
    #Doesnt work#
    do you mean something like

    int main(){
    while(a<= "someinteger")

    switch ( a ) {

    case 1:
    printf("patient 1 \n");
    a= "someinteger"++
    .........
    Last edited by HetzerDK; 12-30-2014 at 10:56 AM. Reason: doesnt work

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    I tried with a while loop (see my other post), but it seems that the goto statement works fine. Many thanks.

    int main(void){
    printf("current variables:\n jo = %d\n a = %d\n", jo, a);
    begin:
    scanf("%d", &a);
    printf("The variable a = %d\n", a);


    switch ( a ) {
    case 1:
    printf("patient 1 \n");
    a=0;
    goto begin;
    break;

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The next guy with goto.
    A do-while-loop do the same thing.

    Code:
    do {
        scanf(" %d", &a);
        printf("The variable a = %d\n", a);
        switch ( a ) {
            case 1:
                printf("patient 1 \n");
                ChooseA();
                break;
    
            case 2:
                printf("patient 2 \n");
                ChooseA();
                break;
    
            case 3:
                printf("patient 3 \n");
                ChooseA();
                break;
    
            case 4:
                printf("patient 4 \n");
                ChooseA();
                break;
    
            case 5:
                printf("patient 5 \n");
                ChooseA();
                break;
    
            case 6:
                printf("Do you want to delete the collected data? 1 for yes/0 for no \n");
                jo=2;
                scanf("%d", &jo);
                if(jo == 1) {
                    FILE *fp;
                    fp=fopen("/home/jpreissler/Skrivebord/ImperativProgrammering/database.txt", "w");
                    fclose(fp);
                    printf("Data deleted\n");
                }
                if(jo == 0) {
                    printf("No data deleted\n");
                }
                break;
    
            default:
                printf("NUMBER NOT VALID\n");
        }
    } while (a != 0);
    I have not check the content of the switchcases.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by HetzerDK View Post
    I tried with a while loop (see my other post), but it seems that the goto statement works fine. Many thanks.
    Do not use "goto"! Use a loop instead, as mentioned by WoodSTokk. Not only is "goto" considered bad practice when used this way, but loops are a very basic programming concept you need to understand. If you can't get your code to work with a loop, post your attempt and we'll help fix it.

    Loops.

    Also check your compiler warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|14|warning: implicit declaration of function 'ChooseA'|
    main.c||In function 'ChooseA':|
    main.c|64|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    If you define your function after "main()", you need to declare it before "main()". See post #2 here: function calsum should have a prototype??

    Also, if your function is not returning a value, its return type should be "void".

    And you should avoid using global variables.



    Quote Originally Posted by aldo_baldo View Post
    I think you could use while, do, for or even goto to "jump back to the beginning".
    As mentioned, the OP should avoid using "goto". It is considered bad practice in all but a few situations. Please avoid giving advice that is bad practice to people who are learning the language.

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Quote Originally Posted by Matticus View Post
    As mentioned, the OP should avoid using "goto". It is considered bad practice in all but a few situations. Please avoid giving advice that is bad practice to people who are learning the language.
    I wrote "or even goto". "EVEN", just to point out that goto should be the last choice if while, do and for can't match one's needs. Maybe my English is poor (I'm an old Italian chap) and I didn't succeed in stating what I had in mind, but that was my intention.

    Anyway, goto is not the devil. As an example, sometimes I find it useful using goto to manage errors in functions that require "cleanup" operations before returning. I don't mean that I use goto EACH TIME I have to manage errors in such funtions, but when I see the opportunity to use it I don't go and look for a priest to save my soul.

    By the way, what does "OP" mean? It's an acronym I found more than once in this forum and I can guess that it's referred to the one who asked a question (more or less), but I don't know what it EXACTLY stands for.

    P.S. As regards the current context, I think that you are right -- goto is utterly useless and can be easily avoided.
    Last edited by aldo_baldo; 12-31-2014 at 09:22 AM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I am aware that "goto" is not "the devil" and is acceptable at certain times (hence my qualifier "in all but a few situations") - one of those being for clean-up as you mentioned.

    However, it's all too easy for a beginner to abuse, which is why I suggest not offering it as advice to a beginner. To be clear, I wasn't trying to be rude to you in my post - I'm just trying to help maintain the quality of advice given in these forums.

    In this context, "OP" stands for "Original Poster" (the person who started the thread).

  9. #9
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    I agree with you. And many thank for the "OP" explanation -- I was miles away from the literal meaning you just let me know.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Excuse me for jumping into this thread, but I have a problem with a Switch/Case. I have a while loop, but the code seems to jump between cases without any reason.

    Do you need the last break at the end of the case statements?
    Does the program jump to the end of the program and then follow the while loop when a break is encountered, or to the end of the case statements?

    Should I start a new thread?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danlee58
    Should I start a new thread?
    Yes, please.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    [QUOTE=WoodSTokk;1220273]The next guy with goto.
    A do-while-loop do the same thing.

    I tried your solution, but the just stops after the chosen switch (im looking away from switch 6, whichs function is to close the program).

  13. #13
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    If you want to quit the program with case 6, then you have to edit the while contition on last line to:
    Code:
    …
    } while (a != 6);
    Other have classes, we are class

  14. #14
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    Thx, it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Case
    By KMAN999 in forum C Programming
    Replies: 2
    Last Post: 06-27-2011, 07:28 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. switch /case
    By shuo in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2007, 06:43 PM
  4. switch case....
    By tetraflare in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2003, 03:52 PM
  5. switch case
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 09-30-2002, 02:37 PM

Tags for this Thread