10 Replies - 296 Views - Last Post: 11 September 2019 - 06:43 PM Rate Topic: -----

#1 coco28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-September 19

(beginner) Putting numbers from text file into an array.

Posted 10 September 2019 - 07:20 PM

I've created a random number generator that places numbers 100-200 at random into a text file and now
I want to store the random numbers from that file into an array.
This is practice specifically using arrays. Any help is greatly appreciated :)

The random 100 numbers are generating and being put into the file correctly, but aren't being stored in the array or either aren't printing out from the array.

After I figure this out I plan to move onto:
*finding the smallest and the largest of the random numbers and their array position
*inserting 150 in the 51th element of the array and print
*deleting the 75th element of the array and print
*sorting the data in the final array in an ascending order and print

but right now, I'm nearly delusional. lol Thanks in advance


#include "pch.h"
#include <iostream>
#include <fstream>



using namespace std;

int main()
{
	ofstream file;		

	file.open("randomData.txt");	


			srand((unsigned)(0)); 
			int ran_data;				

			for (int index = 0; index < 100; index++) {

				ran_data = (rand() % 101) + 100;  
				
			}

		    file << ran_data;   
			file.close();



			int array[101] = {};		
			ifstream is("ran_data");
			int num = 0;
			int x;

			while (num < array[101] && is >> x)
				array[num++] = x;

			cout << "The random numbers are:" << "\n";

			for (int i = 0; i < num; i++) {
				cout << array[i] << '  ';

			}

			is.close();

		return 0;

	}



Is This A Good Question/Topic? 0
  • +

Replies To: (beginner) Putting numbers from text file into an array.

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: (beginner) Putting numbers from text file into an array.

Posted 10 September 2019 - 08:12 PM

You have multiple issues. The first issue is that you truly are delusional regarding verifying that the numbers are being put into the file correctly. Look closer. Although you are generating multiple random numbers on lines 19-23, you are only putting one number into the file on line 25.

Next: Can you explain why you are doing this: num < array[101] ?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is online

  • member icon

Reputation: 5745
  • View blog
  • Posts: 17,601
  • Joined: 25-December 09

Re: (beginner) Putting numbers from text file into an array.

Posted 10 September 2019 - 08:54 PM

Also posted here.
Was This Post Helpful? 2
  • +
  • -

#4 coco28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-September 19

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 05:51 AM

View Postjimblumberg, on 10 September 2019 - 08:54 PM, said:

Also posted here.



Yessss, why wouldn't I post it on multiple websites? *confused*

View PostSkydiver, on 10 September 2019 - 08:12 PM, said:

You have multiple issues. The first issue is that you truly are delusional regarding verifying that the numbers are being put into the file correctly. Look closer. Although you are generating multiple random numbers on lines 19-23, you are only putting one number into the file on line 25.

Next: Can you explain why you are doing this: num < array[101] ?


LOL why even comment on a beginners post if your goal is only to chastise and not give actual advice? *facepalm*

I figured it out guys! Someone on another site was nice enough to help me out.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15219
  • View blog
  • Posts: 60,921
  • Joined: 12-June 08

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 06:51 AM

Quote

I figured it out guys! Someone on another site was nice enough to help me out.

What was the issue?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 01:38 PM

View Postcoco28, on 11 September 2019 - 08:51 AM, said:

LOL why even comment on a beginners post if your goal is only to chastise and not give actual advice? *facepalm*

I did give advice. I told you exactly which lines to look at for the issue with failing to write into the file correctly. Just like the way you failed to inspect the file where you were writing out your random numbers into, you also failed to read my advice closely.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 01:44 PM

View Postcoco28, on 11 September 2019 - 08:51 AM, said:

Yessss, why wouldn't I post it on multiple websites? *confused*

Because it is poor Netiquette. Proper netiquette calls on forum users to pick a forum to post in, try to get their question answered there, and only if they are reaching a dead end, then try a different forum. If you spam your question to multiple forums at the same time, yes, you may get your answer faster, but at the same time you caused multiple people to waste their time putting together a response and/or going over the same material. If you stick to one forum first, other people in the same forum can see if an answer has been reached and/or what has already been discussed. That doesn't happen across forums.
Was This Post Helpful? 0
  • +
  • -

