ok guys I need your help. Im still new to C++ and what I need to do is read a .txt file into an array. The file consits of number like this ie 345 55555 56788765 5444 333 22 3 55656757 etc.... then I need to sort them using the bubble sort and resave them to the file after they are sorted. Ok my issue is writing the file into an array. I know how to do the bubbloe sort but cant get the array to work properly. I get it to read the txt file b/c i test with a cout but it goes to the last few numbers and i get a run time error about maxSize being used with about be initialized but it is. help

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputFileName(); // a function to prompt for the complete file name
void bubblesort( string num,int maxSize);


int main ()
{ 
	int maxSize;
	string num;
	//int num_array[501];
	ofstream outFile;
	ifstream inFile;
	string fileName; // complete file name including the path

	fileName = getInputFileName(); // prompt and obtain the full file name

	// try to open the file
	outFile.open(fileName.c_str(),ios_base::binary | ios::out| ios::app);
	inFile.open(fileName.c_str(),ios_base::binary);

	if(!outFile.is_open())
	{
		cerr << "File open Error Creating file" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!inFile.is_open())
	{
		cerr << "File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}

while (!inFile.eof())
{
	getline (inFile,num);
cout << num;
}
	//int maxSize;
	
 	bubblesort(num,maxSize);
	outFile.close();
	inFile.close();





	// keeps program opening till user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}




//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//             
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value: 
//               
//
//************************************************************
void bubblesort(string num,int maxSize   )
{ 
	int i,j;
	
	for ( i=1; i< maxSize; i++)
	{ 
		for ( j = maxSize-1;j>=i;j--)
		{
			if(num[j] < num[j-1])
				swap ( num[j-1],num[j]);
		}
	}
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************

string getInputFileName()
{
	string f_Name; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

So you read the entire file into the single string num

while (!inFile.eof())
{
	getline (inFile,num);
cout << num;
}

Then you call bubblesort() passing in the one string value num and sort this one value maxSize times.

bubblesort(num,maxSize);
	outFile.close();
	inFile.close();

What happens when you try to access num[1] , num[5] , etc?

Do you need to think this through a little more?

so should i put the file into an array ie num_array[500] and read the file into the array so i can access num[1] num[5] etc.. and sort them would getline put them in an array though

As long as you are reading from cin in the code, using
progname <filename.txt
will read the numbers from filename.txt

so should i put the file into an array ie num_array[500] and read the file into the array so i can access num[1] num[5] etc.. and sort them would getline put them in an array though

Yes . But one point of caution. Make sure the size of the file is less than 500. Also you are going into a string but you need is numbers. You will have to use the appropriate conversion methods

Sorry, wrong answer.

Read the numbers into num like you did, but you need to convert them into numbers (atoi() would do it) and load them into that array.

I dont think my teacher will accept that b/c we havnt learned it yet.Is there a way to just read the file into an array?

ok here is my new code with arrays it runs but, when i write out the result to the text file i get the numbers in red added on instead of the numbers sorted...


15437 5579 30403 20723 15051 1275 31856 3192 20446 26400
2346 1804 20246 10454 24880 5574 5060 26748 22640 28586
8296 26589 3486 19567 21101 16655 23428 24710 32276 25244
29849 23127 1711 5856 23764 17614 18191 6834 13762 29462
24127 1863 4311 22948 13427 4946 15362 30840 19515 28528
15491 15007 13308 16836 27649 28413 11169 1246 27607 15945
21134 19938 25264 16985 29141 15846 14419 8864 13997 8859
17344 23029 28960 2608 8059 2453 11011 20083 6184 32108
11051 23354 26968 17808 14558 13313 15523 2319 13192 31956
7927 30186 21167 2672 24623 29281 8745 15781 4327 29553
3725 11774 4751 7324 5607 26532 28095 3304 28848 16512
25290 32042 18173 2208 26774 4996 17910 6620 3499 4141
10634 2745 28553 14192 3681 2189 15423 7416 8210 7396
29241 13386 30615 25123 21082 19091 15870 27499 14411 19876
26464 25536 19730 1083 6679 10347 18406 7044 5676 25244
12609 11743 12559 25684 23608 9956 14731 19586 2568 10404
22520 13933 27633 22066 16051 30801 30535 10083 16075 13645
16326 10705 5373 18044 19494 21286 739 16227 6383 18583
2152 4491 15759 16302 20863 4974 26537 23629 11285 28312
8369 12272 6723 14598 4534 15922 21705 15826 14718 10899
20920 14610 18648 29560 9070 2813 6446 24179 17351 9807
29018 31173 8082 15855 3258 22305 30274 30159 6279 25523
25062 6932 7010 353 7004 10104 31536 17745 20161 5100
5416 32683 5163 16001 7210 28832 24731 26504 12627 20139
813 2552 27342 534 658 4881 15597 14785 28352 15679
31096 15647 31464 4673 22853 8065 29609 22666 17416 14487
25987 28648 19053 1587 1934 24931 1636 23377 15980 9832
21948 85 19324 27431 8948 10387 31824 20537 12366 24852
28258 28550 6436 23703 15106 10342 32127 16599 8083 30470
3046 22796 30199 23310 23924 20916 4424 4480 22574 24979
21193 9699 2963 1017 2844 7842 15220 10307 31669 18607
3333 31396 3214 26219 2613 10790 2588 17866 28475 20459
32643 434 30321 23481 262 29349 5734 10678 31425 4768
26158 9583 22426 22595 7165 7711 5237 19761 23029 12335
5031 25530 15675 26617 17581 20408 25424 465 29727 26294
22943 27462 22495 19288 11090 27045 11158 26615 362 2495
29025 31883 2319 15596 10951 17358 5156 18256 7663 29598
10271 25437 31503 7331 28440 16025 30248 11571 16159 13218
10481 7026 11219 25967 20752 31422 22065 8152 16016 74
28329 8537 1680 15112 7543 12819 20024 717 17712 11283
16882 25264 21403 22743 3286 691 5315 18446 25701 21669
15434 928 31867 2118 6400 17646 30591 2733 9908 3931
9949 5351 29259 21515 12214 3553 1039 747 26582 17287
17481 11505 32486 16174 3601 7077 18402 14639 25701 480
14030 28629 32121 10707 26277 25978 6222 17484 15251 18924
20614 15669 30051 4886 25719 14685 17979 2268 8574 8572
16775 18502 27285 24177 14131 481 3368 10957 20253 30562
22565 7573 30922 31885 10579 21060 856 32597 25828 21008
29107 23365 2893 8814 24332 31774 24917 7384 1927 5058
24172 29646 18740 26448 21823 2331 12609 30609 2697 31065
31065-858993460-858993460

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputFileName(); // a function to prompt for the complete file name
int bubblesort( int num[501],int maxSize);


int main ()
{
	int maxSize=501;
	int num[501];

	ofstream outFile;
	ifstream inFile;
	string fileName; // complete file name including the path

	fileName = getInputFileName(); // prompt and obtain the full file name

	// try to open the file
	outFile.open(fileName.c_str(),ios_base::binary | ios::out| ios::app);
	inFile.open(fileName.c_str(),ios_base::binary);

	if(!outFile.is_open())
	{
		cerr << "File open Error Creating file" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!inFile.is_open())
	{
		cerr << "File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}
	int index;
int i=0;


	while (!inFile.eof())
	{
		for (index = 0; index < 501; index++)
		{
			inFile >> num[i];  
		i++;
		}
	}


	//int maxSize;

	num[501]=bubblesort(num,maxSize);
	outFile << num[501];
	outFile.close();
	inFile.close();





	// keeps program opening till user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}




//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//             
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value: 
//               
//
//************************************************************
int bubblesort(int num[501],int maxSize   )
{ 
	int i,j;
maxSize = 501;
	for ( i=1; i< maxSize; i++)
	{ 
		for ( j = maxSize-1;j>=i;j--)
		{
			if(num[j] < num[j-1])
				swap ( num[j-1],num[j]);
		}
	}
	
	return  num[501];
	
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************

string getInputFileName()
{
	string f_Name; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

Edited 6 Years Ago by smeghead007: n/a

Change the sequence of steps that you carry out to this.
1. Open the file .
2. Read the data
3. Close the file.
4. Sort the data
5. Open the file
6. Write the data
7. Close the file

Also print the contents of the array num on line 64 to see if you have sorted the data correctly

Edited 6 Years Ago by abhimanipal: n/a

i get the same thing i think it has something to do with the way im filling in the array but not sure

Comments
help sir

Check the bubble sort. Reduce the input to just 5 numbers. It will be easier for you to debug
Check what is the maximum size of int in your system. I think the input numbers are greater than the maximum allowable. That could be one reason why bubble sort is failing

Edited 6 Years Ago by abhimanipal: n/a

still getting the same thing its adding on or returning a negatvie number -858993460 so its either garbage or the way im filling up the array is wrong or my buble sort

still getting the same thing its adding on or returning a negatvie number -858993460 so its either garbage or the way im filling up the array is wrong or my buble sort

I think your input values are exceeding the maximum allowed value for int in your system. Either change it from int to long. Or reduce the value of the input data

so hard..

sir can i have a question ?

Edited 6 Years Ago by peter_budo: Do not flood forum with unnecessary posts

while (!inFile.eof())
	{
		for (index = 0; index < 501; index++)
		{
			inFile >> num[i];  
		i++;
		}
	}

Why are you putting a for loop inside a while loop? Get rid of the for loop.

while (!inFile.eof())
	{
		for (index = 0; index < 501; index++)
		{
			inFile >> num[i];  
		i++;
		}
	}

Why are you putting a for loop inside a while loop? Get rid of the for loop.

dont i need the for loop. The while loop is reading the file the innor for loop in filling the array

dont i need the for loop. The while loop is reading the file the innor for loop in filling the array

No, the while loop is for filling the array. The for loop is useless. Remove the for statement. It serves no purpose. Don't be a smeghead about it.

Edited 6 Years Ago by WaltP: n/a

Ok I got the code to work but with some errors. First when i print out the aorted array to the file it is sorted escept for one number in the beginning second it gives me a negative number whihc i believe is a pointer how can i fix this? I'll include a copy of the print out.
Thanks

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputFileName(); // a function to prompt for the complete file name
void bubblesort(ifstream& inFile, ofstream& outFile);


int main ()
{
	int num[501];

	ofstream outFile;
	ifstream inFile;
	string fileName; // complete file name including the path

	fileName = getInputFileName(); // prompt and obtain the full file name

	// try to open the file
	outFile.open(fileName.c_str(),ios_base::binary | ios::out| ios::app);
	inFile.open(fileName.c_str(),ios_base::binary);

	if(!outFile.is_open())
	{
		cerr << "File open Error Creating file" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!inFile.is_open())
	{
		cerr << "File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}












	//int maxSize;

	bubblesort( inFile, outFile);
	outFile.close();
	inFile.close();





	// keeps program opening till user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}




//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//             
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value: 
//               
//
//************************************************************
void bubblesort(ifstream& inFile, ofstream& outFile )
{ 
	int i ;	
	int k = 0;
	int num[501];
	int count = 0;


	while(!inFile.eof() ) // Input The Numbers from The File Into The Array NumbersIn[]
	{ // While i<100 And While File Is Not End Of File. 
		inFile>>num[k];
		k++;
	}

	int j;
	int  maxSize = 501;
	for ( i=1; i< maxSize; i++)
	{ 
		for ( j = maxSize-1;j>i;j--)
		{
			if(num[j] < num[j-1])
				swap ( num[j-1],num[j]);
			count++;
		}
	}
	for (int i = 0; i < maxSize; i++)
	{	
		outFile << num[i] << "   "  << endl;



	}
	outFile << " You swapped numbers" << "  " << count << "  " << "times" <<endl;
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************

string getInputFileName()
{
	string f_Name; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

here is the printed to file output. I removed a bunch of the numbers since there is 500 of them but the first two as you can see are wrong.

15437
-858993460
74
85
262
353
362
434
465
480
481
534
658
691
717
739
747
813
856
928
1017
1039
1083

You swapped numbers 124750 times

Edited 6 Years Ago by smeghead007: n/a

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