Solved

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

Posted on 2007-11-19
7
216 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 87
how to convert c++ code to Android App 3 109
Grammars for C C++ and java 1 131
Best book to learn C++ 4 85
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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