Spelling program

Hello I have a question.

I need to read in words from a line from a file.
for example if the first sentence contains apples aples apppples, i need to be able to read in the first word of the sentence and compare the other words in the same line as the files has many lines to read from. After reading in the first word in the sentence i need to compare the other words to see if the other words are miss spelled. If so how is it miss spelled etc.

[code]
This is my code so far. I was able to read in the first word in the sentence but don't know how to read in the next word.

fin.open("Spellingwordfile.txt");


fin >> word;


cout << "Spelling Correction by IKANT SPELL " << endl << endl;
//<< endl << "\n*****Starting a new line*****\n";


cout << "*****Starting a new line*****";
cout << "\nThe word being checked is " << word
<< endl << endl;


i also don't know how many words there might be in a sentence before i can go on to the next word.
you could use fin.peek() to check if the next character in the file is a new line.
there are some functions i am not allowed to use such as
fin.peek function , grab next word.
after reading in the first word which is spelled correctly how do i read in the next word and compare it to the original word. for example the first word is Apple
the next word is apppple. how do i compare the second word with the first word?
Maybe you could push all the words into a vector and compare the word at vec.at(0) with the rest of the vector values?

And to check how it is misspelled, make a separate function that will compare each char in the two strings (the correct string and the misspelt string) and output the part that makes them not the same?

This is my first time answering a question, but that's where I would try to start :)
You could try a getline for each line and Then use the individual lines in a string stream
Double O wrote:
i also don't know how many words there might be in a sentence before i can go on to the next word.

Your collections aren't sentences, they are what appears on one line.


I should go with @highwayman's suggestion:
You could try a getline for each line and Then use the individual lines in a string stream



Unless you want to distinguish proper nouns, you may also want to write a short function to turn each word into a common (probably lower) case.
I need to read in words from a line from a file.
for example if the first sentence contains

To be clear, does the input contain lines:
apple aple appel apple
banana bahnana bannana
pear pair pare


Or sentences:
apple aple appel apple. banana bahnana bannana. pear pair pare
?

after reading in the first word which is spelled correctly how do i read in the next word and compare it to the original word

Read the first word into a string variable (maybe call it correct because it's correctly spelled). Then use a loop to read the words that follow. After reading each word, compare it to correct to see if it's spelled right.
Hello Double O,

So far you have mentioned that you can not use "fin.peek()". So the question is what can you use? And what can you not use?

If you can not use a string stream or vector, maybe an array of strings?

Andy
Hello Double O,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still to indent your code.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This is your code with proper code tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
fin.open("Spellingwordfile.txt");


fin >> word;


cout << "Spelling Correction by IKANT SPELL " << endl << endl;
//<< endl << "\n*****Starting a new line*****\n";


cout << "*****Starting a new line*****";
cout << "\nThe word being checked is " << word
<< endl << endl;

First off it does not compile because of what is missing. It is always a good idea and helpful to have enough code to compile and test what you have.

Line 1 opens the file, but how do you know it is open and usable?

What you want is something like this:
1
2
3
4
5
6
7
8
fin.open("Spellingwordfile.txt");

if (!fin)
{
	std::cout << "\n File \"" << "Spellingwordfile.txt" << "\" did not open" << std::endl;

	return 1;
}

Topic archived. No new replies allowed.