Hi,

I'm a newbie in C++ and i'm working on a project, but it doesn't work..

The C++ program should read numbers into an array, the numbers are in a txt file.
Each array number : ex. array[1], array[2], array[3]... should get a number from the txt file.
In the file, for each new number, a new line is started.

Can someone help me with this ?

Sven

fstream header will do. just include <ifstream>
then do
ifstream vname("filename");
vname >> array; as many times as needed

Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave

What, exactly, doesn't work? The input portion?
Once you input the data, have you outputted it to check if it is wrong or right?

Perhaps you could post the code you have already written, to give us something to work with.

Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave

I know that, but I'm using a for loop..
for (i=1; i<=number; i++)
and array

Or isn't that correct ?

nope, if you make an array of length 5

int test[5];

then you have to use elements 0 to 4

for (i=0; i<5; i++)
#include <iostream>
#include <fstream>

using namespace std;

void drukintro ();
void geef_rij( int& aantal, int getallen[]);
void kies_bestand (int aantal, int rij[]);
void lees_bestand (char bestandnaam[], int aantal, int bestandrij[]);
void sorteerint(int aantal, int rij[]);
void printintrij(int aantal, int rij[]);

void drukintro ()
{
	int i;
	cout << "Dit is een programma dat een rij van getallen sorteert" << endl
		 << "en het voorkomen van elk getal telt," << endl 
		 << "en daarna van hoog naar laag op het scherm drukt." << endl;
	for ( i=0; i<=54; i++)
	{
		cout << "-";
	}
	cout << endl << endl;

}

void geef_rij( int& aantal, int getallen[])
{
	int i, eigen_getal;
	cout << "How many numbers do you want to enter ?" << endl;
	cin >> aantal;

	for (i=0; i<aantal; i++)
	{
		cin >> eigen_getal;
		getallen [i]= eigen_getal;
	}
}

void kies_bestand (int aantal, int rij[])
{
	int keuze_lijst;

	cout << "Which list do you want to use ?" << endl
		<< "For list 1, type 1" << endl
		<< "For list 2, type 2" << endl
		<< "For list 3, type 3" << endl << endl;
	cout << "Keuze : ";
	cin >> keuze_lijst;
	
	if (keuze_lijst < 1 || keuze_lijst > 3 )
	{
		cout << "This list doesn't exist !" << endl;
		exit(1);
	}
	
	if (keuze_lijst == 1)
	{
		lees_bestand ("lijst1.txt", aantal, rij);
	}

	if (keuze_lijst == 2)
	{
		lees_bestand ("lijst2.txt", aantal, rij);
	}

	if (keuze_lijst == 3)
	{
		lees_bestand ("lijst3.txt", aantal, rij);
	}
}

void lees_bestand (char bestandnaam[], int aantal, int bestandrij[])
{
	ifstream bestand;
	int i=0;

	bestand.open(bestandnaam);
	if ( ! bestand.fail() )
	{
		do 
		{
			bestand >> bestandrij[i];
			i++;
		}while (i<aantal);
		bestand.close();
	}

	else if (bestand.fail())
	{
		cout << "Opening of txt file failed ! \n";
		exit(1);
	}
}


void sorteerint(int aantal, int rij[])
{	
	int i,j,temp;
	for (i=0; i<aantal-1; i++)
		for (j=aantal-1; j>i; j--)
			if (rij[j-1]>rij[j]) 
			{	
				temp=rij[j];
				rij[j]=rij[j-1];
				rij[j-1]=temp;
			}
}

void printintrij(int aantal, int rij[])
{
	int i;
	for (i=0; i<aantal; i++)
		cout << rij[i] << " ";
	cout << endl;
}
	

int main ()
{
	int  eigen_rij[50], aantal_getallen;
	char antwoord;

	drukintro ();
	cout << "Do you want to give in an array of whole numbers yourself" << endl
		<< "or read from an array of whole numbers from a file ? (Y / F )" << endl;
	cin >> antwoord;
	cout << endl;

	if ( antwoord == 'Y' || antwoord == 'y' )
	{
		geef_rij ( aantal_getallen, eigen_rij );
		cout << endl;
	}

	if ( antwoord == 'F' || antwoord == 'f' )
	{
		kies_bestand( aantal_getallen, eigen_rij );
		cout << endl;
	}

	else
	{
		exit (1);
	}

	sorteerint ( aantal_getallen, eigen_rij );
	cout << "Sorted array : "; 
	printintrij( aantal_getallen, eigen_rij );
}

I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..

If you test it out... You'll see that the array doesn't read in one of the txt-files..

I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..

If you test it out... You'll see that the array doesn't read in one of the txt-files..

We can't test it out if you don't supply the input text files. Which text file doesn't read in?

