Attempting to output contents of a text file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 777 Views - Last Post: 02 April 2013 - 10:01 AM Rate Topic: -----

#1 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Attempting to output contents of a text file

Posted 02 April 2013 - 03:55 AM

Hi guys, I am fairly new to this forum so please forgive any mistakes on my part. Anyway I am making a program that will check a text file and then compare it against a list of abbreviations and output how many abbreviations there are, the problem is is that I count the abbreviations found which were outputted and it doesn't match up with the amount in the first text file.
 
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	string newString;
	vector<std::string> textFileVector;
	vector<std::string> abbrevVector;
	ifstream textfile( "text1.txt" );
	ifstream abbrevfile( "abbreviations.txt" );

	if (!textfile)
	{
		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!textfile.eof())
	{
		textfile >> newString;
		textFileVector.push_back(newString);
		cout << newString << " ";
	}
	
	cout << endl;

	if (!abbrevfile)
	{
		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!abbrevfile.eof())
	{
		abbrevfile >> newString;
		abbrevVector.push_back(newString);
		cout << newString << " ";
	}

	cout << endl;
	cout << endl;

	int i = 0;
	for (i; i < abbrevVector.size(); i++)
	{
		int count = 0;
		int k = 0;
		for (k; k < textFileVector.size(); k++)
		{
			if (abbrevVector[i] == textFileVector[k])
			{
				cout << " Element " << abbrevVector[i] << " was found " << count << " times " << endl;
				count++;
			}
		}
	}

	system ( "pause" );
	return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Attempting to output contents of a text file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 05:21 AM

Using eof is a generally a bad idea.

Give us an idea what you receive for output vs what you expect, as well as the format of your input.
Was This Post Helpful? 0
  • +
  • -

#3 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 05:54 AM

it should show a list like this and say something like element " " was found " " times
Aberd
admin
approx
Austral
div
Capt
Comdr
e
HQ
m
mil
mt
n
NATO
NS
pop
s
sq
w
instead of this it just shows what is in the attached picture

View Postmrbondysa, on 02 April 2013 - 05:50 AM, said:

it should show a list like this and say something like element " " was found " " times
Aberd
admin
approx
Austral
div
Capt
Comdr
e
HQ
m
mil
mt
n
NATO
NS
pop
s
sq
w
instead of this it just shows what is in the attached picture


this is the image
http://imgur.com/1cKEKHT
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:08 AM

Please post your sample "text1.txt" file.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:13 AM

Here is the file

NATO troops were on exercise in Aberd Capt Jones of the first div told Comdr Frank that his troops were near the E flank of the NATO forces. Complaining about the amount of Admin, Capt Jones radioed the NATO HQ asked for navigation relative to the HQ. The Captain then left.

Attached File(s)

  • Attached File  text1.txt (274bytes)
    Number of downloads: 10

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:24 AM

You are printing your "found" string each time you find a match, not once for each match found. You may want to move your print statement to after the inner loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:29 AM

Do you mean like this
int i = 0;
	for (i; i < abbrevVector.size(); i++)
	{
		int count = 0;	
		int k = 0;
		for (k; k < textFileVector.size(); k++)
		{
			if (abbrevVector[i] == textFileVector[k])
			{
				
			}
		}
		cout << " Element " << abbrevVector[i] << " was found " << count << " times " << endl;
		count++;
	}


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:31 AM

Yes except the incrementing of count should stay in the loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:37 AM

Thankyou, it looks good now, just I now have the problem being that some of the abbreviations aren't found in the results. For example...
http://imgur.com/mR1zLRz
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:48 AM

Post your current code, and the complete text file, since it is different than what you posted.

But note this program will only find complete matches, not partial matches. The string "HQ." is not the same as "HQ". If you want this to match then you could try reversing the order of the strings in the if statement.

