Uh-oh! I am having a major switch problem!

This is a discussion on Uh-oh! I am having a major switch problem! within the C Programming forums, part of the General Programming Boards category; Hey. i'm making this program right now, and I KNOW what the problem is, but I don;'t know what I ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Uh-oh! I am having a major switch problem!

    Hey. i'm making this program right now, and I KNOW what the problem is, but I don;'t know what I did wrong. All the functions and everything work correctly, so no need to worry about those, but I'm pretty sure I screwed up SOMEWHERE in my switching. Everytime I slect a number, ALL possible entries take place. Entries 1-11 and 99. Can anyone see my problem? i've been debugging the crap out of this thing to no avail.

    OMG! NEVERMIND! I can't believe I was so stupid I didn't put BREAK in there!


    Code:
    int main ()
    {
    FILE *fp;
    double array[100];
    int selection=0, num=0, count=0, column=0;
    
    
    
    while (selection != 99)
    {
    printf("1- Read from arraynumbers1.txt \n");
    printf("2- Read from arraynumbers2.txt \n");
    printf("3- Read from arraynumbers3.txt \n");
    printf("4- Sort in ascending order \n");
    printf("5- Sort in decending order \n");
    printf("6- Calculate the sum \n");
    printf("7- Calculate the average \n");
    printf("8- Calculate the Minimum \n");
    printf("9- Calculate the Maximum \n");
    printf("10- Display the array in 1 columns \n");
    printf("11- Read from arraynumbers3.txt \n");
    
    printf("Enter Selection: \n");
    scanf("%i", &selection);
    
    
    switch(selection)
    {
    case 1:
    {
    fp = fopen("arraynumbers1.txt", "r");
    
    if ( fp == NULL )
    {
    printf("File could not be opened\n\n");
    return 1;
    }
    
    num=0;
    while (fscanf(fp, "%lf", &array[num]) != EOF)
    num++;
    
    printf("%i numbers were read from arraynumbers1.txt. \n", num);
    
    }
    
    case 2:
    {
    num=0;
    
    fp = fopen("arraynumbers2.txt", "r");
    
    if ( fp == NULL )
    {
    printf("File could not be opened\n\n");
    return 1;
    }
    
    while (fscanf(fp, "%lf", &array[num]) != EOF)
    num++;
    
    printf("%i numbers were read from arraynumbers2.txt. \n");
    }
    
    case 3:
    {
    num=0;
    fp = fopen("arraynumbers3.txt", "r");
    
    if ( fp == NULL )
    {
    printf("File could not be opened\n\n");
    return 1;
    } 
    
    while (fscanf(fp,"%lf", &array[num]) !=EOF)
    num++;
    
    printf("%i numbers were read from arraynumbers3.txt. \n");
    }
    
    case 4:
    {
    if (num==0)
    printf("Please select 1-3 first. \n");
    
    else
    {
    dsort(array, num, 0);
    
    printf("The New array is:");
    
    for (count = 0; count<num; count++)
    printf("%.2lf ", array[count]);
    
    printf("\n\n");
    }
    
    case 5:
    {
    if (num==0)
    printf("Please select 1-3 first. \n");
    
    else
    dsort(array, num, 1);
    
    printf("The New array is: \n");
    
    for (count = 0; count<num; count++)
    printf("%.2lf  ", array[count]);
    
    printf("\n\n");
    }
    
    case 6:
    {
    if (num==0)
    printf("Please select 1-3 first. \n");
    
    else
    printf("sum=%.2lf", dsum(array,num));
    
    printf("\n\n");
    }
    
    case 7:
    {
    if (num==0)
    printf("Please select 1-3 first.\n");
    else
    printf("average=%.2lf", daverage(array,num) );
    
    printf("\n\n");
    }
    
    case 8:
    {
    if (num==0)
    printf("Please select 1-3 first.\n");
    else
    printf("maximum=%.2lf", dmax(array,num) );
    
    printf("\n\n");
    }
    
    case 9:
    {
    if (num==0)
    printf("Please select 1-3 first.\n");
    else printf("minimum=%.2lf", dmin(array,num) );
    
    printf("\n\n");
    }
    
    case 10:
    {
    if (num==0)
    printf("Please select 1-3 first.\n");
    
    else
    {
    for (count = 0; count<num; count++)
    printf("%.2lf \n ", array[count]);
    }
    
    printf("\n\n");
    }
    
    case 11:
    {
    if (num==0)
    printf("Please select 1-3 first.\n");
    
    else
    {
    for (count = 0, column = 0; count<num; count++, column++)
    {
    if(column==2)
    {
    printf("\n");
    column = 0;
    }
    printf("%.2lf    ", array[count]);
    }
    }
    printf("\n\n");
    }
    
    
    case 99:
    printf("Thank You and Come Again.\n");
    
    default:
    printf("Invalid Entry. Please Try Again.\n");
    }
    
    return 0;
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Hehe, I also had a couple errors, but I fixed those. Gues I should really look at my program for 5 minutes before deciding to ask for help!

  3. #3
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    switch statements use this syntax (from VC++ help file):

    switch ( expression )
    {
    declarations
    .
    .
    .
    case constant-expression :

    statements executed if the expression equals the
    value of this constant-expression
    .
    .
    .
    break; /*You forget this statement*/
    default :
    statements executed if expression does not equal
    any case constant-expression
    }

    You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.

    The default statement is executed if no case constant-expression is equal to the value of switch ( expression ). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. In fact it is often more efficient if it appears at the beginning of the switch statement. A case or default label can only appear inside a switch statement.

    The following examples illustrate switch statements:

    Code:
    switch( c ) 
    {
        case 'A':
            capa++;
        case 'a':
            lettera++;
        default :
            total++;
    }
    All three statements of the switch body in this example are executed if c is equal to 'A' since a break statement does not appear before the following case. Execution control is transferred to the first statement (capa++ and continues in order through the rest of the body. If c is equal to 'a', lettera and total are incremented. Only total is incremented if c is not equal to 'A' or 'a'.

    Code:
    switch( i ) 
    {
        case -1:
            n++;
            break;
        case 0 :
            z++;
            break;
        case 1 :
            p++;
            break;
    }
    In this example, a break statement follows each statement of the switch body. The break statement forces an exit from the statement body after one statement is executed. If i is equal to ?1, only n is incremented. The break following the statement n++; causes execution control to pass out of the statement body, bypassing the remaining statements. Similarly, if i is equal to 0, only z is incremented; if i is equal to 1, only p is incremented. The final break statement is not strictly necessary, since control passes out of the body at the end of the compound statement, but it is included for consistency.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Um, thanks, but didn't you read? I just forgot the breaks. I know how switches work, I just simply forgot to put in my breaks. I edited my post to say that I found and corrected the problem. Your long explanation was not necesarry.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I edited my post to say that I found and corrected the problem.
    > Your long explanation was not necesarry.

    You just remember those lines of text, when noone helps you out
    in the future and you're whining about it. You don't have to be a
    *censored*, just because someone explained the issue more in
    detail. It's called _being helpful_.

    Additionally, you are not the only poster here. Thus, just because
    you don't need the explanation, doesn't mean it isn't helpful to
    some other person out there.

    You know what? When people ask for help, and someone actually
    offers some assistance, it's considered polite to thank them,
    rather than be snide.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21