4 Replies - 2383 Views - Last Post: 15 March 2011 - 06:43 AM Rate Topic: -----

#1 nryoung   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-October 10

std::logic_error

Posted 14 March 2011 - 11:58 PM

I am attempting to open a file and read that file to a string for later use in my program. The program compiles fine but it throws an error when I run it that I can't seem to figure out. My files and code are below:

my globals.h file
#include <iostream>
#include <fstream>
#include <string>

#ifndef globals_h
#define globals_h

  // PRE: none
  // POST: displays greeting and describes program
  std::string greeting();

  // PRE: Command line arguments have been specified
  // POST: returns true if command line arguments are specified
  bool commandline_check(unsigned);

  // PRE: Filestream and string reference
  // POST: Reads data from a text file and puts it in a string
  bool read_data(std::ifstream&, std::string&);


#endif



My globals.cpp file
#include "globals.h"
#include <iostream>
#include <fstream>
#include <string>

std::string greeting()
{

  std::cout << " This program will parse through a text file and give the frequencies of how often each letter appears in the text file. Please press enter to continue.\n";
 
  std::cin.ignore();
  return 0;
}

bool commandline_check(unsigned x)
{
  if( x < 2)
    {
      std::cerr << "Please specify a text file to be used with this programs.\n";
      return (1);
    }
  else
    {
      return true;
    }
}

bool read_data(std::ifstream& in, std::string& text_string)
{
  
  if(!in)
    {
      std::cerr << "Please check file and try again.\n";
      return false;
    }
  else
    {
   getline(in, text_string);

  std::cout << text_string << '\n';
  in.close();
  return true;
    }
}



My main driver .cpp file
#include "globals.h"
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{

  greeting();

  commandline_check(argc);

  std::ifstream infile(argv[1], std::ios::in);
  /*if(!infile)
    {
      std::cerr << "Please check file and try again." << '\n';
      }*/
  std::string original_string("intialized");
  read_data(infile, original_string);
  
  return 0;

}



The error I get on run time
./a.out testfile 
 This program will parse through a text file and give the frequencies of how often each letter appears in the text file. Please press enter to continue.

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid
Aborted



Other notes: I am using the g++ compiler in Linux. Thanks for your help in advanced!

Is This A Good Question/Topic? 0
  • +

Replies To: std::logic_error

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: std::logic_error

Posted 15 March 2011 - 02:43 AM

Debugging tip.
Try modifying your main() as follows"
#include "globals.h"
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{

  greeting();
	std::cout << "got here A" << std::endl;
  commandline_check(argc);
	std::cout << "got here B" << std::endl;
  std::ifstream infile(argv[1], std::ios::in);
	std::cout << "got here C" << std::endl;
  std::string original_string("intialized");
	std::cout << "got here D" << std::endl;
  read_data(infile, original_string);
  
  return 0;
}

See all those output statements that will throw stuff to the screen as different parts of the program complete.
These help us work out where the error is happening.
Add the same as I have to your code and compile and run your program.
What did you learn about where your problem lies in the code?

BTW
This is almost certainly not doing what you think it does
      return (1);
    }
  else
    {
      return true;
    }


By returning 1 you are returning 'true'. So that code returns true elae it returns true. It always returns true. That's almost certainly not what you were trying to do.
Was This Post Helpful? 3
  • +
  • -

#3 nryoung   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-October 10

Re: std::logic_error

Posted 15 March 2011 - 05:37 AM

View Postjanotte, on 15 March 2011 - 02:43 AM, said:

BTW
This is almost certainly not doing what you think it does
      return (1);
    }
  else
    {
      return true;
    }


By returning 1 you are returning 'true'. So that code returns true elae it returns true. It always returns true. That's almost certainly not what you were trying to do.


Thank you so much for the debugging tips, I will most certainly try those out. Here is another quick question. I want the program to exit if the commandline arguments are not specified. I thought that was what return 1; was doing but I guess I was wrong. Is there an easier way to acheive this? Thank you so much for your help again.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: std::logic_error

Posted 15 March 2011 - 05:47 AM

return only returns from the called function. If that function is not main then it won't exit the program. Use exit() to exit the program altogether from any point.
Was This Post Helpful? 2
  • +
  • -

#5 nryoung   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-October 10

Re: std::logic_error

Posted 15 March 2011 - 06:43 AM

View PostJackOfAllTrades, on 15 March 2011 - 05:47 AM, said:

return only returns from the called function. If that function is not main then it won't exit the program. Use exit() to exit the program altogether from any point.


Thank you both for your help. I got it figured it out. I am sure I will be back for more help though. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1