Pattern Matching

K

kittykat

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.
 
R

rossum

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top