3 Replies - 923 Views - Last Post: 08 September 2011 - 06:09 PM Rate Topic: -----

#1 dfournie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-September 09

Problem: Program terminates abruptly..C++

Posted 08 September 2011 - 05:25 PM

I am supposed to be reversing the text of a file "p01.txt". First of all, any tips on the second function would be greatly appreciated. My problem now is that when im reading the file in, the DOS screen quickly appears and dissipates instantly. I am not sure if I am doing something incorrectly because when I run the debug, my values are true and claims to be working.

Here is what I have so far:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


const int MAX_TOKENS_PER_STRING = 50;
const string INPUT_FILE = "p01.txt";
const char SPLIT_DELIMITER = ' ';

//Function Prototypes

// This function makes the connection between the filename and its handle.
// It returns true to indicate a successful connection, or false otherwise.
bool ConnectInputFile(ifstream& fin, const string& filename);

// This function takes a string and breaks it into tokens (words), using
// the supplied delimiter. Each token is placed into the array tokens.
void SplitStringIntoTokens(const string& stringToSplit, string tokens[], 
                                         int& numberOfTokens, const char& delimiter);

// This function takes a word and returns the word reversed. For example,
// if "pots" is passed in, then "stop" is returned.
string ReverseTheString(const string& str);


int main() 
{ 
	string backwards;
	int wordcount;
	string sentence[49];
	ifstream fromfile;
	
	
	ConnectInputFile(fromfile, INPUT_FILE);
	getline(fromfile, backwards); 
	while(!fromfile.eof())
	{
		cout << backwards << endl;
		SplitStringIntoTokens(backwards, sentence, wordcount, SPLIT_DELIMITER);
		
		//ReverseTheString()
			//for
		getline(fromfile,backwards);
	}

	return 0;	
}// end main()

bool ConnectInputFile(ifstream& fin, const string& filename)

{
	fin.open(filename.c_str()); 
	return fin.is_open();
		if(fin.fail())
	{
		return (false);
		cout << "Error opening the file p01.txt " << endl;
	}
	else
		return(true);
}

void SplitStringIntoTokens(const string& stringToSplit, string tokens[], 
						int& numberOfTokens, const char& delimiter)
{
        
}

string ReverseTheString(const string& str)
{
        //only have this because i have to return a value
	return("hello2");
}



This is the Debugger output I receive after running the code:
'p01.exe': Loaded 'C:\Documents and Settings\Crashem'\My Documents\Visual Studio 2010\Projects\p01\Debug\p01.exe', Symbols loaded.
'p01.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
'p01.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
'p01.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'p01.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
The program '[3056] p01.exe: Native' has exited with code 0 (0x0).

Is This A Good Question/Topic? 0
  • +

Replies To: Problem: Program terminates abruptly..C++

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Problem: Program terminates abruptly..C++

Posted 08 September 2011 - 05:48 PM

In the C++ FAQs topic, follow the link to "Holding the execution window open"
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Problem: Program terminates abruptly..C++

Posted 08 September 2011 - 05:50 PM

Quote

First of all, any tips on the second function would be greatly appreciated.
It's mental weighlifting. Keep at it and you'll solve the problem at some point.

Quote

My problem now is that when im reading the file in, the DOS screen quickly appears and dissipates instantly.
Did the console window remain open earlier in your program development?

Quote

I am not sure if I am doing something incorrectly because when I run the debug, my values are true and claims to be working.
What values? What claims? I want observations. What did you observe? What data did you collect precisely?

Quote

This is the Debugger output I receive after running the code:
That is not debugger output. There should be tutorials and learning resources on how to use the debugger. Use them to learn how to work with the debugger to collect information and isolate the issue.

Your code also makes no sense at various points. The first three lines of connectinputfile:

	fin.open(filename.c_str()); 
	return fin.is_open();
		if(fin.fail())



Translate to English for me, please.
Was This Post Helpful? 0
  • +
  • -

#4 dfournie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-September 09

Re: Problem: Program terminates abruptly..C++

Posted 08 September 2011 - 06:09 PM

Quote

My problem now is that when im reading the file in, the DOS screen quickly appears and dissipates instantly.
Did the console window remain open earlier in your program development?

I can alter certain parts of the code to make it stay open but the sidebar is all the way down to the bottom as if its continually runing blank lines. But other than that, no I have not had the window to remain open now or earlier in the development.

Your code also makes no sense at various points. The first three lines of connectinputfile:

	fin.open(filename.c_str()); 
	return fin.is_open();
		if(fin.fail())



Translate to English for me, please.
[/quote]

maybe i am incorrect here, but the fin is the name I assigned for the ifstream. The filename will be passed through the the stream when I call the "ConnectInputFile" in main(). Should it be something more along the lines of
fin.open("p01.txt");
if(!fin.is_open());
{
cerr << "Error opening the file p01.txt " << endl;
}



??? I am not sure what your saying as far as how it doesnt make sense...But it has been a while since i programed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1