Help 4 newbie w/ I/O into array

This is a discussion on Help 4 newbie w/ I/O into array within the C++ Programming forums, part of the General Programming Boards category; The first part I'm having trouble with is having the user type in the file name to read in to ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Help 4 newbie w/ I/O into array

    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>
    #include<string>

    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);
    Code:
    int main()
    {
    	char input_file[21];
    	int number_used;
    	ifstream in_stream;
    	in_stream.open(input_file);
    	
    	
    	cout<< "Please enter the name of the input file.\n";
    	cin>> input_file;
    	cin.read(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";
    	
    		in_stream.close();
    	}
    	
    	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 dorky; 07-01-2005 at 06:23 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    We'll need a permission slip from your teacher
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    getline(cin, string, '\n');
    
    file.open(string.c_str, ios::in);
    
    while(!file.fail())
    {
        file >> myINT;
        vectorint.push_back(myINT);
    }

    there i didnt declare any variable for you
    youll have to do that, and i didnt do the last part
    for you cause im lazy, but maybe some more
    help will be provided soon as you show people what youve
    done and where your having problems.
    Last edited by ILoveVectors; 07-01-2005 at 11:15 AM.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by ILoveVectors
    there i didnt declare any variable for you
    ...Which is a fairly simple and mindless task. Try not to post solutions for homework problems.

    <pedantic>
    I have to wonder how you would declare those variables, considering that you didn't fully qualify getline. From that, one could guess that you'd have either a using std::getline; or a using namespace std;. Then how would you create a std::string named string?
    Code:
    getline(file, string, '\n');
    You could fully qualify the variable declaration (std::string string;), but it seems inconsistent to do so after you've already used a using statement above for getline.
    </pedantic>

    Further, vectors can work in this situation, but aren't really ideal. A map<int,int> makes it much simpler. Additionally, the OP said he was reading the numbers into an array, which makes me believe that the class hasn't yet learned any STL.

    To the OP: Show us your attempt, and we'll see what we can do to help you.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    it was kind of a sarcastic post, ive seen you guy do it many
    time figured i could of. not to mention with there being alot
    wrong its not really a solution, but it an idea.
    Last edited by ILoveVectors; 07-01-2005 at 11:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. newbie needs File I/O tips
    By JohnY in forum C Programming
    Replies: 2
    Last Post: 02-12-2002, 12:33 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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