Having troubles compiling

This is a discussion on Having troubles compiling within the C Programming forums, part of the General Programming Boards category; So, basically my program is suppose allow the user to enter a number(s) for a menu. Display an average, highest ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Having troubles compiling

    So, basically my program is suppose allow the user to enter a number(s) for a menu. Display an average, highest number, lowest number, total of numbers entered so far, and how many numbers that have been entered.

    What I have highlighted in red, is where my Dev C++ compiler stops. I really do need help on this


    Code:
    #include <ctype.h> // to use the upper function
    #include <stdio.h>
    #include <stdlib.h>
    
    char getMenuChoice();
    int getNumber();
    
    
    int main() {
    //Declare variables here
         int aNumber = 0;
         float average = 0.0;
         int high = -1;
         int low = 1001;
         int total = 0;
         int count = 0;
         char getChoice = ' ';
         
         
         do{
            getChoice = getMenuChoice();
            switch (getChoice) {
                   
                   case 'A': 
                        aNumber = getNumber();
                        total += aNumber;
                        count++;
                        
                        if (aNumber = getNumber)
                           high = aNumber;
                        if (aNumber < low)
                           low = aNumber;
                        
                        printf(" Inside of A\n");
                        break;
                        
                   case 'B':
                        
                        average = (double)total / count;
                        printf("The avg is %.2f\n");
                        system("pause");
                       break;
                   case 'C':
                        
                        printf("The Highest Value WAS %i\n", high);
                        system("pause");
                         
                        break;
                   case 'D':
                        prinf("The Lowest Number is %i\n", low);
                        system("pause");
                        break;
                   case 'E':
                        printf("Inside of E\n");
                        break;
                   case 'F':
                        prinf("Inside of F\n");
                        break;
                   case 'G':
                        printf("Inside of G\n");
                        break;
                        }//End switch
                        
            } while (getChoice != 'G');
         
       system("pause");
       return 0;
    }      // end of Main
    
    
    char getMenuChoice() {
         char result, choice = ' ';
         
         
           while (result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E' && result != 'D' && result != 'E'
         && result != 'F' && result != 'G');
         
         system("cls"); //clear the screen
         
         printf("*********************************\n");
         printf("A) Enter a number between 0 and 1000\n");
         printf("B) Display the average\n");
         printf("C) Display the Highest Number\n");
         printf("D) Display the Lowest Number\n");
         printf("E) Display the total of all numbers entered so far\n");
         printf("F) Display how many numbers were entered during the session\n");
         printf("G) Quit the program\n");
         scanf("%c",&result);
         result = toupper(result);
         
         if (result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E' && result != 'D' && result != 'E'
         && result != 'F' && result != 'G') {
                   
                   printf("You must enter A - G only \n");
                   system("pause");
                   
                   }//End IF
                   
         
         
         
        
         return result;
    }//End of getMenuChoice
    
    
    int getNumber() {
        int result = -1;
        
        while (result < 0 || result > 1000)
        
        
        printf("Enter a number between 0 and 1000: ");
        scanf("%i", &result);
        
        if (result < 0 || result > 1000) {
        printf("You must enter a number between 0 and 1000\n")
        system("pause")
        
    }// End IF
    Last edited by Wolf776; 10-07-2011 at 11:39 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Next time, please copy-paste the error message from your compiler. It helps us immensely.

    The reason the compiler stops is because you use getNumber. getNumber() with parentheses calls the function. getNumber without parentheses is a pointer to said function. You can't go assigning a pointer (getNumber) to an int (aNumber). Also, a single = is for assignment. A double == is for comparison. You probably want the latter, and you probably don't want to call getNumber() twice in that case statement.

    Your getNumber function looks horribly broken. You're while loop is infinite and will only print "Enter a number...". Your if statement is missing a closing curly bracket }. I would use a do while loop, and I would probably recommend you use curly brackets for every if, while, for and do-while loop, even if you don't need them.

    And fix your indenting (look into the auto-indent feature of your editor/IDE), it will make several of these errors more obvious.

    There's quite a bit more work to do, but that should keep you busy for a while.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Ok, now I made to here. The compiler started going, then stopped on 'Link Errors'. My professor didnt even tell us about these...

    here are the messages:

    [Linker error] undefined reference to `prinf'
    [Linker error] undefined reference to `prinf'
    ld returned 1 exit status

    Code:
    #include <ctype.h> // to use the upper function
    #include <stdio.h>
    #include <stdlib.h>
    
    char getMenuChoice();
    int getNumber();
    
    
    int main() {
    //Declare variables here
         int aNumber = 0;
         float average = 0.0;
         int high = -1;
         int low = 1001;
         int total = 0;
         int count = 0;
         char getChoice = ' ';
         
         
         do{
            getChoice = getMenuChoice();
            switch (getChoice) {
                   
                   case 'A': 
                        aNumber == getNumber();
                        total += aNumber;
                        count++;
                        
                        if (aNumber = getNumber())
                           high = aNumber;
                        if (aNumber < low)
                           low = aNumber;
                        
                        printf("Inside of A\n");
                        break;
                        
                   case 'B':
                        
                        average = (double)total / count;
                        printf("The avg is %.2f\n");
                        system("pause");
                       break;
                   case 'C':
                        
                        printf("The Highest Value WAS %i\n", high);
                        system("pause");
                         
                        break;
                   case 'D':
                        prinf("The Lowest Number is %i\n", low);
                        system("pause");
                        break;
                   case 'E':
                        printf("Inside of E\n");
                        break;
                   case 'F':
                        prinf("Inside of F\n");
                        break;
                   case 'G':
                        printf("Inside of G\n");
                        break;
                        }//End switch
                        
            } while (getChoice != 'G');
         
       system("pause");
       return 0;
    }      // end of Main
    
    
    char getMenuChoice() {
         char result, choice = ' ';
         
         
           while (result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E' && result != 'D' && result != 'E'
         && result != 'F' && result != 'G') {
         
         system("cls"); //clear the screen
         
         printf("*********************************\n");
         printf("A) Enter a number between 0 and 1000\n");
         printf("B) Display the average\n");
         printf("C) Display the Highest Number\n");
         printf("D) Display the Lowest Number\n");
         printf("E) Display the total of all numbers entered so far\n");
         printf("F) Display how many numbers were entered during the session\n");
         printf("G) Quit the program\n");
         scanf("%c",&result);
         result = toupper(result);
         
         choice = getchar();
         fflush(stdin);
         choice = toupper(choice);  //BUILT IN FUNCTION; caps//lower recognized
         
         if (result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E' && result != 'D' && result != 'E'
         && result != 'F' && result != 'G') {
                   
                   printf("You must enter A - G only \n");
                   system("pause");
                   
                   }//End IF
                   }
         
         
         
        
         return result;
    }//End of getMenuChoice
    
    
    int getNumber () {
        int result = -1;
        
        while (result < 0 || result > 1000)
        
        
        printf("Enter a number between 0 and 1000: ");
        scanf("%i", &result);
        
        if (result < 0 || result > 1000) {
        printf("You must enter a number between 0 and 1000\n");
        system("pause");
        
        return result;
        
    }// End IF
    }//end of getNumber function

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    NEVER MIND!!! I found the link errors. i spelled the Printf's wrong lolol. And my loop is continuing out of control though. How would I fix the broken function. I dont see where to place the bracket
    Last edited by Wolf776; 10-07-2011 at 05:18 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For starts... your getMenuChoice function is not really a good plan... the menu and the command switch are so closely linked that you had to go into wild contrivancy to separate them... moreover it's only used in one place in the code. Move it back into the main() function and integrate it into your do-while() loop.

    In get number you are using system(pause) ... don't do that. Simply print the error message and let the function cycle back up to asking for information. The extra keystrokes will annoy the heck out of your operators...

    In fact don't use system() calls at all, if you can get out of it.

    For your loop running wild... it's very likely the user (you) is entering lower case characters which your convoluted menu system can't deal with.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    For starts... your getMenuChoice function is not really a good plan... the menu and the command switch are so closely linked that you had to go into wild contrivancy to separate them... moreover it's only used in one place in the code. Move it back into the main() function and integrate it into your do-while() loop.
    My professor is trying to get us to use functions. So we are made to do it that way.

    In get number you are using system(pause) ... don't do that. Simply print the error message and let the function cycle back up to asking for information. The extra keystrokes will annoy the heck out of your operators...

    In fact don't use system() calls at all, if you can get out of it.

    For your loop running wild... it's very likely the user (you) is entering lower case characters which your convoluted menu system can't deal with.
    Well either, ie: A or a, it still comes out the same.

    I was able to close the if statement, but im still rampant
    Last edited by Wolf776; 10-07-2011 at 05:51 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Wolf776 View Post
    My professor is trying to get us to use functions. So we are made to do it that way.
    Then he is teaching bad programming practice...

    A properly written function does one thing... parameters go in the top, answers come out the bottom.
    Splitting up tightly coupled code most often does nothing but cause problems... as you've discovered.



    Well either, ie: A or a, it still comes out the same
    EXACTLY what is it doing... It goes into the menu display and never comes back?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    I found the issue, I didn't put the brackets after the fflush; to stop the while. I think he was trying to show, that you can easily put stuff into functions

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Wolf776 View Post
    I found the issue, I didn't put the brackets after the fflush; to stop the while. I think he was trying to show, that you can easily put stuff into functions
    You may want to take a closer look at that setup... you are asking for input TWICE in that function (lines 88 and 91) and assigning them to two different variables...

    Moreover; fflush(stdin) is depricated (meaning you shouldn't use it in new code) because flushing input queues is highly unreliable and compiler dependent. Flushing is for output functions to ensure data is written, not input...

    Which do you flush at home... your taps or your toilet?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    fflush(stdin) results in undefined behavior, don't use it. Read Cprogramming.com FAQ > Why fflush(stdin) is wrong and Cprogramming.com FAQ > Flush the input buffer.

    As for the need for functions, don't put menu choice error checking in the menu function. Try something like:
    Code:
    char getMenuChoice(void) {
        char choice;
    
        system("cls"); //clear the screen
    
        printf("*********************************\n");
        printf("A) Enter a number between 0 and 1000\n");
        printf("B) Display the average\n");
        printf("C) Display the Highest Number\n");
        printf("D) Display the Lowest Number\n");
        printf("E) Display the total of all numbers entered so far\n");
        printf("F) Display how many numbers were entered during the session\n");
        printf("G) Quit the program\n");
        scanf("%c", &choice);
    
        // code to flush the input buffer you learned from the second link above
    
        return toupper(choice);
    }//End of getMenuChoice
    That uses a function, and does one thing and does it well. Now, the checking can be done in main. You can take advantage of the "default" case label for error checking:
    Code:
        char choice;
    
        do{
            choice = getMenuChoice();
            switch (choice) {
                case 'A':
                     // do A stuff
                    break;
                case 'B':
                    // do B stuff
                    break;
    ...
                case 'F':
                    // do F stuff
                    break;
                case 'G':
                    // do nothing, we're just going to quit
                    break;
    
                // this is a "catch-all" that will cover any invalid menu choice
                default:
                    printf("You must enter A - G only \n");
                    system("pause");
                    break;
            }//End switch
         } while (choice != 'G');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. troubles compiling valgrind with ulibc
    By cerr in forum Linux Programming
    Replies: 1
    Last Post: 01-19-2010, 01:24 PM
  2. cin.get() Troubles
    By yukapuka in forum C++ Programming
    Replies: 19
    Last Post: 06-04-2008, 02:30 PM
  3. troubles in using GDB
    By leiming in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 02:54 AM
  4. NIC Troubles?
    By Bajanine in forum Tech Board
    Replies: 4
    Last Post: 05-16-2004, 05:45 PM
  5. WINOLDAP (Compiling troubles)
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-15-2001, 02:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21