Thread: Array not outputting properly

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Array not outputting properly

    Mainly due to the fact my book barely explains array's and doesn't really tell how to manually take user-input and feed it into an array, I am having some issues as of now.

    I am trying to take three user defined inputs and store them into my array int real[2].
    I get no compile errors as it is, and I am more or less wondering if someone can help me fix up whatever it is that needs fixing. I'v checked online for how to manually put values into an array, however it seems everything I see does it via a counter, which doesn't help me at all. Any help would be appreciated, and i'll post my code below.

    (Not finished, just using a printf statement to try and get it so the output is actually real and I know it works properly.)

    Code:
    /* Loan Calculator
       Name: Tyler Sinclair-Day
       Purpose: To create a calculator which will use a loan calculation to display a table in which how long a loan will take
       to pay of, as well as how much each month will be paid, showing other information as showed in the program
    */
    
    #include <stdio.h>
    #include <math.h>
    
    
    int menu(void); //Prototype for int menu(void) function
    
    
    int main(void)
    {    
        
        int real[2];
        //Printf showing options to user as what each number represents for entering data//
        printf("--------------------\nEnter in the Numeric \nCharacter next to your choice"
                                           "\n--------------------\n\n");
                                           
        printf("||1. Enter the Principal                     ||\n"
                     "||2. Enter the annual interest rate          ||\n"
                     "||3. Enter duration of loan in months        ||\n"
                     "||4. Calculate your loan payments.           ||\n"
                     "||5. Show Loan Table.                        ||\n"
                     "||0. Exit program                            ||\n");
    
        real[2] = menu(); //Calling menu function
        
        
        //printf("Month::Old Balance::Payment::Interest::Principal::New Balance");
        printf("%d and %d and %d", real[0], real[1], real[2]);
        
        
        getchar();
        getchar();
        return 0;
    }
    
    int menu(void)//Menu function for gathering information from user.
    {
        //int principal;
        //int rate;
        //int months;
        int real[2];
        
      
        while ((scanf("%d", &choice)) != 0)//Runs until recieves '0' than quits
              {
                  switch (choice)//Case used for gathering data
                  {
                         case 1://Gets data for principal
                              printf("What is the principal you wish to enter?:\n");
                              scanf("%d", &real[0]);
                              break;
                         case 2://Gets data interest rate
                              printf("What is the annual interest rate?:\n");
                              scanf("%d", &real[1]);
                              break;
                         case 3://Gets data for months
                              printf("How many months in which does the loan need to be paid?:\n");
                              scanf("%d", &real[2]);
                              break;
                         case 4://Calculates table
                              printf("Your payment chart is calculated.\n");
                              break;
                         case 5://Prints table out
                              printf("Below is your loan table.\n\n\n");
                              break;
                         case 0:
                              return 0;
                         default://Will display message if number >5 or <1 is entered.
                                 printf("You entered a value outside of 1 to 5.\nPlease only use 1, 2, 3, 4, 5 or 0 to exit.\n\n");
                  } 
              }
              return real[2];
    }
    
    
    
    
    
                  /*for(int i = 0;i < months; ++i)
                     {
                     printf(" new balance: %d ", newbalance);
                     
                     
                     principal = principal * (1 + (rate / 12));
                     oldbalance=oldbalance-principal;
                     newbalance=oldbalance;
                     }*/

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Why is this a new thread?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by mike65535 View Post
    Why is this a new thread?
    The other was about using return [variable]; and my question was never really answered. After some fiddling around I got something working with Array's, and it's now a different question? Why the Array itself is outputting values that seem to be stored in said space from some previous .c file or from god knows what else?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    C has something called "rules of scope" which put into simple terms means that whenever you have a variable in a scope block, denoted by curly braces, there can only be one variable named that, and the variable closest to the executing code is the one that is used. Meaning:
    Code:
    int x = 0;
    {
      int x = 2;
      printf("x=%d", x);
    }
    This prints "x=2" because the x which is 2 is closer to the printf(). Once we leave the block after the line with the closing brace, x is 0 again.

    It should follow then that the real declared in menu() is different from the real declared in main(), for basically the same reason. You have to pass in real to menu() and I showed you that earlier in your other thread.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    /* Loan Calculator
    Name: Tyler Sinclair-Day
    Purpose: To create a calculator which will use a loan calculation to display a table in which how long a loan will take
    to pay of, as well as how much each month will be paid, showing other information as showed in the program
    */
    Ok if this is your task... using a menu system is plainly unwise. It makes the program awkward to use and far more complex than it needs to be.

    Just ask the questions in order...
    Code:
    #include <stdio.h>
    
    int main (void)
      { float principle = 0.0, interest = 0.0, payment = 0.0, balance = 0.0;
    
         printf("Loan payment calculator\n\n");
         printf("Principle\t: ");
         scanf("%f", &principle);
         printf("\nInterest\t: ");
         scanf("%f", &interest);
         printf("\nPayment\t: ");
         scanf("%f",&payment);
    
         // ok information gathered
         // do calculations and present report
    
       return 0; }
    Go ahead, try it out... you'll find out this is a 25 line program that is a whole lot easier to write without a bunch of complex bullsmut that only gets in the operator's way....

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by CommonTater View Post
    Ok if this is your task... using a menu system is plainly unwise. It makes the program awkward to use and far more complex than it needs to be.

    Just ask the questions in order...
    Code:
    #include <stdio.h>
    
    int main (void)
      { float principle = 0.0, interest = 0.0, payment = 0.0, balance = 0.0;
    
         printf("Loan payment calculator\n\n");
         printf("Principle\t: ");
         scanf("%f", &principle);
         printf("\nInterest\t: ");
         scanf("%f", &interest);
         printf("\nPayment\t: ");
         scanf("%f",&payment);
    
         // ok information gathered
         // do calculations and present report
    
       return 0; }
    Go ahead, try it out... you'll find out this is a 25 line program that is a whole lot easier to write without a bunch of complex bullsmut that only gets in the operator's way....
    Ya, I know this can be done in 25 lines. Actually. This can be done in ONE line. However, directions dictate it has to be done with a menu function.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Directions from your teacher?
    Is your teacher even a programmer... really, has he written anything worthwhile?

    It does not help you one bit to give you an assignment that demands unnecessary complexity.
    In fact it's likely to teach you some very bad coding practices.

    In your place I'd hand in the simple calculation assignment like I outlined above (because that's all it is) but I would wire it up with a very simple menu...
    Code:
    int Menu (void)
      { printf("Main Menu\n");
         printf("1. Continue"\n);
         printf("2. Quit\n\n");
         printf(Choice?  ");
    
         ...

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    If you are stuck with a menu, then you are stuck. Originally I thought this menu was to implement the classic "enter any three and I'll tell you the fourth" kind of thing (you know, enter term, rate and payment and I'll tell you the principal, etc.)

    (Yes, it's principal, not principle.)

    Clearly, though, a menu here is out of place since one must ask ALL the questions.

    Of course, one *could* take advantage of the menu idea and print out the current values entered into each (term, rate, etc) each time the menu is displayed giving the user a clear indication of what he's entered already:

    Code:
    Your current selections are
           principal: $500
       interest rate: 5%
        term (in mo): 24
    
    Loan payment calculator
    1) Enter new Principal
    .
    .
    .

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    (Yes, it's principal, not principle.)
    Schazbut! I always get those two confused.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    the first thing I saw -- You have
    Code:
    int real[2];
    Exactly what do you think this does? And if you guess correctly, what's wrong with
    Code:
    printf("%d and %d and %d", real[0], real[1], real[2]);
    ?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting an Array of 5 Numbers in Order
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2009, 07:20 AM
  2. Problem Outputting Array Element
    By bulletbutter in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2008, 03:51 PM
  3. Outputting an Array, what's wrong...?
    By fp123 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 03:22 PM
  4. outputting words in array of text
    By hopeolicious in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2005, 12:28 PM
  5. What am I doing wrong outputting this array
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2003, 05:32 PM