Thread: Really bad programming! I think i need help..

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    18

    Question Really bad programming! I think i need help..

    The program that I am writing takes 6 inputs from the user which are all numbers. When all the numbers have been inputted the program asks whether the user is happy with the values. If they enter "n" for no then this is what comes next:

    Code:
    int changedata()
    {
        char answer[2],changedvalue[80];
    
        while(1)                        /*The while loop ensures the program gets the pre determined answer before the user can continue*/
        {
            system("cls");                  /*This clears the screen each time so it doesn't fill up */
            displaydata();
            printf("\n\n\n              Are you happy with these values? (Y for yes,N for no)\n\n                                       ");
            scanf("%s",answer);
    
            if (strcmpi("y",answer)==0)          /*Confirmation that the values are correct from the user*/
            {
                delay();                /*If the user is happy with the values then the program says "storing data" and the while loop ends*/
                break;                  /*The break signals the end of the while loop*/
            }
    
            if (strcmpi("n",answer)==0)
            {
            while(1)
                {
                system("cls");          /*This clears the screen so it doesn't fill up */
                displaydata();
                printf("\n\n              Please enter the value (A,B,C,X,Y or Z) you wish to change.\n              Type 'OK' to accept the data\n\n                                       ");
                scanf("%s",changedvalue);
    
                    if (strcmpi("ok",changedvalue)==0)
                    {
                        delay();
                        break;
                    }
    
                    if (strcmpi("a",changedvalue)==0)
                    {
                        functiona();            /*This calls the function to re input "a"*/
                    }
    
                    if (strcmpi("b",changedvalue)==0)
                    {
                        functionb();            /*This calls the function to re input "b"*/
                    }
    
                    if (strcmpi("c",changedvalue)==0)
                    {
                        functionc();            /*This calls the function to re input "c"*/
                    }
    
                    if (strcmpi("x",changedvalue)==0)
                    {
                        functionx();            /*This calls the function to re input "x"*/
                    }
    
                    if (strcmpi("y",changedvalue)==0)
                    {
                        functiony();            /*This calls the function to re input "y"*/
                    }
    
                    if (strcmpi("z",changedvalue)==0)
                    {
                        functionz();             /*This calls the function to re input "z"*/
                    }
    
                }                               /*end of while loop*/
                break;                          /*break out of the second while loop */
            }                                   /*end of if statement*/
        }
    }

    And just below this in my program I have:

    Code:
    int functiona(void)
    {
    
        system("cls");                          /*This clears the screen and displays the data*/
        displaydata();
        printf("Please enter a new value for A:");         /*Gets the new value of a*/
        scanf("%s",a);
        system("cls");                          /*Immediate update of table of data!*/
        displaydata();
        printf("A has changed to %s",a);
    }
    
    int functionb(void)
    {
        system("cls");                          /*This clears the screen and displays the data*/
        displaydata();
        printf("Please enter a new value for B:");
        scanf("%s",b);
        system("cls");                          /*Immediate update of table of data!*/
        displaydata();
        printf("B has changed to %s",b);
    }



    Am i right in thinking that this is a really inefficient way of doing this? I have a functiona,functionb,functionc etc. I have a function to re-enter each value. Is there a way to have 1 function that can be used to re enter all 6 values? I'm sure there is and i've been playing around with sending variables between functions but i just can't work it out.

    I really appreciate any help, thanks everyone

    Jez

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    1. Use if ... else if instead of if ... if
    2. instead of strcmpi("a" ... do the following:
    Code:
    if (strcmpi("ok", changedvalue) == 0) {
        delay();
        break;
    }
    
    if (changedvalue[1] != '\0')           /* a, b, c etc. are single letters, so the second item in the array must be nul */
        /* Error */;
    
    switch (changedvalue[0]):
        case 'a':
            functiona();
            break;
    
        case ...
    
        default:
            /* Error */
            break;
    }
    3. why int function[abcxyz]? That would go, too:

    Code:
    void function(char name, char *array)
    {
        /* clear_screen(); display_data();*/
        printf("Please enter a new value for %c:", name);
        scanf("%s", array);
        /* clear_screen(); display_data(); */
        printf("%c has changed to %s", name, array);
    }
    Last edited by Brafil; 04-20-2009 at 07:35 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Any time you see code repeated several times like this, with very few differences in it, it should be re-thought.

    Instead of a different function, why not just one?

    Code:
    void change(char *old_char, pointer to data struct that holds the ABCDEF values.) 
    { 
         print "You want to change %s", old_char)
         print the new value of %s", old_char
    
    
         scanf( get the new value, pointer to that place on the data struct)
    
    }
    That's it in a nutshell.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    thanks for the quick replies

    Brafil i will work on what you said but am having trouble getting the switch and case "a" part to work, it says there is a syntax problem.

    Adak, are you saying the same as brafil in the code above yours? Sorry i'm quite new to this...

    Code:
    void function(char name, char *array)   
    void change(char *old_char, pointer to data struct that holds the ABCDEF values.)
    What is the * on the other side of the comma on each of your functions?

    Thanks again,

    Jez

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You have to switch the first char in changedvalue (If the second one is a NUL). And you gotta write 'a' (in single qutoes), not "a".

    PS: If you don't know about pointers yet (the strange *), try this:

    Code:
    void function(char name, char array[])
    It will have the same effect.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    thanks i think i'm doing alright now

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The * just means that you'll need an address for the variable that you want to change. The name of the array can serve that function if it's used just right, as well.

    In C, as you may know, you always get a *copy* of the variable in the parameters for a function, never the original.

    Thankfully, the *thing* (whatever thing you want) that is at address (whatever address) can be referenced by making a copy of it's address, and letting the function work with it (that thing, again ), through that address.

    It's sort of like, I've never seen you, and you're nowhere close by, and I have to find you to do a job. I don't even know where you are, but I know the address where you work, and what desk or workbench, you work at. So I can find you.

    Yes, our idea's were the same, basically. Mine are just better looking.

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You've written it in pesudo-code. Of course it is better looking! :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 06:57 PM
  2. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  3. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM