Thread: Program optimization

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Program optimization

    I have quite a few places in my program where input from the user is required. I want the user to be able to press q at anytime, so that they go back to the beginning of the program (where there is a little menu, and the whole things starts again. You can see in the program where i have an if statement, trying to do this, but it is not a solution for any point in the program.

    Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <Windows.h>
    
    int main()
    {
        char *options[100], *as[100], *bs[100], *cs[100], *Xos[100], *Yos[100], *Zos[100], *filename[20];//the s at the end of the name tell me it is stored as a string.
        float a, b, c, Xo, Yo, Zo;
        FILE *recentdata, *EulerData;//pointer to store recent data in text file.
    while (1)
    {
        menu();
        scanf("%99s", options);
    
        //What to do if user types exit
        if(strcmpi(options,"exit") == 0)//compare options to "recent" NOT case sensitive.
        {
            exit(0);
    
        }
    
        //What to do if user types q
        else if(strcmpi(options,"q") == 0)//compare options to "recent" NOT case sensitive.
        {
            system("cls");
            
    
    
    
        }
    
        //What to do if user types recent
        else if(strcmpi(options,"recent") == 0)//compare options to "recent" NOT case sensitive.
        {
            recentdata = fopen("recentdata.txt", "r");
             fscanf (recentdata, "%s %s %s %s %s %s", as, bs, cs, Xos, Yos, Zos);
             fclose;
    
        }
    
        //if the user didnt type recent, they typed something else in...?
    else
    {
        strcpy(as,options);//copy the value of options into as.
        scanf("%99s", bs);
        scanf("%99s", cs);
        printf ("%s %s %s\n\n", as, bs, cs);
    
        printf("Please input Xo, Yo and Zo\n");
        scanf("%99s", Xos);
        scanf("%99s", Yos);
        scanf("%99s", Zos);
        printf ("%s %s %s\n\n", Xos, Yos, Zos);
    
        recentdata = fopen("recentdata.txt", "w");
        fprintf(recentdata, "%s %s %s %s %s %s\n", as, bs, cs, Xos, Yos, Zos);
        fclose;
    
    }
    
            a=atof(as); //converting strings to floats so that the compiler understands it is a number that can be used mathematically.
            b=atof(bs);
            c=atof(cs);
            Xo=atof(Xos);
            Yo=atof(Yos);
            Zo=atof(Zos);
    
            printf (" a = %.2f\n b = %.2f\n c = %.2f\nXo = %.2f\nYo = %.2f\nZo = %.2f\n", a, b, c, Xo, Yo, Zo);
    
            printf("Beginning execution of improved Euler...\n\n");
            Sleep(1000);
            printf("Please enter the name of the file to store the data in\n");
            printf("If it doesnt exist, it will be created.\n\n");
            scanf("%s", filename);
            printf("\nStoring all Data in file '%s.txt'\n", filename);
            Sleep(1000);
    
    
    }
    return 0;
    }
    
    int menu()
    {
    
        printf("Type 'recent' to use the last entered data or:\nInput a, b and c, hitting Enter between each one,\n");
        printf("Typing 'q' at any time will bring you back here,\n");
        printf("To exit the program, from this screen type 'exit'.\n");
    
    return(1);
    }
    Last edited by a.mlw.walker; 03-27-2009 at 06:08 AM. Reason: noticed an error

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char *options[100]

    should be
    char options[100]

    for what you want to do is a little bit advanced to implement...

    maybe it will be enough on any step where you ask some input from user check that it is 'q'?

    also - you need to work on your indentation and maybe add more functions like menu() that do small tasks, instead of one long main that is hard to read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM