help with arrays and functions

This is a discussion on help with arrays and functions within the C++ Programming forums, part of the General Programming Boards category; i'm trying to build a program that will read a text file, and take elements from that file, store them ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    help with arrays and functions

    i'm trying to build a program that will read a text file, and take elements from that file, store them into arrays, and then display those arrays. i'm not nearly close to a finished product yet, as i'm in way over my head here. but just with what i have, when i try to run it, just to see how it's working, the input file "data7.txt" is crashing (by which i mean, a windows dialogue box pops up telling me it's not functioning, and it will check for a solution, that thing) it happens each time. with what code i have at the moment, what might be causing this? if it makes any difference, the whole assignment can be found here, in case that gives any clues as to what i'm screwing up. i have no experience with arrays and am overwhelmed. i'm trying to go from top to bottom, but perhaps that doesn't work? i don't know, i'm lost. any advice would be much appreciated. https://webcourses.niu.edu/courses/1..._1/240pgm7.htm
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    const int LENGTH = 30;
    
    int family[LENGTH];
    
    int item[LENGTH];
    
    int quantity[LENGTH];
    
    double price[LENGTH];
    
    int buildArrays (int family[], int item[], int quantity[], double price[]);
    
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	int i;
    	cout<<family[i]"\n"<<item[i]"\n"<<quantity[i]"\n"<<price[i];
    }
    int main()
    {
    	int i;
    	int num;
    
    
    
    
    
    ifstream inFile;
    
    inFile.open( "data7.txt" );
    
    if( inFile.fail() )
    {
    	cout << "text failed to open";
    	exit( -1 );
    };
    
    while (inFile)
    	{
    	buildArrays;
    	inFile>>num;
    	family[i]=num;
    	inFile>>num;
    	item[i]=num;
    	inFile>>num;
    	quantity[i];
    	inFile>>num;
    	price[i];
    	i++;
    	printArrays;	
    	
    	}
    	
    	
    	
    	
    return 0;
    }
    the data7.txt file contains this
    Code:
    3 12345 1 15.95
    2 67800 3 50.00
    4 32145 2 4.50
    6 98765 1 75.00
    1 44496 2 11.30
    0 67356 4 3.50
    5 54862 1 52.50
    4 14541 1 4.25
    2 64488 2 1.00
    0 34945 3 7.40
    6 74322 1 6.50
    1 39240 10 0.75
    3 52525 15 1.00
    0 60002 1 12.00
    5 31579 2 2.50

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    What does buildArray do? All you've given is a function declaration.

    Code:
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	int i;
    	cout<<family[i]"\n"<<item[i]"\n"<<quantity[i]"\n"<<price[i];
    }
    The variable i is uninitialized. You've declared a variable, of type integer, named i. What does i equal? Is it 0? Perhaps, but we've no way of knowing for sure. Get into the practice of initializing your variables. However, initializing i to anything doesn't solve the problem. What I think you want is more along the lines of:

    Code:
    void printArray(int index) {
        cout << family[index] << endl;
        cout << item[index] << endl;
        cout << quantity[index] << endl;
        cout << price[index] << endl;
    }
    Also look at the way you're calling the function you've defined. You have a function that takes 4 arguments, yet you're calling it with zero arguments (actually, you aren't calling it at all: you're program won't compile)

    Code:
    printArray;
    Isn't the same as

    Code:
    printArray();
    Also note that "num" is of type integer, yet data7.txt contains the price of items (15.95, 4.50, 50.00, etc...), which you cannot store in an integer.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	int i;
    	cout<<family[i]"\n"<<item[i]"\n"<<quantity[i]"\n"<<price[i];
    }
    What do you expect i to be here, and why?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Please get a good debugging tool (MSVS, GDB) and go through your code line by line. This method will help you to resolve some big mistakes you have done here. Then ask the people what you have missed if something still doesn't work.
    Please help yourself first, it is pleasure to help those who deserve the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizes of arrays within functions
    By tempster09 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 11:17 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 10:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM

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