This program is working but the Error Check is kicking my butt for some reason because it detects the error but it doesn't give me a chance to retry well it does but.. You'll see if you try the code...
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string name;
string feeling;
string str1[3] = {"fine", "decent", "very good"};
string str2[2] = {"bad", "horrible"};
string str3[1] = {"hungry"};
cout<<"Input your name: ",
getline (cin,name);
//First response
cout<<"\nHello "<< name <<"\n"<< endl;
cout << "*** You may type a full sentence in response. ***\n\n";
cout << "Use one of the following words within the sentence.";
cout << "\t\nfine, decent, very good";
cout << "\t\nbad, horrible, hungry";
//asking
cout<<"\n\nHow are you feeling today?? ",
getline (cin,feeling);
{
//finding the word in string 1
size_t found = feeling.find(str1[2]); // Increase (str1[?]) when adding words the appropriate string.
//finding the word in string 2
size_t found2 = feeling.find(str2[1]);
//finding the word in string 3
size_t found3 = feeling.find(str3[0]);
//Finding the appropriate response check.
int i;
for( i = 0; i<3; ++i) // Increase (i<?) when adding words to any above string.
{
if (feeling.find(str1[i]) != string::npos)
{
cout <<"\n'm glad you are feeling " << (str1[i])<< " "<< name << "."<< "\n\n";
break;
}
elseif (feeling.find(str2[i]) != string::npos)
{
cout << "\n\nI'm sorry that you are feeling " << (str2[i])<< " "<< name <<"."<< "\n\n\n";
break;
}
elseif (feeling.find(str3[i]) != string::npos)
{
cout << "\n\nYou are feeling " << (str3[i])<< " "<< name <<"??"<< "\n\n\n";
break;
}
}
while (i==3 ) {//Error Check
cout<<"\nError!! You have entered something wrong\nPlease try again: ";
getline(cin, feeling);//Keeps repeating the error check no matter what I put in..
}
}
return 0;
}
Well, then I==3 would increase to be greater than 3 in the error check if I add more words..
The program is actually smart enough to determine what word you type in the sentence and gives the appropriate response.. Hence I need to have a retry incase if someone types in something that's not in the strings..
For example.... "How are you feeling today??"
User Input: "I'm feeling fne today."
Program Response: "Error!! You have entered something wrong.. Please try again:"
At this point the user should be able to try again but the program keeps popping up this error check instead of checking what the user puts in... That's the part I need help on..
Lets pretend that I write something. X.
The i==0.
X is not "fine", so line 51 is false.
X is not "bad", so line 58 is false.
X is not "hungry", so line 64 is false.
The i increments to 1.
X is not "decent", so line 51 is false.
X is not "bad", so line 58 is false.
Line 64 attempts to read str3[1], but the str3 array has only one element, str3[0]. This is a range error.
Perhaps you are (un)lucky and reading "string" from unknown memory doesn't crash.
Now the i is indeed 3 and the while loop starts.
Line 71 does not change the i.
Line 72 does not change the i.
The i is still 3 and the while loop continues.
Line 71 does not change the i.
Line 72 does not change the i.
The i is still 3 and the while loop continues.
Line 71 does not change the i.
Line 72 does not change the i.
The i is still 3 and the while loop continues.
...