Howdy, Stranger!

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

Categories

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

lanzlanz Member Posts: 11
/*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

  • Geeth AsokanGeeth Asokan Member Posts: 127
    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,


  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ______ | http://forcoder.org | free video tutorials and ebooks about [ Swift Scratch PL/SQL Visual Basic Python Perl Java Visual Basic .NET C++ Delphi R Ruby PHP Objective-C C Go Assembly MATLAB C# JavaScript SAS Apex Prolog ABAP D Clojure Awk Lisp LabVIEW Alice Scala COBOL Bash Ada Scheme Kotlin VBScript Logo Erlang ML Transact-SQL Julia Hack F# FoxPro Lua Rust Crystal Fortran Dart ] ___________

Sign In or Register to comment.