#8 coco28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-September 19

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 04:57 PM

View PostSkydiver, on 11 September 2019 - 01:38 PM, said:

View Postcoco28, on 11 September 2019 - 08:51 AM, said:

LOL why even comment on a beginners post if your goal is only to chastise and not give actual advice? *facepalm*

I did give advice. I told you exactly which lines to look at for the issue with failing to write into the file correctly. Just like the way you failed to inspect the file where you were writing out your random numbers into, you also failed to read my advice closely.


Oh no, it wasn't a failure to not read your advice closely. I stopped reading it once I realized you were being churlish with me.
Was This Post Helpful? 0
  • +
  • -

#9 coco28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-September 19

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 05:05 PM

View PostSkydiver, on 11 September 2019 - 01:44 PM, said:

View Postcoco28, on 11 September 2019 - 08:51 AM, said:

Yessss, why wouldn't I post it on multiple websites? *confused*

Because it is poor Netiquette. Proper netiquette calls on forum users to pick a forum to post in, try to get their question answered there, and only if they are reaching a dead end, then try a different forum. If you spam your question to multiple forums at the same time, yes, you may get your answer faster, but at the same time you caused multiple people to waste their time putting together a response and/or going over the same material. If you stick to one forum first, other people in the same forum can see if an answer has been reached and/or what has already been discussed. That doesn't happen across forums.


Ah! I see where you're coming from! This is just a miscommunication then, because had I been helped here I definitely would've followed up, even if I'd been helped from the other forum prior (only posted in two). Plus, during practice and learning, it's good to get different viewpoints and I'd learn from more than one answer. Just as if I needed advice in a personal situation, I may not only ask one friend for help...but instead, take a collective wisdom. For future reference, if anyone ever goes out of their way or takes their time to help me, I would NEVER leave them hanging just because I'd already figured it out. I'm a very appreciative and gracious person. But that doesn't really matter or apply here today...because that didn't happen. :)
Was This Post Helpful? 0
  • +
  • -

#10 coco28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-September 19

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 05:11 PM

View Postmodi123_1, on 11 September 2019 - 06:51 AM, said:

Quote

I figured it out guys! Someone on another site was nice enough to help me out.

What was the issue?


line 25 was out of the loop and my while loop within the array needed to be simplified. I was overcomplicating it and confusing myself basically. It's always such a relief to see something work. It probably seems juvenile to others, but it took me a long time. *cry laughs*

Here's the array now if interested :D

int array[101] = {};		
			ifstream is("randomData.txt");
			int num = 0;

			while (num < 100)
			{
				is >> array[num++];
			}

			cout << "The random numbers are:" << "\n";

			for (int i = 0; i < num; i++) {
				cout << array[i] << ' ';



Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: (beginner) Putting numbers from text file into an array.

Posted 11 September 2019 - 06:43 PM

View Postcoco28, on 11 September 2019 - 07:57 PM, said:

View PostSkydiver, on 11 September 2019 - 01:38 PM, said:

View Postcoco28, on 11 September 2019 - 08:51 AM, said:

LOL why even comment on a beginners post if your goal is only to chastise and not give actual advice? *facepalm*

I did give advice. I told you exactly which lines to look at for the issue with failing to write into the file correctly. Just like the way you failed to inspect the file where you were writing out your random numbers into, you also failed to read my advice closely.


Oh no, it wasn't a failure to not read your advice closely. I stopped reading it once I realized you were being churlish with me.

But I wasn't being rude. I was simply confirming that yes, you were delusional, and then pointed out where in your code things were likely going wrong.

To remind you, you wrote:

View Postcoco28, on 10 September 2019 - 10:20 PM, said:

The random 100 numbers are generating and being put into the file correctly ...
:
but right now, I'm nearly delusional.


I replied with:

View PostSkydiver, on 10 September 2019 - 11:12 PM, said:

The first issue is that you truly are delusional regarding verifying that the numbers are being put into the file correctly. Look closer. Although you are generating multiple random numbers on lines 19-23, you are only putting one number into the file on line 25.


I don't see any churlishness happening there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1