Finding and displaying a word found in a string

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()

{
//Program Goal
//Have 3 Strings to represent 3 categories
//Have the user type in their name
//Have the user type in a full sentence how they are feeling
//Program should find the word in the user inputted sentence and display an appropriate response

//Error that I'm having currently... It seems to go through the loop twice
//and that it displays the wrong word first.



string name;
std::string feeling;
/*std::string str1 ("fine" "decent");*/
std::string str1[2] = {"fine", "decent"};
std::string str2[2] = {"bad", "horrible"};
std::string str3[1] = {"good"};
std::string words = "";
int i = 0;
cout<<"Input your name: ",
getline (cin,name);

cout<<"\nHello "<< name <<"\n"<< endl;


//asking
cout<<"How are you feeling today?? ",
getline (cin,feeling);


//finding the word in string 1
std::size_t found = feeling.find(str1[2]);

//finding the word in string 2
std::size_t found2 = feeling.find(str2[2]);

//finding the word in string 3
std::size_t found3 = feeling.find(str3[1]);


if (found=std::string::npos)
for (int i= 0; i < 2; i++)
std::cout <<"\n\nI'm glad you are feeling " << str1[i]<< " "<< name << "."<< "\n\n";
else
if (found2=std::string::npos)
for (int i= 0; i < 2; i++)
std::cout << "\n\nI'm sorry that you are feeling " << (str2[i])<< " "<< name <<"."<< "\n\n";
else
if (found3=std::string::npos)
for (int i= 0; i < 2; i++)
std::cout << "\n\nI'm glad you are feeling " << (str3[i])<< " "<< name <<"."<< "\n\n";


return 0;
}


//Results on Screen
//Why does this display twice and how can I fix this?

//Input your name: john
//
//Hello john
//
//How are you feeling today?? I'm feeling decent today
//
//
//I'm glad you are feeling fine john.
//
//
//
//I'm glad you are feeling decent john.
//
//Press any key to continue . . .
Last edited on
replace
1
2
3
if (found=std::string::npos)
    for (int i= 0; i < 2; i++)
        std::cout <<"\n\nI'm glad you are feeling " << str1[i]<< " "<< name << "."<< "\n\n";
with
1
2
if (found != std::string::npos)
    std::cout <<"\n\nI'm glad you are feeling " << str1[found]<< " "<< name << "."<< "\n\n";
Do the same with othe cases
Last edited on
that doesn't work and gives no response...

// Result on screen.
Input your name: john

Hello john

How are you feeling today?? i'm feeling decent today
Press any key to continue . . .

=====================

I also tried this version but it crashes the program..

if (found = std::string::npos)
std::cout <<"\n\nI'm glad you are feeling " << str1[found]<< " "<< name << "."<< "\n\n";

else
if (found2 = std::string::npos)
std::cout <<"\n\nI'm glad you are feeling " << str2[found2]<< " "<< name << "."<< "\n\n";

else
if (found3 = std::string::npos)
std::cout <<"\n\nI'm glad you are feeling " << str3[found3]<< " "<< name << "."<< "\n\n"

Ow, I misread your code. It has a whole bunch of problems.

std::size_t found = feeling.find(str1[2]); Out of bound access: array str1 has only 2 element and you are trying to access third here.

if (found=std::string::npos) You are assigning value instead of checking for equality.

Logic error: You don't actually store index of found string.
To do so you might do something like:
1
2
3
4
int found_good = -1;
for (i = 0; i < 2; ++i)
    if (feeling.find(str1[i]) != std::string::npos)
        found_good = i;
Thanks for the help and now it's working! 2 months trying to figure this out lol.. Well not a constant 2 month but thanks!! :)

Revised and working code..

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()

{
string name;
std::string feeling;
std::string str1[2] = {"fine", "decent"};
std::string str2[2] = {"bad", "horrible"};
std::string str3[1] = {"hungry"};

cout<<"Input your name: ",
getline (cin,name);

cout<<"\nHello "<< name <<"\n"<< endl;


//asking
cout<<"How are you feeling today?? ",
getline (cin,feeling);


//finding the word in string 1
std::size_t found = feeling.find(str1[1]);


//finding the word in string 2
std::size_t found2 = feeling.find(str2[1]);

//finding the word in string 3
std::size_t found3 = feeling.find(str3[0]);

for(int i = 0; i<2; ++i)
{
if (feeling.find(str1[i]) != std::string::npos)
{
std::cout <<"\n\nI'm glad you are feeling " << (str1[i])<< " "<< name << "."<< "\n\n";
}
else
if (feeling.find(str2[i]) != std::string::npos)
{
std::cout << "\n\nI'm sorry that you are feeling " << (str2[i])<< " "<< name <<"."<< "\n\n";
}
else
if (feeling.find(str3[i]) != std::string::npos)
{
std::cout << "\n\nYou are feeling " << (str3[i])<< " "<< name <<"??"<< "\n\n";
}
}
return 0;
}
Topic archived. No new replies allowed.