Howdy, Stranger!

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

Categories

my C program doesn't work!!!

lanzlanz Member Posts: 11
/* This is a little incomplete but working 'C' program of mine .*/

/* I created another two separate files namely engine.txt and specs.txt
and this program will try to read the contents from these two files.*/

/*I have a problem though, after displaying the contents of the "engine.txt"
file; I want my program to ask the user if he wants to go back to the
sub-menu so he could also display the contents of the 'specs.txt' file*/

/*The problem is that the program always exits and
doesn't give the user a chance to go back in the sub-menu*/

/*the case 'n' for no (if the user doesn't want to go back in the sub-menu)
doesn't work that's why I erased it...*/

/*Please help me... this is for my project, I would really appreciate it if you could
help me... thanks!!!*/

#include
#include

main (void)
{
FILE *filepointer;
char ch;
char choice;

main:

textbackground (GREEN);
clrscr();
textcolor (RED);
gotoxy (24,2);
cprintf ("--=====Sports Cars=====--");
textcolor (BLUE);
gotoxy (13,5);
cprintf ("1 - Ferrari");
gotoxy (13,7);
cprintf ("2 - Quit");
gotoxy (24,9);
cprintf ("Enter no. of your choice:");
choice=getche();

switch (choice)
{
case '1': /*Ferrari*/
sub:
clrscr ();
textcolor (WHITE);
gotoxy (28,9);
cprintf ("[e]ngine");
gotoxy (28,11);
cprintf ("[s]pecifications");
gotoxy (20,13);
cprintf ("Enter the letter of your choice:");

scanf ("%s", &choice);

switch (choice)
{
case 'e': /*engine*/
case 'E':

filepointer = fopen("engine.txt", "r");
clrscr();
gotoxy(28,9);
while ((ch = fgetc(filepointer)) != EOF)
printf("%c", ch);
fclose(filepointer);
gotoxy(28,20);
cprintf ("go back to the sub-menu? [y/n]:");

switch (choice)
{
case 'y':
case 'Y':
goto sub;
break;

case 'n':
case 'N':
exit(0);
}


break;


case 's': /*specifications*/
case 'S':

filepointer = fopen("specs.txt", "r");
clrscr();
gotoxy(28,9);
while ((ch = fgetc(filepointer)) != EOF)
printf("%c", ch);
fclose(filepointer);
gotoxy(28,20);
cprintf ("go back to the sub-menu? [y/n]:");

switch (choice)
{
case 'y':
case 'Y':
goto sub;
}


break;



}
break;





case '2': /*quit*/
clrscr();
gotoxy (20,9);
cprintf("Thanks!!!");
gotoxy (34,11);
cprintf ("

");
exit (1);
}


}

Comments

  • BriballBriball Member Posts: 265
    I am more of a iostream guy myself, but I think I see the problem (despite all of the crossing outs). You seem to be using switch/case statements for characters, which hardly ever works. You're asking the user for a char, then doing a case 1: Well, there is only one character that's code is equal to one, and I believe that it is undisplayable. Try using if/else logic instead of switch case for this purpose.
  • SteveCSteveC Member Posts: 63
    Here are your input and switch statements
    : choice=getche();
    : switch (choice)
    : scanf ("%s", &choice);
    : switch (choice)
    : switch (choice)
    See what's missing?

    Also, it's not a good idea to mix input routines. scanf in particular leaves unexpected characters (usually newline) on the input stream, ready to mess up something else.
    Try this approach
    char buff[BUFSIZ]; // declaration

    // read a char
    fgets( buff, BUFSIZ, stdin );
    choice = buff[0];

    Now do all your switch/case on choice

    Also, nested switch statements get very long very quickly (and hence unmanageable)
    Try creating some functions to do most of the work
    Instead of
    : case '1': /*Ferrari*/
    : sub:
    Try
    case '1': do_ferrari(); break;


  • Andre YoungAndre Young USAMember Posts: 0

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

Sign In or Register to comment.