Hello again guys,
I am trying to sort an array. First i must get the info from a txt file. The txt file has numbers and the only way i can put them into an array is to put them into a char array. Now my problem is i need that to be a string not a char. I am very lost :(

char numbers[100];
	

	while(!infile.eof()){
	infile.getline(numbers,100);
	}

the numbers in the txt file are as long as 10,000 per line. The char string doesn't pick up on that. I don't know what to do :( can anybody shed some light on this topic? Thanks!

Can you be more specific? Why do you need string?
Maybe you can use vector<char> instead of array of chars?

The txt file has numbers and the only way i can put them into an array is to put them into a char array.

Why?!..
Do you know that unsigned char range is 0..255 only?

the numbers in the txt file are as long as 10,000 per line. The char string doesn't pick up on that.

Why? What compiler and OS are you using?

I am using Visual Studio 2008. It gives me an error when i make it a string array. Says it can't convert it from string to a char * so thats why i made it a char. But ya the char doesn't store all the numbers so thats why i was wondering how can i do this...


The char string doesn't pick up on that.

Sorry, I mean't to say char array. :)

ok i did this instead... and it's running but gives me an overstack/overflow error.

string numbers[100000];
	

	for(int x=0; x<100000; x++)
	{
	infile>>numbers[x];
	
	}

I think it is better for you to use a vector You Use A Vector to store in all the strings in your string. This pretty much acts like an array. So you will not be having much problems with it i guess

vector<string>numbers;
string temp;	
while(!infile.eof()){
	infile>>temp;
numbers.push_back(temp);
}

Well, if you have a file with numbers, why string array or string vector? Use a vector of numbers or an array of numbers:

std::vector<int> v; // or vector<double>
v.reserve(10000);
int x;
while (infile >> x)
    v.push_back(x);

That's KISS principle ;)

lol Thank you so much ArkM you openeded up my mind completely, i don't know why i was making an array of strings haha. I switched it to an integer array and it's working perfectly now, no vectors needed.

ok guys im having time passing an array by reference. I have it in one function and i need to get the array to the next function. I would have to pass it to main and then from main to the next function i believe. I am doing that but it's giving me errors. Help please :(

#include <iostream>
#include <fstream>
#include <string>

using namespace std;



void openInputFile(ifstream&infile);
void readInputFile(int,ifstream&infile); 
void selectionSort();


int main(){
	
	
	int list[100];

	
	
	
	
	ifstream infile;





openInputFile(infile);
readInputFile(list,infile);
selectionSort(list);






system("pause");
return 0;
}

void openInputFile(ifstream&infile) 
{
	char filePath[200];
	

	
	cout << "Enter path of file:";
	cin >> filePath;

	infile.open(filePath);
	
	if(!infile){
		cout << "There was an error opening the file" << endl;
	}else{
		cout << "The file was able to open correctly" << endl;
	}


}

void readInputFile(int *list[],ifstream&infile){

//taking the 100 numbers from file and dumping into array


	int numbers[100];
	
	

	for(int x=0; x<100; x++)
	{
	infile>>numbers[x];
	
	}

cout<<numbers[4];


}

void selectionSort(int list[]){



[B]//i need to get the array here to sort it[/B]

}

First of all, you have code:

void openInputFile(ifstream&infile);
//it should be:
void openInputFile(ifstream& infile);

Second:

void readInputFile(int *list[],ifstream&infile);
//int *list[] doesn't make any sense. You have two options:
int *list //or
int list[]

Third: take a look at

void readInputFile(int,ifstream&infile); 
//and
void readInputFile(int *list[],ifstream&infile){
//...

They're not the same, fix this.

Fourth: In readInputFile you use
int numbers[100];
to store data.
But you should use list[], you want to fill that array?

Try these modifications, and then see if it works

hey thanks for the help. Got the program to run :)...now when i do a quick test to make sure i have the same array in the selectionsort function as the read function

void readInputFile(int *list,ifstream& infile){

	int numbers[100000];
	

	for(int x=0; x<100; x++)
	{
	infile>>numbers[x];
	
	}

[B]cout<<numbers[4];[/B]


}

void selectionSort(int *list){



int numbers2[100];

[B]cout<<numbers2[4];[/B]

It gives me a big error and freezes the whole Visual Studios. I don't know what's happening

You still use array numbers, although you don't need them. In
readInputFile
use *(list+i) instead of numbers, like:

infile>>*(list+x);

And write selectionSort.

Another question: why do you print out numbers[4]?

I will delete that i just wanted to see if the array was passing correctly. Let me apply what you just said and ill get back to you. Thanks!

Your a genius. Thank you so much :) Everything is working. Now to sort it lol Thanks again!!!

Don't mention it. And, in C++ spirit, I'm obliged to tell you to look at vectors, because that's better for handling a large arrays.

This article has been dead for over six months. Start a new discussion instead.