Wrox Programmer Forums
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2008, 06:40 PM
Registered User
 
Join Date: Sep 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to etow Send a message via MSN to etow Send a message via Yahoo to etow
Default Need Help With Code

Hi I have problem with displaying the data from this data file below:
4
0 1 1 -1 -3
1 2 -1 0 2
2 4 1 -3 -2
1 -4 -7 -1 -19


My code below:
Code:
    private: System::Void buttonTestSplitFunction_Click(System::Object^  sender, System::EventArgs^  e) {
                 int theRow;
                 int theCol;
                 String^ errorResults = " ";
                 String^ results = "";

           if (System::Windows::Forms::DialogResult::OK != openFileDialog1->ShowDialog(this))
            return;
           System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(openFileDialog1->FileName);
           try
           {
              int n = int::Parse(sr->ReadLine()); 

              theRow = 1;
              theCol = 1;

                 while (theRow <= n)
                 {
                   array<Char>^charsSpace = {' '};
                   String^ number = sr->ReadLine();
                   array<String^>^splitNumber = number->Split(charsSpace);

                   IEnumerator^ myEnumNumber = splitNumber->GetEnumerator();

                   while (myEnumNumber->MoveNext() && theCol <= n+1)
                   {
                    String^ n = safe_cast<String^>(myEnumNumber->Current);
                    Equation[theRow,theCol] = double::Parse(n);
                    theCol++;
                   }
                   theRow++;
                 }

              results = results + n.ToString() + Environment::NewLine;

              for(int i = 1 ; i <= n; i++)
              {
               for(int j = 1; j <= n+1; j++)
               {
                   results = results + (Equation[i,j]).ToString(); 
               }

               results = results + Environment::NewLine;
              }

              this->textBoxSplitResults->Text = results; 

           }
           catch(FormatException^ message)
           {
            errorResults = errorResults + " Your file contains incorrect data format to run this program!!" + Environment::NewLine + Environment::NewLine;
            errorResults = errorResults + message->ToString();
            this->textBoxErrorResults->Text = errorResults;
           }
           finally
           {
             sr->Close();
           }
          }


 
Old September 30th, 2008, 08:37 AM
Registered User
 
Join Date: Sep 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to etow Send a message via MSN to etow Send a message via Yahoo to etow
Default

Here is the data file:
4
2 1 -1 2 -6
3 4 0 1 1
1 5 2 6 -3
5 2 -1 -1 3

Here is the correct code below to read numbers separated by each space from a text file above into a two dimensional array, the results of the two dimensional array is displayed in a text box :

    
Code:
private: System::Void buttonTestSplitFunction_Click(System::Object^  sender, System::EventArgs^  e) {
Code:
                 int theRow;
                 int theCol;
                 String^ errorResults = " ";
                 String^ results = "";
                 int maxRow = 10;
                 int maxColumn = 11;
                 array<double,2>^ Equation = gcnew array<double,2>(maxRow,maxColumn);
                 array<String^>^splitNumber;

           if (System::Windows::Forms::DialogResult::OK != openFileDialog1->ShowDialog(this))
            return;
           System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(openFileDialog1->FileName);
           try
           {
              int n = int::Parse(sr->ReadLine()); 

                 theRow = 0;

                 while (!(sr->EndOfStream))
                 {
                   String^ numbers = sr->ReadLine(); 
                   array<wchar_t>^charsSpace = {' '};

                   splitNumber = numbers->Split(charsSpace);

                   for (theCol = 0; theCol <= n; theCol++)
                     Equation[theRow,theCol] = double::Parse(splitNumber[theCol]);

                   theRow++;
                 }


              results = results + "From your data file : " + "\"" + this->openFileDialog1->FileName + "\"" + Environment::NewLine + Environment::NewLine;
              results = results + n.ToString() + Environment::NewLine;

              for(int i = 0 ; i <= n-1; i++)
              {
               for(int j = 0; j <= n; j++)
               {
                   results = results + (Equation[i,j]).ToString(); 
               }

               results = results + Environment::NewLine;
              }

              this->textBoxSplitResults->Text = results; 

           }
           catch(FormatException^ message)
           {
              errorResults = errorResults + "Your file : " + "\"" + this->openFileDialog1->FileName->ToString()+ "\"" + "  cause a format exception because it contains incorrect data format to run this program!!" + Environment::NewLine + Environment::NewLine;
              errorResults = errorResults + message->ToString();
              this->textBoxErrorResults->Text = errorResults;
           }           
           catch(OverflowException^ message)
           {
              errorResults = errorResults + "Your file : " + "\"" + this->openFileDialog1->FileName->ToString()+ "\"" + "  cause an overflow exception because the data values either too big or too small for this program to handle!!" + Environment::NewLine + Environment::NewLine;
              errorResults = errorResults + message->ToString();
              this->textBoxErrorResults->Text = errorResults;
           }
           finally
           {
             sr->Close();
           }
          }







Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent:hard disk serial code and vb code ivanlaw Pro VB 6 0 July 25th, 2007 04:05 AM
VB: .Exe file, serial code and activation code ivanlaw Pro VB 6 8 July 6th, 2007 05:44 AM
code clinic - Why wont example asp code work? jardbf Classic ASP Basics 3 April 27th, 2006 06:22 PM
Writing Client Side Script from Code-Behind code sajid_pk Classic ASP Databases 1 January 18th, 2005 12:53 AM
disable forum code within [code] blocks? nikolai Forum and Wrox.com Feedback 0 October 23rd, 2003 07:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.