Also remember C/C++ is case sensitive so if there is a difference in case it won't match.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:55 AM

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	string newString;
	vector<std::string> textFileVector;
	vector<std::string> abbrevVector;
	ifstream textfile( "text1.txt" );
	ifstream abbrevfile( "abbreviations.txt" );

	if (!textfile)
	{
		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!textfile.eof())
	{
		textfile >> newString;
		textFileVector.push_back(newString);
		cout << newString << " ";
	}
	
	cout << endl;

	if (!abbrevfile)
	{

		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!abbrevfile.eof())
	{
		
		abbrevfile >> newString;
		abbrevVector.push_back(newString);
		cout << newString << " ";
	}

	cout << endl;
	cout << endl;

	int i = 0;
	for (i; i < abbrevVector.size(); i++)
	{
		int count = 0;	
		int k = 0;
		for (k; k < textFileVector.size(); k++)
		{
			if (abbrevVector[i] == textFileVector[k])
			{
				count++;
			}
		}
		cout << " Element " << abbrevVector[i] << " was found " << count << " times " << endl;
		
	}

	system ( "pause" );
	return 0;
}



View Postmrbondysa, on 02 April 2013 - 01:54 PM, said:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	string newString;
	vector<std::string> textFileVector;
	vector<std::string> abbrevVector;
	ifstream textfile( "text1.txt" );
	ifstream abbrevfile( "abbreviations.txt" );

	if (!textfile)
	{
		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!textfile.eof())
	{
		textfile >> newString;
		textFileVector.push_back(newString);
		cout << newString << " ";
	}
	
	cout << endl;

	if (!abbrevfile)
	{

		std::cout << "Error opening the output file" << endl;
		system( "pause" );
		return -1;
	}
	while (!abbrevfile.eof())
	{
		
		abbrevfile >> newString;
		abbrevVector.push_back(newString);
		cout << newString << " ";
	}

	cout << endl;
	cout << endl;

	int i = 0;
	for (i; i < abbrevVector.size(); i++)
	{
		int count = 0;	
		int k = 0;
		for (k; k < textFileVector.size(); k++)
		{
			if (abbrevVector[i] == textFileVector[k])
			{
				count++;
			}
		}
		cout << " Element " << abbrevVector[i] << " was found " << count << " times " << endl;
		
	}

	system ( "pause" );
	return 0;
}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 06:57 AM

That's not the complete text file shown in your picture.

What exactly do you expect the output of your program to be. The output I'm getting is what I expect based on the program structure.

Also in future please cut and paste the output into the forum. The pictures are not always easy to read.

Jim

This post has been edited by jimblumberg: 02 April 2013 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#13 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 07:00 AM

The text file in the picture is both text1 and abbreviations being shown on screen. I expect the program to output on screen how many times an abbreviation is found
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,683
  • Joined: 25-December 09

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 07:14 AM

It is printing on the screen how many times an abbreviation is found.

This is the output I get:

Quote

NATO troops were on exercise in Aberd Capt Jones of the first div told Comdr Frank that his troops were near the E flank of the NATO forces. Complaining about the amount of Admin, Capt Jones radioed the NATO HQ asked for navigation relative to the HQ. The Captain then left.
Aberd admin approx Austral div Capt Comdr e HQ m mil mt n NATO NS pop s sq w

Element Aberd was found 1 times
Element admin was found 0 times
Element approx was found 0 times
Element Austral was found 0 times
Element div was found 1 times
Element Capt was found 2 times
Element Comdr was found 1 times
Element e was found 0 times
Element HQ was found 1 times
Element m was found 0 times
Element mil was found 0 times
Element mt was found 0 times
Element n was found 0 times
Element NATO was found 3 times
Element NS was found 0 times
Element pop was found 0 times
Element s was found 0 times
Element sq was found 0 times
Element w was found 0 times
sh: 1: pause: not found


It is exactly as I would expect. So what do you expect?

Jim
Was This Post Helpful? 0
  • +
  • -

#15 mrbondysa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-April 13

Re: Attempting to output contents of a text file

Posted 02 April 2013 - 07:19 AM

Oh sorry, what i'm trying to do is make it count as both lower and upper case and can't find a way to do it. Sorry for not being very helpful
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3