Hey guys, so I want to make a program that finds and shows me all the words that contain a given amount of letters and a given letter in a specific position in a word, for example:
say I have the words: egg, desk, cart, tart, mouse, house
all those words are in a text file
the first variable I introduce is going to be the number of letters I want my program to show in the console.
so if I say 4, it's going to give me: desk, cart and tart
afterwards, its going to ask me for a letter, let's say the letter a.
then, its going to ask me for a position(a number which represents the place of a letter in a word), let's say 2;
so then it should give me these two words: cart and tart
the first part works fine but then when I try and make it so it filters out the words with non corresponding letters in a position I have the error: invalid conversion from 'char' to 'const char*'
here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <string.h>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;
ifstream f("words.txt");
struct wordData {
char word[20];
}data[100];
int main()
{
int nr_char; //this variable holds the number of letters the words that you want to search for have
char lit[1]; //a letter
int pos; //the position of said letter in a word
int n; //number of words in the text file
f >> n; //the first thing in the text file is the number of words
for(int i=1; i<=n; i++)
{
f >> data[i].word;
cout << data[i].word << " "; //this shows all the words from the text file in the console
cout << endl;
}
do
{
cout << endl;
cout << "Number of characters: ";
cin >> nr_char;
cout << endl;
cout << "Words with " << nr_char <<" letters: "<<endl<<endl;
for(int i=1; i<=n; i++)
{
if(nr_char == strlen(data[i].word))
cout << data[i].word << endl; //it works well up until this point, so far it has shows all the words with the number of letters you introduced
cout<<"enter a letter:";
cin>>lit;
cout<<endl;
cout<<"The position of the letter in a word:";
cin>>pos;
cout<<"All words with the given data: "<<endl;
if(strcmp(data[i].word[pos], lit)) //error: invalid conversion from 'char' to 'const char*' [-fpermissive]
cout<<data[i].word<<endl;
}
}while(nr_char != 0);
return 0;
}
|