Help with this error check please!!

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...

Any help that you could give would be awesome!!

I've tried to label everything that needed to be.



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
74
75
76
77
78
79
#include <iostream>
 #include <string>
 #include <iomanip>

 using namespace 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;
	}
		else
		if (feeling.find(str2[i]) != string::npos) 
			{
			cout << "\n\nI'm sorry that you are feeling " << (str2[i])<< " "<< name <<"."<< "\n\n\n";
			break;
			}
		else
		if (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;
 }
Last edited on
The i can go up to 2 in the loop. What happens, if you dereference str2[2], str3[1], or str3[2]?
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..
What happens, if you dereference str2[2], str3[1], or str3[2]?


In the code I tagged it what that's for.. Basically when I add words to....

string str2[2] = {"bad", "horrible"};

I then need to increase.
1
2
 //finding the word in string 2
 size_t found2 = feeling.find(str2[1]); // Increase (str2[?]) when adding words the appropriate string. 




But like I said before the program works fine but the error check is what I need help with...
No.

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.
...
Well, do you have another idea for doing the error check instead of while (i==3)?
Last edited on
Topic archived. No new replies allowed.