nested statements in C (not C++!!!)

/*I need this for my project in C.. can anyone give me an example program that have nested statements (p.s. Can anyone give me codes which is in C not C++?)
here's an example program of mine that doesn't work. I appreciate any changes or alterations in my program, thanks!!!*/

#include
main (void)
{
int selection, subselection1, subselection2, subselection3;

tryagain:
printf ("

###Automobiles###");
printf ("

1 - Ford");
printf ("

2 - Chevrolet");
printf ("

3 - Dodge");
printf ("

Enter your choice:");
scanf ("%d", &selection);

switch (selection)
{
case 1: /*Ford*/
printf ("

[t]aurus");
printf ("

[e]xplorer");
printf ("

Enter your choice:");
scanf ("%d", &subselection1);

switch (subselection1)
{
case 't':
printf ("

You chose a Ford Taurus");

case 'e':
printf ("

You chose a Ford Explorer");
}
break;

case 2: /*Chevrolet*/
printf ("

[t]ahoe");
printf ("

[b]lazer");
printf ("

Enter your choice:");
scanf ("%d", &subselection2);

switch (subselection2)
{
case 't':
printf ("

You chose a Chevrolet Tahoe");

case 'b':
printf ("

You chose a Chevrolet Blazer");
}
break;
case 3: /*Dodge*/
printf ("

[v]iper");
printf ("

[g]rand caravan");
printf ("

Enter your choice:");
scanf ("%d", &subselection3);

switch (subselection3)
{
case 'v':
printf ("

You chose a Dodge Viper");

case 'g':
printf ("

You chose a Dodge Grand Caravan");
}
break;
default:
printf ("

Invalid choice. Try again");
goto tryagain;
}


}

Comments

  • Hi,

    Some of the variables haven't been declared properly and you have used the same character for different cases.

    I have modified your code to read the following:

    [code]
    #include

    void main(void)
    {
    /* Declaring the variables */
    int selection;
    char subselection1, subselection2, subselection3;
    char buffer;

    tryagain:

    printf("

    ###Automobiles###");
    printf("

    1 - Ford");
    printf("

    2 - Chevrolet");
    printf("

    3 - Dodge");
    printf("

    Enter your choice: ");
    scanf("%d", &selection);

    switch(selection)
    {
    case 1: /*Ford*/
    printf("

    [t]aurus");
    printf("

    [e]xplorer");
    printf("

    Enter your choice, (t or e): ");
    scanf("%c%c", &buffer, &subselection1);

    switch(subselection1)
    {
    case 't':
    printf("

    You chose a Ford Taurus
    ");
    break;

    case 'e':
    printf("

    You chose a Ford Explorer
    ");
    break;
    }
    break;

    case 2: /*Chevrolet*/
    printf("

    t[a]hoe");
    printf("

    [b]lazer");
    printf("

    Enter your choice, (a or b): ");
    scanf("%c%c", &buffer, &subselection2);

    switch(subselection2)
    {
    case 'a':
    printf("

    You chose a Chevrolet Tahoe
    ");
    break;

    case 'b':
    printf("

    You chose a Chevrolet Blazer
    ");
    break;
    }
    break;

    case 3: /*Dodge*/
    printf("

    [v]iper");
    printf("

    [g]rand caravan");
    printf("

    Enter your choice: ");
    scanf("%c%c", &buffer, &subselection3);

    switch(subselection3)
    {
    case 'v':
    printf("

    You chose a Dodge Viper
    ");
    break;

    case 'g':
    printf("

    You chose a Dodge Grand Caravan
    ");
    break;
    }
    break;

    default:
    printf("

    Invalid choice. Try again!
    ");

    goto tryagain;
    }
    }
    [/code]

    I noticed that there are no Ferraris and Porsches in your code [b]very bad![/b] :-)

    Hope this helps,


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion