The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
#include "Hashtable.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
if(argc != 2)
{
cout << "Invalid usage ./dictionary <inputfile>" << endl;
return 0;
}
ifstream input;
input.open(argv[1]);
if(!input.is_open())
{
cout << "Unable to open the input file" << endl;
return 0;
}
int num = 0;
input >> num;
Hashtable dictionary(num*2);
if(!dictionary.FillFromFile(input))
{
cout << "Error filling from file" << endl;
return 0;
}
string phrase;
string notfound[200];
int count;
string temp;
string word;
int dummy = 0;
while(dummy == 0)
{
cout << "Enter a phrase ('quit' to quit): ";
getline(cin, phrase);
if(phrase == "quit")
break;
count = 0;
istringstream iss(phrase);
while(iss >> word)
{
temp = dictionary.Search(word);
if(temp != "")
{
cout << temp << " ";
}
else
{
notfound[count] = word;
cout << "*" << notfound[count] << "*" << " ";
++count;
}
}
cout << endl;
char yn;
for(int i = 0; i < count; i++)
{
do
{
cout << "The word '"<< notfound[i] << "' is not in the dictionary. Do you wish to add it? (y/n): ";
cin >> yn;
if(yn != 'y' && yn != 'n')
cout << "Not a valid input. y/n only." << endl;
}
while(yn != 'y' && yn != 'n');
if(yn == 'y')
{
string spanish;
cout << "Enter the Spanish definition of '" << notfound[i] << "': ";
getline(cin, spanish);
dictionary.Insert(notfound[i], spanish);
}
}
}
return 1;
}
output
Enter a phrase ('quit' to quit): do you speak spanish
hacer tu hablar *spanish*
The word 'spanish' is not in the dictionary. Do you wish to add it? (y/n): y
Enter the Spanish definition of 'spanish': Enter a phrase ('quit' to quit):
As you can see, the second getline where I take in the definition of the word is skipped for some reason.
ps, I know "do you speak spanish" != "hacer tu hablar espanol" but we are just translating it word for word.
You need to flush the input buffer because it has a \n character in it after you enter the 'y' or 'n'. Unfortunately I can't remember how to do that. This is a common problem, I'm guessing a google search will help.
Posts
Edit: a quick google search reveals that cin.ignore() is the function you want to use: http://www.cppreference.com/cppio/ignore.html
It works.
Thanks.