Cant search words from an input :'(

Hi i am trying to create a c++ program that asks for an input and determines if it is a question or not by searching each word from the input across a database (txtfile)..... so far I have managed to make it search the database for the word but it only works on one word.... i would like to be able for it to search every word on the input seperatly.. is there a way to do this as I am really struggling...

here is the code:

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
string input1, input2;//declaring inputs
getline(cin, input1);//getting input1
input2 = "question." + input1;//giving input2 the value of input1 plus adding a "question." to the beggining
ifstream getfile;//loading the file
getfile.open("database.txt");//loading the file

if(!getfile.is_open()){
cout << "\nFile doesn't exist\n";//anouncing if the file doesnt exist
exit(EXIT_FAILURE);
}

int discovery = 0;//setting a variable for if the word has been discovered
char word[50];
getfile >> word;
while(getfile.good()){
if(word == input2)//if the database has been found announce its discovery
{
cout << "\nThe database has found " << input1 << endl << endl;
discovery = 1;
}
getfile >> word;
}
if (discovery == 0){//announces if the words wasnt discovered
cout << "\nThe database hasn't found " << input1 << endl << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Just check for a ? mark at the end......................

This is the only way I know how. Use a C style string and a pointer to the cstyle string

Google it to learn how!
Last edited on
but i want it to be so you don't have to put a question mark on... is there any way that i can pick out certain words out of a variable or move each word into part of an array?.... for example the variable is "what is the day" and i can pick out the "what" and search it on the database?
yes ofc you can. Search up C Style Strings!

I am in a hurry right now to do something but I will give you some sample code in the morning that does exactly what you want it to do!

ok?
ive made my own solution :D

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
int relatewords = 0;
char input1[255];
cout << "\n\nEnter the sentence for testing: ";
cin.getline(input1,255);
string wordstore[30];
int wordcount = 0;
int i = 0;
int prev = i - 1;
int beg = -1;
int end = 0;
int calcpos = 0;
int calcposcnt = 0;
while(input1[i])//while an input character exists
{
if(input1[i] == ' ')//if there is a space continue
{
if(input1[prev] != ' ')//if the previus character isnt a blank or space
{

beg = beg -2; //bug fix for beggining

while((input1[beg] == ' ') || (input1[beg] == 0))
{
beg = beg + 1;
}
calcpos = beg;//setting the calculator possition to the beggining of the word
end = i - 1;//bug fix for end
calcposcnt = 0;//resets the letter count

while(calcpos <= end)//calculates length of word
{
wordstore[wordcount] = wordstore[wordcount] + input1[calcpos];// adds the word to an array
calcposcnt++;//counts up 1 digit
calcpos++ ;//moves to the next digit on the word
}
}
prev = i - 1;

}
else
{
if(input1[prev] == ' ')
{
calcpos = i;
wordcount = wordcount + 1;
beg = i;
//cout << "\nFound a word: " << wordcount << endl;
}
prev = i - 1;
}
i++;
}

int check = 0;
int discovery = 0;

while(check <= wordcount)
{
ifstream getfile;
getfile.open("database.txt");

if(!getfile.is_open()){
cout << "\nFile doesn't exist\n";
exit(EXIT_FAILURE);
}

char word[50];
getfile >> word;
while(getfile.good()){
if(word == wordstore[check])
{
cout << "\n\nThe database has matched: " << wordstore[check] << endl;
relatewords = relatewords + 1;
}
getfile >> word;
}
check++;
}
cout << "\n\nWords that indicate a question: " << relatewords << endl << endl;
check = 0;
while(check<=wordcount)
{
cout << "Word " << (check + 1) << ": " << wordstore[check] << endl;
check++;
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
Topic archived. No new replies allowed.