We can't test it out if you don't supply the input text files. Which text file doesn't read in?

It doesn't read any of them..

list1 :
15
78
32
15
74
15
96
32
159
-1
96
20
25
45
38
95
18
20
98
159
78
15

list 2 :
58
78
12
13
95
147
258
369
159
753
58
95
95
14
78
62
32
14
159
842
24
75
62
314
789
15
78
321
8
6
7

list3:
45
78
97
12
14
18
32
0
-1
-25
87
26
17
26
78
45
12
18
45
65
32
-25

It doesn't read any of them..

Don't assume that a run-time error means that the program isn't reading from the files. Place some cout statements inside the function that reads from the file that display the parameters passed to the function and display what is being read in. Make sure that the parameters are what you want them to be, and that what you want to read in is actually being stored how you want it to be stored, and that you are going through the loop the correct number of times.

You never initialized aantal_getallen
which is the number of records to read/sort/print.

You never initialized aantal_getallen
which is the number of records to read/sort/print.

But what should than be the value of "aantal_getallen" ?
Because the program doesn't know how much numbers there are in the files..

Don't assume that a run-time error means that the program isn't reading from the files. Place some cout statements inside the function that reads from the file that display the parameters passed to the function and display what is being read in. Make sure that the parameters are what you want them to be, and that what you want to read in is actually being stored how you want it to be stored, and that you are going through the loop the correct number of times.

But I don't have errors, the program works..
But when I choose a file, it doesn't give a sorted array of it..
I tested the function with some cout statements and they worked..

But I don't have errors, the program works..
But when I choose a file, it doesn't give a sorted array of it..
I tested the function with some cout statements and they worked..

If the program worked, you wouldn't be posting here. It doesn't work, as you've said in your earlier posts, and whatever cout statements you placed elsewhere, you have placed none in lees_bestand or you have taken them out already. That is where you need them. You need to figure out how many times you are going through that do...while loop in order to successfully debug the program.

If the program worked, you wouldn't be posting here. It doesn't work, as you've said in your earlier posts, and whatever cout statements you placed elsewhere, you have placed none in lees_bestand or you have taken them out already. That is where you need them. You need to figure out how many times you are going through that do...while loop in order to successfully debug the program.

void lees_bestand (char bestandnaam[], int& aantal, int bestandrij[])
{
	ifstream bestand;
	int i;

	bestand.open(bestandnaam);
	if ( ! bestand.fail() )
	{
		for (i=0; i<aantal; i++)
		{
			if ( ! bestand.eof ())
			{
				bestand >> bestandrij[i];
				aantal++;
			}
			else
			{
				break;
			}
		}
		bestand.close();
	}

	else if (bestand.fail())
	{
		cout << "Openen van tekstbestand mislukt ! \n";
		exit(1);
	}
}

and

int aantal_getallen=1;

In the main-function.

Yes, that's true, I didn't put those cout statements in my posts..
But I've been working on it furter..
And it reads and prints the lists..
But there's one problem, if it always reads one more line in the file I think.. cause the first number of the sorted array is always something like :"-858993460", and I didn't put this number in one of the file .. :s
I first thought that it was because I had set "aantal_getallen" equal to 1, but if I set it equal to zero, it displays only the first number of the file..


Yes, that's true, I didn't put those cout statements in my posts..
But I've been working on it furter..
And it reads and prints the lists..
But there's one problem, if it always reads one more line in the file I think.. cause the first number of the sorted array is always something like :"-858993460", and I didn't put this number in one of the file .. :s
I first thought that it was because I had set "aantal_getallen" equal to 1, but if I set it equal to zero, it displays only the first number of the file..

-858993460 is almost certainly an uninitialized variable. Do you ever initialize element 0 to anything?

I first thought that it was because I had set "aantal_getallen" equal to 1, but if I set it equal to zero, it displays only the first number of the file..

Your first instinct is probably correct on this. If you set it to 1, make sure that element 0 gets assigned to something. The way to solve it is to fix your loop:

for (i=0; i<aantal; i++)
		{
			if ( ! bestand.eof ())
			{
				bestand >> bestandrij[i];
				aantal++;
			}
			else
			{
				break;
			}
		}

Will i ever catch up to aantal so that the condition of the for-loop is false? If you always go through the loop 0 times, 1 time, or millions of times, then it's not really a for-loop and you should change it. Your real loop control here is the eof and the break , so if the for-loop serves no purpose and simply hinders the number of times you go through the loop, change it or simply delete it.

Also consider initializing all of your array elements to some nonsense value in the beginning. If that nonsense value shows up in the end, chances are extremely high that you never initialized it to a REAL value.

Okay, thank you for your help..
It works !

Sven

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.