Hello everyone, I am starting to run completely out of time on an assignment that is due tomorrow. No I'm not asking for free code here, because I already have it 99% complete.

My assignment is to read a text file into an array and bubble sort them from highest to lowest. The specific file that we have to use has 19 as the first value. The 19 serves as a unit of information pertaining to how many data values are in the file. Now my current issues are that I need the 19 to not show up in the display and the other issue is that the last value in the text file is not being read. the last value in the file is 77.7 . Can some one please help me out on this? I am at yet another brick wall. Thanks in advance.

P.S. here is the data file values:
E:\numbers.txt
19
12.6
7.2
8.0
3.9
100.8
98.6
21.50
1.6
5.6
32.6
16.6
1.9
12.2
0.7
80.0
15.9
55.01
47.0
77.7

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int N=19;
double y[N];


void read_file()
{
	int i=0;
	char name[20];
	double temp;

	ifstream emma;
	cout<<"Enter a file name."<<endl;    
    cout<<"This Program limits file names to a maximum of 19 characters."<<endl;        
    cin>>name;       
 
    emma.open(name, ios::in); 
	
	if (emma.fail())          
        {                        
                cerr<<"File could not be opened"<<endl;       
                cin.get();                  
                exit(1);       
        }          

	emma>>temp;	

	while (i < N  && !emma.eof())	
	{
		y[i] = temp;				
		i++;					
		emma>>temp;			
	}
}

void orig_data()
{
	int i=0;

	ofstream emma_1;

	for(i=0; i<N + 1; ++i)
	{
		emma_1<<y[i];
		cout<<setw(15)<<left<<y[i]<<endl;
	}

	emma_1.close();
}

void bb_sort()
{
	double temp_1;

	for(int i=1; i<N; i++)
		for(int j=0; j<N-1;j++)
		{
			if (y[j]<y[j+1])
			{
				temp_1 = y[j];
				y[j]= y[j+1];
				y[j+1]= temp_1;
			}
		}
		
}

void main()
{
	read_file();
	
	cout<<"The original order of the data values is:"<<endl;
	orig_data();
	cout<<"     "<<endl;
	bb_sort();
	cout<<"         "<<endl;
	cout<<"The data values from highest to lowest is:"<<endl;
	orig_data();
}

Which line of your code reads the 19?
What do you do with that 19 after it has been read?
What will your code do if your professor decides to test it on a file containing 20 values?

the while loop statement reads the 19 due to that value being the first in the text file to be tested. the only thing that i want done with the 19 is for it to establish the size of the array and then be disregarded as far as sorting concerns. if my professor attempts to use this code on a text file with more values then this one would not work. he simply wanted us to apply this code to this particular file. I am a real noob rookie when it comes to C++ and it would take me forever to figure out how to make this program bubble sort any text file with numerial values. it took me a week to get where i am now with this current program. did that help with your questions?

Edited 6 Years Ago by decoy684: n/a

ok so maybe it is like 80% complete with a minor issue. i have just been staring at the program trying to figure out what is wrong with it. im not that good with C++ as you can probably tell and i cant find any answers as to why my program wont simply use the 19 to assign the size of the array and why it wont read the last value in the file. its killing me. and its due today lol.

ok i managed to get rid of the value 19 from the array and only use it to assign the size of the array, but it still wont read in the last value in the file. Any advice?

ok i managed to get rid of the value 19 from the array and only use it to assign the size of the array, but it still wont read in the last value in the file. Any advice?

Not without seeing your current code...

Best guess, the conditional controlling your loop isn't correct.

Edited 6 Years Ago by Fbody: n/a

There are two mistakes in your program. One in the while loop that in the read_file() function and the other in line 46. Figure them out yourself

ok i found what was wrong in line 46. and the only way that i was able to get rid of the value 19 was to have the loop start at the actual first value to be read into the array. i still can not however get the last value 77.7 to appear in the array. guys im completely new to this and trying, sorry. here is the current code. somethings in it might be useless or redundant but i cant tell.

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int N=19;
double y[N];


void read_file()
{
	int i=0;
	char name[20];
	double temp, new_temp;

	ifstream emma;


	cout<<"Enter a file name."<<endl;    
         
	cin>>name;       
 
	emma.open(name, ios::in); 
	
	if (emma.fail())          
	   {                        
			cout<<"File could not be opened"<<endl;       
		                
		   } 

	emma>>temp;	
	emma>>new_temp;

	while (i < N  && !emma.eof())	
	{
		y[i] = new_temp;				
		i++;					
		emma>>new_temp;			
	}
}

void orig_data()
{
	int i=0;

	ofstream emma_1;

	for(i=0; i<N ; ++i)
	{
		emma_1<<y[i];
		cout<<setw(15)<<left<<y[i]<<endl;
	}

	emma_1.close();
}

void bb_sort()
{
	double temp_1;

	for(int i=1; i<N; i++)
		for(int j=0; j<N-1;j++)
		{
			if (y[j]<y[j+1])
			{
				temp_1 = y[j];
				y[j]= y[j+1];
				y[j+1]= temp_1;
			}
		}
		
}

void main()
{
	read_file();
	
	cout<<"The original order of the data values is:"<<endl;
	orig_data();
	cout<<"     "<<endl;
	bb_sort();
	cout<<"         "<<endl;
	cout<<"The data values from highest to lowest is:"<<endl;
	orig_data();
}

i still can not read the last value of 77.7 into the array.

nevermind I found the issue for both of my problems. disregard my last post please. thank you rurouni, you really helped me out on this one. thank you everyone!!!!!!
P.S. it works but i think that i still have some redundant code in it. it will only work for the file provided in class and not for any text file with values in it. here is the current code.

#include <fstream>
#include <iostream>
using namespace std;

const int N=19;
double y[N];


void read_file()
{
	int i=0;
	char name[19];
	double temp, new_temp;

	ifstream emma;


	cout<<"Enter a file name."<<endl;    
         
	cin>>name;       
 
	emma.open(name); 
	
	if (emma.fail())          
	   {                        
			cout<<"File could not be opened"<<endl;       
		                
		   } 

	emma>>temp;	
	emma>>new_temp;

	while (i < N )	
	{
		y[i] = new_temp;				
		i++;					
		emma>>new_temp;			
	}
}

void orig_data()
{
	int i=0;

	ofstream emma_1;

	for(i=0; i<N ; ++i)
	{
		emma_1<<y[i];
		cout<<left<<y[i]<<endl;
	}

	emma_1.close();
}

void bb_sort()
{
	double temp_1;

	for(int i=1; i<N; i++)
		for(int j=0; j<N-1;j++)
		{
			if (y[j]<y[j+1])
			{
				temp_1 = y[j];
				y[j]= y[j+1];
				y[j+1]= temp_1;
			}
		}
		
}

void main()
{
	read_file();
	
	cout<<"The original order of the data values is:"<<endl;
	orig_data();
	cout<<"     "<<endl;
	bb_sort();
	cout<<"         "<<endl;
	cout<<"The data values from highest to lowest is:"<<endl;
	orig_data();
}

Edited 6 Years Ago by decoy684: n/a

No problem. You're right, there is some redundant code. But anyway, good work.

This question has already been answered. Start a new discussion instead.