473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with streamreading into arrays

10
Working in c#. This is my current code
Expand|Select|Wrap|Line Numbers
  1.  string[] UID = new string[studentlength];
  2.                 string[] RegNo = new string[studentlength];
  3.                 int j = 0;
  4.                 int k = 1;
  5.  
  6.                 for (int i = 0; i < words.Length; i=i+5)
  7.                 {
  8.                     UID[j++] = words[i];
  9.                     RegNo[k++] = words[i];
  10.  
  11.                 } 
The variable "words" is an array that has been streamed from a file, the file is a series of lists. The two arrays named UID and RegNo are trying to take the different parts of the lists. UID works but RegNo causes an error because it overshoots the length of the array. Any ideas on how to fix this??
Feb 15 '08 #1
3 850
Plater
7,872 Expert 4TB
I am a bit confused by what you are doing.
UID and RegNo are getting the same value from words.
As for why RegNo overshoots it's index while UID doesn't, it's because you start that index at 1 and not at 0.
(Actually, using the ++ the way you do, UID starts at index 1 and RegNo starts at index 2, both should probably be starting at index 0)

You are also ittertaing i by 5, yet your check condition does not take that into account.
Try something like this:
for (int i = 0; i < (words.Length-5); i=i+5)
Feb 15 '08 #2
Chiefy
10
I am a bit confused by what you are doing.
UID and RegNo are getting the same value from words.
As for why RegNo overshoots it's index while UID doesn't, it's because you start that index at 1 and not at 0.
(Actually, using the ++ the way you do, UID starts at index 1 and RegNo starts at index 2, both should probably be starting at index 0)

You are also ittertaing i by 5, yet your check condition does not take that into account.
Try something like this:
for (int i = 0; i < (words.Length-5); i=i+5)
RegNo starts at 1 because it needs to hold the contents of the next list. For example
UID RegNo
123 2005
124 2006
125 2007
Taking 5 off of the max for i will surely make it overshoot even more.
Feb 15 '08 #3
Plater
7,872 Expert 4TB
RegNo starts at 1 because it needs to hold the contents of the next list. For example
UID RegNo
123 2005
124 2006
125 2007
Taking 5 off of the max for i will surely make it overshoot even more.
That doesn't make any sense, I don't think you understand arrays correctly.

I believe my original assumption was wrong, the index into words[] is not what is going out of bounds, I would say it is the indexes into UID and RegNo.


Suppose studentlength=4;
You now have UID[4] and RegNo[4].
They each index at
[0]
[1]
[2]
[3]

You start UID at [1], skipping [0]
And you start RegNo at [2] skipping both [0] and [1].
The indexes you skiped will not got populated with data.


Set a break point before the loop and put the variables into your watch window and step through all the code watching the values change as you go.
Feb 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.