Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI
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:
 
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 Frown | :(
 
Many thanks
Posted 6-Apr-13 11:38am
Edited 6-Apr-13 11:39am
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 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 at 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
lostandconfused1234 at 7-Apr-13 9:39am
   
I'm finding even more issues trying this...
 
If I want to split then the only msdn I can find: http://msdn.microsoft.com/en-us/library/e3awe53k.aspx
 
Says use a string for the delimeter to allow for conversion to string to conversion between splits, which is already fiddily, and then to convert to double. And to top it off, I can't find how I get the second string in the split. As far as I can tell it removes the space without actually just taking each. I'm really confused. I dont care how messy my code is, please can you give an answer in either method. It's killing me!
lostandconfused1234 at 7-Apr-13 9:12am
   
What I dont understand though is how you get the second part of the string. msnd shows how to use delimeters:
 
String^ str1 = str->Split(' ');
 
but how would i get the second half of this line? I read msdn every time before posting, but it's not the easiest read, and my cli book has nothing on double parse from file. it's rather specific.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 | :laugh:
  Permalink  
Comments
nv3 at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 162
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100