Thread: my second program

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

    Question my second program

    So this is my second real program. It calculates the Improved Euler method - or is supposed to.

    When it runs the user types in either, recent - which loads the last data from a text file, or data, which allows them to enter the start values.

    If you type data, it should then ask for you to enter a, b, c, Xo, Yo, Zo.

    But for some reason, which i cant figure, it crashed when you press enter after typing either of these. I think its a problem with the Euler Function - possibly because I am trying to pass it strings in the wrong way.

    Please could someone have a look.

    I;m pretty new to this so my commenting and indenting is pretty poor. Please bear with me:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <Windows.h>
    
    int main()
    {
        char  *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.
    
        FILE  *recentdata, *EulerData;//pointer to store recent data in text file.
    while (1)
    {
        menu();
    
    
        //if the user typed in data, it will bring them back to here.
    
        printf("Please enter the data");
        printf("A:");
        scanf("%99s", as);
        printf("B:");
        scanf("%99s", bs);
        printf("C:");
        scanf("%99s", cs);
        printf ("%s %s %s\n\n", as, bs, cs);
    
        printf("Please input Xo, Yo and Zo\n");
        printf("Xo:");
        scanf("%99s", Xos);
        printf("Yo:");
        scanf("%99s", Yos);
        printf("Zo:");
        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;
    
    
    
    
    
            printf ("\n a = %.2s\n b = %.2s\n c = %.2s\nXo = %.2s\nYo = %.2s\nZo = %.2s\n", as, bs, cs, Xos, Yos, Zos);
    
            Sleep(1000);
            printf("\nPlease 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);
            printf("Beginning execution of improved Euler...\n\n");
            Sleep(1000);
    
            //send Euler function the relevent variables.
            Euler(as, bs, cs, Xos, Yos, Zos);
    
    
    }
    return 0;
    }
    
    int menu()
    {
    
        printf("\nType '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 here type 'exit'.\n\n");
        user_command();
    
    return 0;
    }
    
    int Euler(t, u, v, x0, y0, z0)
    
        char t, u, v, x0, y0, z0;
        {
            float x[100], y[100], z[100], dxdt_i, dydt_i, dzdt_i,dx, dy, dz, dxdt_new, dydt_new, dzdt_new, dt = 1;
            float a, b, c, Xo, Yo, Zo;
            int i;
    
            a=atof(t); //converting strings to floats so that the compiler understands it is a number that can be used mathematically.
            b=atof(u);
            c=atof(v);
            Xo=atof(x0);
            Yo=atof(y0);
            Zo=atof(z0);
    
            x[0] = Xo;
            y[0] = Yo;
            z[0] = Zo;
    
            system("cls");
            printf ("\nLorenz formulas:\n\n");
            printf("dx                          dy                             dz \n");
            printf("--  = a(y - x)       :      --  = x(b - z) - y      :      --  = xy - cz\n");
            printf("dt                          dt                             dt\n\n\n");
    
    
            for (i = 0; i<101; i++)
            {
    
            dxdt_i = t*(y[i] - x[i]);
            dydt_i = x[i]*(u - z[i]) - y[i];
            dzdt_i = (x[i]*y[i]) - (v*z[i]);
    
            dx = (t*(y[i] - x[i]))*dt;
            dy = (x[i]*(u - z[i]) - y[i])*dt;
            dz = ((x[i]*y[i]) - (v*z[i]))*dt;
    
    
            dxdt_new = t*(y[i] - (x[i] + dx));
            dydt_new = x[i]*(u - z[i]) - (y[i] + dy);
            dzdt_new = (x[i]*y[i]) - (v*(z[i] + dz));
    
            x[i+1] = x[i] +(dt/2)*(dxdt_i + dxdt_new);
            y[i+1] = y[i] +(dt/2)*(dydt_i + dydt_new);
            z[i+1] = z[i] +(dt/2)*(dzdt_i + dzdt_new);
    
            printf("dx                          dy                             dz\n");
            printf("--  = %5.2f  :      --  = %5.2f  :      --   = %5.2f\n", dxdt_i, dydt_i, dzdt_i);
            printf("dt                          dt                             dt\n");
            printf("\nnew xgrad = %.3f new ygrad = %.3f new zgrad = %.3f\n\n", dxdt_new, dydt_new, dzdt_new);
            printf("\nnew x = %.3f new y = %.3f new z = %.3f\n\n", x[i+1], y[i+1], z[i+1]);
    
           i =  i++;
        }
        }
    
    int user_command()
    {
        char *options[100];
        FILE *recent;
       char *af[100], *bf[100], *cf[100], *Xof[100], *Yof[100], *Zof[100];
        //function decides what to do based on user input
    
        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");
            menu();
    
        }
    
        //What to do if user types recent
        else if(strcmpi(options,"recent") == 0)//compare options to "recent" NOT case sensitive.
        {
                recent = fopen("recentdata.txt", "r");
                fscanf (recent, "%s %s %s %s %s %s", af, bf, cf, Xof, Yof, Zof);
                fclose;
               
                Euler(af, bf, cf, Xof, Yof, Zof);
    
    
    
        }
        else if (strcmpi(options,"data") == 0)
    {
        return(0);
    }
    }

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    it crashed when you press enter after typing either of these
    use getchar after each scanf to get ride of the \r , thats why *i think you program crashes
    cause it stores inappropriate values

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Just saying, its not good programming practice to have functions start with a capital letter.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Poincare View Post
    Just saying, its not good programming practice to have functions start with a capital letter.
    why?
    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

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    OK will get rid of capital letters but as vart said, why?

    where is the \r?

    what will getchar do, how is it used?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In your user_command function, you don't have any space to store the data that the user types in. Perhaps you meant "char options[100]" instead?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    you have a i=i++ in your code and thats not good.
    do i=i+1;
    or just use i++; alone but never i=i++ or i++ = i;

    that way of increment and decrement is is something that make the program unstable and crash

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