Thread: confused about arrays

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    Code:

    here is what i came up with, i can select menu, go to that option,back to menu, do something different, but can't get it to print out my array after all input has been stored. Any suggestions?



    #include <stdio.h>
    #define MAX 10
    int num_books(int[],int );

    void print_num_books(int [],int);
    int main(void)
    {

    int stock_amt[MAX];

    int n;
    while(n!=11)
    {

    printf("\nPlease choose an option below by typing a number between 1 and 11.\n\n");

    printf("1\tAutomobiles\n\n");
    printf("2\tCookbooks\n\n");
    printf("3\tGardening\n\n");
    printf("4\tHistory\n\n");
    printf("5\tMystery\n\n");
    printf("6\tPlanets\n\n");
    printf("7\tScience fiction\n\n");
    printf("8\tSelf Help\n\n");
    printf("9\tSports\n\n");
    printf("10\tTravel\n\n");
    printf("11\tExit program\n\n");


    n=num_books(stock_amt,MAX);
    print_num_books(stock_amt,n);
    }

    }
    int num_books(int stock_amt[],int lim)
    {
    int index=0,n;

    scanf("%d", &n);


    if(n==1)
    {
    printf("How many automobiles will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else if(n==2)
    {
    printf("How many cookbooks will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    if(n==3)
    {
    printf("How many gardening will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else if(n==4)
    {
    printf("How many History will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    if(n==5)
    {
    printf("How many Mystery will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else if(n==6)
    {
    printf("How many Planets will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    if(n==7)
    {
    printf("How many Science fiction will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else if(n==8)
    {
    printf("How many Self Help will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }

    if(n==9)
    {
    printf("How many Sports will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else if(n==10)
    {
    printf("How many Travel will Perfect book carry?");
    scanf("%d",&stock_amt[index]);
    }
    else return(index);
    }
    void print_num_books(int stock_amt[],int n)
    {
    int index;
    printf("\n***STOCK INVENTORY**\n\n");
    for(index=0;index<n;index++){

    printf("Amount is %d\n",stock_amt[index]);
    }
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use this as a base to work from:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int a[10];
      int index = 0;
      int i;
    
      while ( index < 10 ) {
        printf ( "1) Add a number\n2) Display numbers\n" );
        printf ( "Selection: " );
        fflush ( stdout );
    
        switch ( getchar() ) {
        case '1':
          printf ( "Enter a number: " );
          fflush ( stdout );
    
          if ( scanf ( "%d", &a[index++] ) != 1 ) {
            fprintf ( stderr, "Invalid input\n" );
            return EXIT_FAILURE;
          }
    
          break;
        case '2':
          for ( i = 0; i < index; i++ )
            printf ( "%d ", a[i] );
          printf ( "\n" );
    
          break;
        default:
          fprintf ( stderr, "Invalid selection\n" );
          return EXIT_FAILURE;
        }
    
        getchar(); /* Eat the newline */
      }
    
      return EXIT_SUCCESS;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertex Arrays
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 01-08-2006, 01:24 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM