Solved

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

Posted on 2007-11-19
7
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

752 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