Thread: bad code?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    bad code?

    Hi im writing a program for a project and dont know why i cant refer to the math1 funtion from inside the void option1(void) function(see below) any insight into this would be appreciated.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char option, optiona;
    void math1(void)  
    { 
            system("cls");
            printf("This program will calculate total, average and mean\nof a set of numbers you calculate.\n\n");
    float x, sum, sq;
    int i, n;
    
            printf("How many numbers do you wish to use? ");
            scanf("%d", &n);
            printf("Please enter number  1: ");
            scanf("%f", &x);
            sum = x;/*first value entered*/
            sq = x*x;
    for (i = 2; i <= n; i++)/*i++ OR i = i + 1 OR i += 1*/
    {
            printf("Please enter number %2d: ", i);
            scanf("%f", &x);
            sum += x;
            sq += x*x;
    }
            printf("Sum = %10.3f, average = %10.3f, sum of squares = %10.3f\n", sum, sum / n, sq);
    }
                     
    void option1(void)
    {
            
        
      
            system("cls");
            printf("\t\t*****************\n");
            printf("\t\t*Maths Functions*\n");
            printf("\t\t*****************\n\n");        
            printf("Welcome to the maths functions programs.\n\n"); 
            printf("Please select which option you want.\n\n");        
            printf("1. Program to find the total, average and mean of a set of numbers\n\n");
            
            scanf("%d", &optiona);
            
           
            
            if (optiona == '1')
                math1();        
            else if (optiona <= 0 || optiona > 1)
                printf("NO!");
           
    }
    
    
    void option2(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 2, details later*\n");
            printf("\t\t*************************\n\n");
    
    
    
    }
    
    
    void option3(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 3, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option4(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 4, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option5(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 5, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option6(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 6, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    struct personal{
            char name[30];
            }prs_info;
    
    
    
    main()
    {
    
    
    
            printf("\t\t*********************************************\n");
            printf("\t\t*PROGRAMMING PROJECT, SEMESTER 2 (2001/2002)*\n");
            printf("\t\t*********************************************\n\n");
    
            printf("Hello, this is a data processing package,\n\n");
            printf("Please enter your name so we can get started: ");
            fgets(prs_info.name, 30, stdin);
            
    
    do{
            system("cls");
            printf("Ok %sPlease choose from the following options (1 - 6)\n\n\n", prs_info.name);
    
    
    
    
     
    
    
            
            printf("\t1. Maths Functions Package.\n\n");
            printf("\t2. File Editing Package.\n\n");
            printf("\t3. Option3\n\n");
            printf("\t4. Option4\n\n");
            printf("\t5. Option5\n\n");
            printf("\t6. Option6\n\n");
            scanf("%c", &option);
    
    
    if (option =='1')
       option1();     
    else if (option == '2')
       option2();
    else if (option == '3')
       option3();
    else if (option == '4')
       option4();
    else if (option == '5')
       option5();
    else if (option == '6')
       option6();
    
    }
    while (option <= '0' || option > '6');
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're defining optiona as a char, then using %d within scanf to populate it.

    Try using %c.
    Or defining optiona as an int.

    Either way, it (and maybe you!) appear to be getting confused
    Last edited by Hammer; 05-02-2002 at 10:56 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    thanks hammer but that wasnt it, that was just me trying to change the declaration from int to char and left it like that accidently before i copied it to this thread, any other suggestions?

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Here's something I picked up from another thread. I think this will help.
    Originally posted by Prelude

    Because scanf is lame. What happens is when you enter input, scanf will read what you tell it to, but if there is anything else in the stream that matches the next call to scanf, it will read that immediately, not promting the user for input. Such is the case with string input for scanf, usually the culprit is a newline character that scanf leaves in the input stream. The next call reads the newline and quits because scanf is delimited by a newline and whitespace when reading string data.

    -Prelude
    Maybe you can use the getch or getchar function instead.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster
    Here's something I picked up from another thread. I think this will help.

    Maybe you can use the getch or getchar function instead.
    Yep, that was get a lengthy conversation for such a *simple* topic if I remember rightly, maybe you should have a look at it Brad. Do a search, it was in the last day or two I think.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    i made it work by changing the choices on the maths list to A and not 1 (i dont know why this had an effect). Now the probelm seems to be that it is looping twice before i can type anything( it'd probably be best if you could run the program as it shows the problem obviously) and then the maths program doesnt do the final calculation. im sure its just 1 little thing but i cant see where. thanks

    Brad.

    altered code from last time:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char option, optiona;
    
    
    void math1(void)  
    { 
            system("cls");
            printf("This program will calculate total, average and mean\nof a set of numbers you calculate.\n\n");
    float x, sum, sq;
    int i, n;
    
            printf("How many numbers do you wish to use? ");
            scanf("%d", &n);
            printf("Please enter number  1: ");
            scanf("%f", &x);
            sum = x;/*first value entered*/
            sq = x*x;
    for (i = 2; i <= n; i++)/*i++ OR i = i + 1 OR i += 1*/
    {
            printf("Please enter number %2d: ", i);
            scanf("%f", &x);
            sum += x;
            sq += x*x;
    }
    printf("Sum = %10.3f, average = %10.3f, sum of squares = %10.3f\n", sum, sum / n, sq);
    }
                     
    void option1(void)
    {
            
    do
    {
            
            //system("cls");
            printf("\t\t*****************\n");
            printf("\t\t*Maths Functions*\n");
            printf("\t\t*****************\n\n");        
            printf("Welcome to the maths functions programs.\n\n"); 
            printf("Please select which option you want.\n\n");        
            printf("A. Program to find the total, average and mean of a set of numbers\n\n");
            optiona=getchar();
            
           { 
           if (optiona == 'a' || optiona == 'A')
              math1();        
           else if (optiona != 'a'|| optiona != 'A')
           printf("Thats not an option!\n");
           } 
    }
    while (optiona != 'a'|| optiona != 'A');      
    }
    
    
    
    void option2(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 2, details later*\n");
            printf("\t\t*************************\n\n");
    
    
    
    }
    
    
    void option3(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 3, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option4(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 4, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option5(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 5, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    void option6(void)
    {
            system("cls");
            printf("\t\t*************************\n");
            printf("\t\t*Option 6, details later*\n");
            printf("\t\t*************************\n\n");
    }
    
    struct personal{
            char name[30];
            }prs_info;
    
    
    
    main()
    {
    
    
    
            printf("\t\t*********************************************\n");
            printf("\t\t*PROGRAMMING PROJECT, SEMESTER 2 (2001/2002)*\n");
            printf("\t\t*********************************************\n\n");
    
            printf("Hello, this is a data processing package,\n\n");
            printf("Please enter your name so we can get started: ");
            fgets(prs_info.name, 30, stdin);
            
           
    do{
          
            system("cls");
            printf("Ok %sPlease choose from the following options (1 - 6)\n\n\n", prs_info.name);                
            printf("\t1. Maths Functions Package.\n\n");
            printf("\t2. File Editing Package.\n\n");
            printf("\t3. Option3\n\n");
            printf("\t4. Option4\n\n");
            printf("\t5. Option5\n\n");
            printf("\t6. Option6\n\n");
            option=getchar();
            //scanf("%c", &option);
    
    
    if (option =='1')
       option1();     
    else if (option == '2')
       option2();
    else if (option == '3')
       option3();
    else if (option == '4')
       option4();
    else if (option == '5')
       option5();
    else if (option == '6')
       option6();
    
    }
    while (option <= '0' || option > '6');
    
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Change this
    Code:
    printf("A. Program to find the total, average and mean of a set of numbers\n\n");
    optiona=getchar();
    to this
    Code:
    printf("A. Program to find the total, average and mean of a set of numbers\n\n");
    while ( getchar() != '\n' );
    optiona=getchar();
    Once you clear the input stream the program will wait for user input instead of using what is in the stream.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    thanks prelude, ok, how can i return to the main function from functions above the main?

    Brad.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by brad123
    how can i return to the main function from functions above the main?
    When a function finishes it (normally) returns to it's caller (unless you do things like exit() etc).

    I expect your problem is related to the fact your prog only gives the user the ability to work one option on the menu, then it terminates. You'll need to incorporate an Exit option to allow the user to say when they're done.

    Sorry if I'm way off base with understanding what ya mean
    Last edited by Hammer; 05-02-2002 at 02:22 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    yes i think you are on track with wot i mean hammer, after i type in the variables into the maths program and i have a result how do i return to the previous menu? Thanks

    Brad.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do i return to the previous menu
    You do. The problem is that you don't repeat the menu, instead you exit the program because there's nothing left. Try placing a loop around your menu loop and have it exit only when the option variable holds an exit value.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Thanks prelude thats sorted it

    Brad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Bad code examples
    By Monster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-27-2003, 05:02 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM