Solved

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

Posted on 2007-11-19
7
191 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now