Ask the user for the name of a file (dictionary) - "I don't need to do this as I am using a Mac.
You may assume that the given file contains only words, and that there are at most 1000 words in the file (there may be fewer)
Read all the words from the file, and store them in an array of type string.
Now ask the user to enter a word that you will search for in the array. If you find it, print the location at which it was found. Otherwise, print a message indicating that the word was not found.
You should consider the first word to be in position 1.
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
//CSC150 Lab12
//Ian Heinze
//11/20/2015
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string dictionary[1000], word;
ifstream in_stream;
in_stream.open("/Users/IanHeinze/Downloads/mydictionary11.txt");
//cout << "File name: ";
//cin >> filename;
cout << endl;
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
int i=0;
while(! in_stream.eof())
{
getline(in_stream, dictionary[i]);
i++;
}
int size=i;
int a;
bool found;
while(word != "stop")
{
cout << "Search for: ";
cin >> word;
if(word =="stop")
{
cout << "Bye!" << endl;
break;
}
for(int b=0; b < size; b++)
{
found = false;
if (word == dictionary[b])
{
found = true;
break;
a=b;
}
}
a=a+1;
if (found==true)
{
cout << "'" << word << " was found in position " << a << endl;
}
else
{
cout << "Sorry, word not found." << endl;
}
}
}
|
So this is the code I have at this point. No matter what it prints out that the word was not found. The dictionary file I'm using has the words:
apple
bear
cat
dog
egg
file
google
hello
iphone
jeep
Now, if I take one of the '=' from the 'found == true' out, then it just acts as a counter. No matter what I enter it says it's found and just adds one to the count. I'm not sure what's wrong with this code, and my instructor believes it might be something with the fact that several others and I are using Mac's. Just looking for some help and other opinions on what we could all do to make our code work? Thanks a million!