Thread: Helllppp!!! I/O and array problems

  1. #1
    Registered User
    Join Date
    Jul 2005

    Helllppp!!! I/O and array problems

    The first part I'm having trouble with is having the user type in the file name to read in to the array.

    Second, I need to count the occurance of each element of the array and display the element next to it's number of occurances.

    The text file looks like this: 1 2 3 2 6 7 4 5 3 7 4 5 2 4 7

    Please help me!

    My code looks like this: (Don't laugh)

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    void sort(char a[], int number_used);
    void swap_values(char& v1, char& v2);
    int index_of_smallest(const char a[], int start_index, int number_used);
    int main()
    	char input_file[21];
    	int number_used;
    	ifstream in_stream;;
    	cout<< "Please enter the name of the input file.\n";
    	cin>> input_file;, 21);
    	sort(input_file, number_used);
    	while (!in_stream.eof())
    		for (int index = 0; index < number_used; index++)
    		cout<< input_file[index] << " \n";
    	return 0;
    void sort(char a[], int number_used)
    int index_of_next_smallest;
    for (int index = 0; index < number_used - 1; index++)
    index_of_next_smallest = index_of_smallest(a, index, number_used);
    swap_values(a[index], a[index_of_next_smallest]);
    void swap_values(char& v1, char& v2)
    int num;
    num = v1;
    v1 = v2;
    v2 = num;
    int index_of_smallest(const char a[], int start_index, int number_used)
    int min = a[start_index],
    index_of_min = start_index;
    for (int index = start_index + 1; index < number_used; index++)
    if (a[index] < min)
    min = a[index];
    index_of_min = index;
    return index_of_min; 
    Last edited by Salem; 07-02-2005 at 01:49 AM. Reason: tag fixer - the OP needs to fix the indentation

  2. #2
    Join Date
    Mar 2005
    could you please put code tags over all the code so its easier to read and debug... thanks

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    first off,
    	cin>> input_file;, 21);
    what you probably want there is:
    second, when you hit sort(), what exactly are you trying to sort? and finally, when you hit your while loop, you check for eof on a stream you never opened.

    I didn't check anything past that, but fix that and see where it gets you. one more thing: I see you've included <string>, but don't use any strings anywhere... I'm not so sure you'd need <cstdlib> either...

    since you're reading in integers, you can cheat:
    1. create an int array and initialize to zero (make sure the highest index >= highest # in file)
    2. get the filename from the user
    3. open and check the file
    4. read in a number, and increase the array in that index by 1
    5. when you're done, close the file and go through the file printing out the index and number at that index
    for example, if you read in a 7, increase integers[7] by 1. at the end, your array will look like (using your sample input):
    integers[0]: 0
    integers[1]: 1
    integers[2]: 3
    integers[3]: 2
    integers[4]: 3
    integers[5]: 2
    integers[6]: 1
    integers[7]: 3
    just throw that into a loop that outputs the index and number in that index and you're all set.
    Last edited by major_small; 07-01-2005 at 08:46 PM. Reason: get/getline
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Jul 2005

    Thanks for the Help!

    I'm having other problems, now but you have helped me so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 07:26 PM
  2. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM