Hi, i am trying to make a program that gets words from a txt file (strings)
and see if the words have the character the user has inputed before.
for example...

the program will ask the user to input a letter. ( the user inputs, lets say a letter 'k')
lets say the txt file contains 5 words
dog
kiss
car
airplane
kick
since only 2 words out of the list has the letter k, the program will out put "2/5 words have the letter k in the text file"

so far i have come this far.
---------------------------------------------------------------------------------------------------

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

using namespace std;



int main ()

{
	
	ifstream infile("words.txt");
	string a_word;
	int total_words = 5;
	int correct_letters = 0;
	
	
	
	char c;
	cout << "enter letter";
	cin >> c;

	while (!infile.eof())
	{infile >> a_word ;

		
				
	}

cout << correct_letters << "/" << total_words << "words have" <<" "<<"letter"<<" "<<c<< "in the file";



return 0;

}

-----------------------------------------------------------------------------
I have no clue what my next step is.

Recommended Answers

All 8 Replies

actually, if I can do something like this..

bool hasaletter (string, char)
if (true)
{ ++correct_letters;}

would this work??

line 25 and 26 should be combined like this while( infile >> a_word )

Hi, i am trying to make a program that gets words from a txt file (strings)
and see if the words have the character the user has inputed before.
-----------------------------------------------------------------------------
I have no clue what my next step is.

Maybe look at the word entered (a_word) for the letter specified (c)?

yes, How can i get C++ to read a character from a string??
like, I know i cant do
infile >> a_word>>char

If this is homework. look up strings and their methods in your text. If not, look up strings where you are getting the rest of your information. This is a trivial problem.

trivial?? haha now i feel like shet, i am actually trying really hard in this class, my major requires this one and only computer based class and when it comes to computer related work, I just have trouble understanding it.

I just have trouble understanding it.

Then look up what I mentioned and ask a specific question to clear up what you don't understand.

>>yes, How can i get C++ to read a character from a string??

After reading a line from the file you will want to create a loop that checks each character in the line for the character that you entered at the keyboard. std::string has a couple methods size and length that tell you how many characters are in the string so your loop counter should run from 0 up to but not including that number. Then just use the loop counter to index into the string just as you would any other ordinary array like this: if ( word[i] == c) >>trivial?? haha now i feel like shet
Just because its trivial doesn't make it easy. Hammering a nail into a board is trivial to a carpenter, but I can't do it easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.