Im supposed to sort a file in decending order using bubble sort into an array. My code does all of that but it repeats the sorted array 50 times instead of just listing it once. This is a C ++ file on Dev. Thanks for your help, it's due midnight haha.

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;


const int NUMELEMENTS=50;

int main()
{
ifstream integer;

int Array[NUMELEMENTS];
int I;
integer.open("C://integer.txt", ios::in);



for (I = 0; I < NUMELEMENTS; I++)
{ integer >> Array[I] ;
{

int temp;
int flag =1;


for ( int i=1; i <= I && flag; i++ )
{
flag =0;
for ( int j=0; j < I - 1; j++ )
{
if ( Array[j+1] > Array[j] )
{
temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
flag =1;
}
}
}
for(temp=0; temp<I; temp++)
{cout << Array[temp] << endl;



}



}while (integer.eof());
}
system("PAUSE");
return 0;
}

Edited 6 Years Ago by doogidie: n/a

You need to paste the information in an array.

#include <iostream>
#include <cstdlib>
#include <fstream>


int array[5];
char c;

void file(char* Filename, int length){


	int length;

	ifstream file;
	file.open(Filename);

	if(!file)return false;

	while(!file.eof()){

		for(register int i; i<length; i++){

			file>>array[i];
                        file.get(c);


		}

	}


}

Edited 6 Years Ago by alexchen: n/a

You need to paste the information in an array.

#include <iostream>
#include <cstdlib>
#include <fstream>


int array[5];
char c;

void file(char* Filename, int length){


	int length;

	ifstream file;
	file.open(Filename);

	if(!file)return false;

	while(!file.eof()){

		for(register int i; i<length; i++){

			file>>array[i];
                        file.get(c);


		}

	}


}

what?? that looks like a completely different code.

come on guys, i know its an easy fix that im just overlooking. everything is working fine, the only problem is that its repeating the array 50 times with all 50 integers in the array, instead of just one array with the 50 integers. please help me, im so desperate!

Your problem is that you have masked length. You wrote this:

#
void file(char* Filename, int length)
{
  int length;   // UNINITIALIZED VARIABLE:
  for(int i=0;i<length;i++)
    {
    }
}

Note that length is NOT the same length as the one passed to the function, you have masked it with the later declaration. Any good compiler with the warnings on will tell you about that. (unused variable, and uninitialized variable warnings).

Also your test of the file eof is ONLY for the first element, why not at each stage.

Finally, don't use the keyword register. It is ignored by most compilers, and unless you are mixing assembler and C++, the chances of you actually betting the compilers optimizer are very very low.

Comments
Good points!

Separate the numbers with a white space.

#include <math.h>


using namespace std;

int *list;
char c;


bool arrange(int length){

  for(int i=0;i<length;i++)
  {
	if(list[i]>list[i+1])
		swap(list[i],list[i+1]);

	

    }

  for(int i=0;i<length;i++)cout<<"my new list contains "<<list [i]<<endl;


return true;
}
bool file(char* Filename, int length)
{

	

	ifstream file;
	file.open(Filename);



	list=new int[length];
  for(int i=0;i<length;i++)//don't use the keyword register. It is ignored by most compilers, and unless you are mixing assembler and C++, the chances of you actually betting the compilers optimizer are very very low. 
  {
	file>>list[i];
	cout<<"the list contain: "<<list[i]<<endl;

    }
	arrange(length);

return true;
}

First use code tags.

#include <cstdlib>
#include <iostream>

using namespace std;
// Array drinks
const int Drinks = 25;

// the costs structure holds data about the costs.
struct costs
{
double Coca-cola;
double RootBeer;
double Sprite;
double Spring-Water;
double Apple-juice;
};

// the drinksInfo structure hold an drinks data.
struct drinksInfo
{
char name[NAME_DRINKS];
int Costs;
int DrinksTypes;
int NumofMachine;
};

int main(int argc, char *argv[])
{

My file:

cocacola 5 5.75 redbeer 5 5.75
void drink(char*Filename){

ifstream file;
file.open(Filename);


struct drink
{
	char name[20];
	int num;
	double cost;
}drink;

struct drink d[2];

for(int i=0; i<2; i++)
{
	file>>d[i].name>>d[i].num>>d[i].cost;
	cout<<endl<<"The machine has: "<<d[i].num<<" "<<d[i].name<<endl<<"It cost: "<<d[i].cost<<endl;
}

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