8 Replies - 10111 Views - Last Post: 11 April 2011 - 09:43 PM Rate Topic: -----

#1 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

How do I print the contents of an array ?

Posted 11 April 2011 - 05:09 PM

I'm trying to print out the contentes of my array in order to make sure I'm correctly filling up my array. How can I get the contents to display out on the console ?
I have the following function reading numbers from a text file and if I have it set up correctly, they are being seeded into the array. How would I print them out in a main function ?



void donationReader(int d[])
{


	int i =0;
	int numberOfDonors = 0;

	ifstream inFile; //Declare the input file stream
	inFile.open("donations.txt"); //Opening the file
	
	
	do{
		inFile>>d[i];// seeding the numbers into the array :D/>

	}while(!inFile.eof()); //Keep seeding the array with numbers until the end of the file.


	if(!inFile){
		cout << "Unable to open the file donations.txt";
		exit(1); // shut dowm the program if possble
	}

	inFile.close();// Closes the file

	
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do I print the contents of an array ?

#2 GhostOfPerdition  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 19
  • Joined: 11-April 11

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 05:14 PM

I would suggest using a for loop to iterate through the array and send each element to cout.

This post has been edited by GhostOfPerdition: 11 April 2011 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 05:29 PM

I used this code but I got nothing to print out. I guess it would suggest that my array is empty but the debugger says otherwise.


 for (int i=0; i>100;i++)
	
 {
cout<<d[i]<<endl;
 }
	



Was This Post Helpful? 0
  • +
  • -

#4 GhostOfPerdition  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 19
  • Joined: 11-April 11

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 05:40 PM

Your for loop condition might be wrong.

View Postredchannel, on 11 April 2011 - 05:29 PM, said:

 for (int i=0; i>100;i++)




Assuming it's not a copy/paste error, "i>100" should be "i<100". You want to loop while i is less than 100.

This post has been edited by GhostOfPerdition: 11 April 2011 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 05:51 PM

AWSOME !!!
I fixed that but now I have a new problem. I'm getting a bunch of weird numbers to print out and not the number from the text file :o
Was This Post Helpful? 0
  • +
  • -

#6 GhostOfPerdition  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 19
  • Joined: 11-April 11

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 05:59 PM

Is the first value printed out the last value from the text file?

From your donationReader() function:

    int i =0;
    int numberOfDonors = 0;
 

    ifstream inFile; //Declare the input file stream

    inFile.open("donations.txt"); //Opening the file

    do{

        inFile>>d[i];// seeding the numbers into the array :D/>

    }while(!inFile.eof());



Here you initialize i to 0, but you don't ever increment i. So when you do inFile>>d[i]; it will always read into d[0].

To solve this you'll need to add 1 to i after each read.
Was This Post Helpful? 0
  • +
  • -

#7 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 06:26 PM

I tried that, but I don't think it's working. My for loop keeps printing -87324... 100 times and doesn't even print the first number, I dont think my logic is 100% I think I need i to be the number of elements in the text file. how can I go about counting the numbers ?
Was This Post Helpful? 0
  • +
  • -

#8 TomJoad  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 54
  • Joined: 01-December 10

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 06:58 PM

View Postredchannel, on 11 April 2011 - 06:26 PM, said:

I tried that, but I don't think it's working. My for loop keeps printing -87324... 100 times and doesn't even print the first number, I dont think my logic is 100% I think I need i to be the number of elements in the text file. how can I go about counting the numbers ?



You need to break up the input somehow and let the program know those breaks, or it will write the whole thing over and over again. Weather that is a "," or ":" or "/n."

I use this while using QT's QTextStream class:
for (int iii=0;iii<90; iii++)
{
anArray[iii] = txtstream.readline(); //reads one line and writes it to the array
}


Also, I have never worked with passing an array to the function, but I believe it needs to be initialized. Yours doesn't appear to be. (but I could be wrong)
Was This Post Helpful? 0
  • +
  • -

#9 GhostOfPerdition  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 19
  • Joined: 11-April 11

Re: How do I print the contents of an array ?

Posted 11 April 2011 - 09:43 PM

View PostTomJoad, on 11 April 2011 - 06:58 PM, said:

You need to break up the input somehow and let the program know those breaks, or it will write the whole thing over and over again. Weather that is a "," or ":" or "/n."


That's a good point. If your "donations.txt" file is formated incorrectly your function won't have a chance.

TomJoad said:

Also, I have never worked with passing an array to the function, but I believe it needs to be initialized. Yours doesn't appear to be. (but I could be wrong)


I just tested passing data this way with visual studio 2010 and it compiles and runs without error. Though this could be a new feature. I seem to recall problems with passing arrays this way in previous versions.

redchannel said:

...I think I need i to be the number of elements in the text file. how can I go about counting the numbers ?


Are you supposed to know the number of elements before calling the function? If so, maybe you should pass that in as a second parameter and use a for loop to read the elements in. If not, the ideal solution probably involves reading the file until you reach EOF and then allocating a dynamic array of the correct size and returning that. This expects that the function caller will properly free the memory when done with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1