Solved

[noob][c++] please contact te software support team

Posted on 2007-11-19
7
209 Views
Last Modified: 2010-04-01
For some reason this output pop up in the console

hello
4

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.





after running this




// reading a text file
#include <iostream>
#include <fstream>
#include <string>
      
using namespace std;
      
string open_file_return_lines ();

int main ()
{      
      string string1 = "example.txt";
      
      open_file_return_lines ();
      
      return 0;
}


string open_file_return_lines ()
{
      string line;
      ifstream myfile ( "example.txt" );
     
      int number_of_lines;
      number_of_lines = 0;
      
      if ( myfile.is_open() )
      {
            while ( ! myfile.eof() )
            {
                  getline (myfile,line);
                  number_of_lines = number_of_lines + 1;
            }
            
            myfile.close();
      }

      else
      {
            cout << "Unable to open file";
      }

      cout << "hello" << endl;
      cout << number_of_lines << endl;
     
     
      return 0;
      
}


0
Comment
Question by:Troudeloup
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 20314469
Make that

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
     
using namespace std;
     
int open_file_return_lines (); // <---------------!

int main ()
{      
      string string1 = "example.txt";
     
      open_file_return_lines ();
     
      return 0;
}


int open_file_return_lines ()
{
      string line;
      ifstream myfile ( "example.txt" );
     
      int number_of_lines;
      number_of_lines = 0;
     
      if ( myfile.is_open() )
      {
            while ( ! myfile.eof() )
            {
                  getline (myfile,line);
                  number_of_lines = number_of_lines + 1;
            }
           
            myfile.close();
      }

      else
      {
            cout << "Unable to open file";
      }

      cout << "hello" << endl;
      cout << number_of_lines << endl;
     
     
      return 0;
     
}

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 20314475
To elaborate: The function was declared to return a string, and you used

      return 0;

That's not a good idea at runtime.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 225 total points
ID: 20314482
You are returning 0 and this is being passed into the constructor of the std::string your function creates as a temporary. this is causing the code to fail.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 225 total points
ID: 20314490
This is the same as writing std::string(0); however, it is being generated implicitly by the compiler.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 75 total points
ID: 20314494
Side comment:
The line marked is unnecessary, I suspect you intended to use as an argument for the function open_file_return_lines()

int main ()
{      
      string string1 = "example.txt";   // <-------------- not needed
      open_file_return_lines ();
      return 0;
}
0
 

Author Comment

by:Troudeloup
ID: 20314762
yeah i wanted to pass the file name to the function as a string.


for some reason that doesn't work, but that's a serarate issue i am going to leave behind for now.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20314778
In that case, use

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
     
using namespace std;
     
int open_file_return_lines (string strFile); // <---------------!

int main ()
{      
      string string1 = "example.txt";
     
      open_file_return_lines (string1);
     
      return 0;
}


int open_file_return_lines (string strFile)
{
      string line;
      ifstream myfile ( strFile.c_str() );
     
      int number_of_lines;
      number_of_lines = 0;
     
      if ( myfile.is_open() )
      {
            while ( ! myfile.eof() )
            {
                  getline (myfile,line);
                  number_of_lines = number_of_lines + 1;
            }
           
            myfile.close();
      }

      else
      {
            cout << "Unable to open file";
      }

      cout << "hello" << endl;
      cout << number_of_lines << endl;
     
     
      return 0;
     
}
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question