Solved

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

Posted on 2007-11-19
7
201 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 179
C++ - Loading Managed Assembly From Memory in Unmanaged Process 25 429
computer science syllabus 3 70
max float value 3 26
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 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

15 Experts available now in Live!

Get 1:1 Help Now