Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a stream reader. It reads a line at a time from a file that contains two columns. The first is an integer value and the second is double. I need to seperate these into seperate sides of a 2 dimensional array. [ int ] [ double ] but to make things easier the 2D array is kept as double - I just need the numbers seperate for later on. This is what I have:

C++
String^ File = "C:\\Users\\**** :) secret ****\\Documents\\Error.txt";
 StreamReader^ sr = File::OpenText(File);
 String^ str;
 bool Flip = true;
 int index = 0;

while ((str = sr->ReadLine()) != nullptr)
{
      // initiates a stringbuilder of 50 characters
      StringBuilder^ sb = gcnew StringBuilder("", 50); 
      for(int i=0; i < str->Length; i++)
    {// for each char in line of string str if it is a digit or decimal place then...
              if(Char::IsDigit( str[i]) || str[i] == '.')  
                      sb->Append( str[i] ); //  append to stringbuilder
              else  {
                    // otherwise convert the stringbuilder to a double number
                      double Num = Convert::ToDouble( sb ); 

                      Flip = !Flip; // decides which 'side' of 2d array to place in
                      if(!Flip) 2dArray[index, 0] = Num; // column 1 
                      else      2dArray[index, 1] = Num; // column 2
                       // remove all elements of string clear for rest of line read.
                      sb->Remove(0, sb->Length-1); 

                     
                    }
       }
       index++; // increments for array index
}


To test this I've put the numbers through a for loop to find the max and minimum. These are then sent to textboxes for display. The data is completely unchanged...indicating there is a problem with this code. This was encapsulated inside a try {} catch{}. Omitting this showed me that the

Num = Convert::ToDouble( sb );

isn't allowed. So what alternative do I have? No problem came up with the compiler :(

Many thanks
Posted
Updated 6-Apr-13 10:39am
v2
Comments
Sergey Alexandrovich Kryukov 6-Apr-13 19:04pm    
It's almost impossible to fix. The code should better be re-written from scratch, it's a pain to look at. Don't use StringWriter for conversion to double, use double.Parse(string) or double.TryParse(string); never block propagation of exceptions.
—SA
lostandconfused1234 6-Apr-13 20:00pm    
Does parse.String allow for 2 numbers per line? I can't see how to implement.

http://msdn.microsoft.com/en-us/library/fd84bdyt.aspx

This has me stumped..

Sergey Alexandrovich Kryukov 6-Apr-13 20:06pm    
No, it should be one number. You can split a line using string.Split. Why not looking through essential volume of documentation on some very basic classes, before writing code? This is you real problem: RTFM (which of course means "Read the Following Manual ;-).
—SA
nv3 7-Apr-13 4:45am    
I totally agree with you, SA. There is so much time and talent wasted, just because people don't read even the most elementary documentation. And if they tried to get familiar with a debugger they would make their lives so much easier. -- But we can't change that.
Sergey Alexandrovich Kryukov 7-Apr-13 12:04pm    
Good point about the waste. Unfortunately, even the reading itself is can be a problem, because people try to read or find documentation without connection to related prerequisites. I've just written about it in comments to my answer:

http://www.codeproject.com/Answers/573657/Inheritanceplusproblemplusinplusc-2b-2b#answer1

—SA

1 solution

It's not very nice code, as Sergey (SA) says, but it is fixable. While SA is right that it is a bad solution to the problem, and using string.Split would be a neater and more readable solution, the problem you have met is that there is no Convert.ToDouble overload that takes a StringBuilder so it is passed as a Object and that probably causes the problem. Using ToString to convert the StringBuilder would solve that problem.

But you should use Double.Parse instead, as it only takes a string and the problem would have been more obvious right from the start!

There is another problem with your approach though: unless your line ends with a non digit value, you will only extract one number each time...:laugh:
 
Share this answer
 
Comments
nv3 7-Apr-13 4:40am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900