Howdy, Stranger!

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

Categories

my C program is not working!!!

lanzlanz Member Posts: 11
/*It doesn't work!!!*/


#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

  • AsmGuru62AsmGuru62 Member Posts: 6,519
  • APVAPV Member Posts: 6
    /* You must use 'scanf' function in this style: scanf("%s", &selection)
    %s and %c - for chars (for example 't'), %d is used for decimal digits (in your program
    first switch working with digits - '1' '2' '3' - so program works.
    Advice: try not to use 'goto' keyword in your C/C++ programs - it is included
    for compatibility purposes with older C programs. There is always better
    techniques available. Too many variables, in such a program they are unneeded.
    */

    #include

    main(void)
    {
    char selection;

    tryAgain:

    printf("

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

    1 - Ford");
    printf("

    2 - Chevrolet");
    printf("

    3 - Dodge");
    printf("

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

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

    [t]aurus");
    printf("

    [e]xplorer");
    printf("

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

    switch(selection)
    {
    case 't':
    case 'T':
    printf("

    You chose a Ford Taurus");
    break;

    case 'e':
    case 'E':
    printf("

    You chose a Ford Explorer");
    }

    break;

    case '2': /*Chevrolet*/
    printf("

    [t]ahoe");
    printf("

    [b]lazer");
    printf("

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

    switch(selection)
    {
    case 't':
    case 'T':
    printf("

    You chose a Chevrolet Tahoe");
    break;

    case 'b':
    case 'B':
    printf("

    You chose a Chevrolet Blazer");
    }

    break;

    case '3': /*Dodge*/
    printf("

    [v]iper");
    printf("

    [g]rand caravan");
    printf("

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

    switch(selection)
    {
    case 'v':
    case 'V':
    printf("

    You chose a Dodge Viper");
    break;
    case 'g':
    case 'G':
    printf("

    You chose a Dodge Grand Caravan");
    }

    break;

    default:
    printf("

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

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

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

Sign In or Register to comment.