By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Pattern Matching

P: n/a
Hi,

I want to read the input variables from a user, and then compare this with
data in the text file. If the input variables and the data in the text file
match, then i would like to create a vector to store this information as
binary.

Is there anyone who can help me please? I am a beginner in C++, and I have
so far figured out how to read input from a user. I have a very long way to
go, and I only have a week to do this. Any help would be greatly
appreciated.

Thanx.

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Thu, 02 Dec 2004 14:50:28 -0500, "kittykat"
<f_******@nospam.hotmail.com> wrote:
Hi,

I want to read the input variables from a user, and then compare this with
data in the text file. If the input variables and the data in the text file
match, then i would like to create a vector to store this information as
binary.
Putting your problem into pseudocode gives:

read user-input from keyboard
read file-data from text-file
if (user-input matches file-data) then
store binary-information in information-vector
endif

Looking at the code you posted earlier, just keeping the good bits:

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

int main()
{
// Get input from user
string pattern;

cout << "Enter the item you are searching for: ";
cin >> pattern;
// Read data from file
ifstream myFile("data.txt");
// Have we found a match?

// Store binary information

system("pause");

}

Using comments as place holders can help. The comments reflect the
pseudocode so everything should be in the right place. The line
system("pause"); is not entirely standard, and hence non-portable. It
is a way of stopping the console screen disappearing before you have
had time to read it. It will work on MS Windows.

When you are having problems don't try to solve all of them at once.
Work through them one at a time. Start with reading the file. In
your earlier code you used getline(), which is the correct way to read
a single line from a file. However a file may have more than one line
so you need to put getline() into a loop so you can read all of the
lines in the file. You are going to have to think about where to keep
each line as you read it, you don't want the next line to overwrite
the previous line before you have had a chance to look for a match.
At some point you are going to reach the end of the file, so you are
also going to have to detect the end of the file and exit the loop
gracefully.

Once you can read the file successfully then start thinking about how
to look for a match.
Is there anyone who can help me please? I am a beginner in C++, and I have
so far figured out how to read input from a user. I have a very long way to
go, and I only have a week to do this. Any help would be greatly
appreciated.

Thanx.


--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.