How do I get this to only output the userinput that it finds.

#include <iostream>
#include <string> // Note: This library is needed to use the string type
using namespace std;

int main() {
string userInput;

cout << "Enter text: " << endl;
getline(cin, userInput);

cout << "You entered: " << userInput << endl;

if (userInput.find("BFF,0")) {
cout << "BFF: best friend forever" << endl;
}

if (userInput.find("IDK, 0")) {
cout << "IDK: I don't know" << endl;
}


if (userInput.find("JK")) {
cout << "JK: just kidding" << endl;
}

if (userInput.find("TMI")) {
cout << "TMI: best friend forever" << endl;
}

if (userInput.find("TTYL")) {
cout << "TTYL: too much information" <<endl;

}

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
    std::cout << "enter text: " ;
    std::string text ;
    std::getline( std::cin, text ) ;
    std::cout << "you entered '" << text << "'\n" ;

    const std::string acronym = "BFF" ;
    const std::string expanded = "best friend forever" ;

    const std::size_t pos = text.find(acronym) ;
    if( pos != text.npos ) // if it was found
    {
        std::cout << acronym << " => " << expanded << '\n' ;
        text.replace( pos, acronym.size(), expanded ) ; // replace the acronym with its expanded form
        std::cout << "modified text: '" << text << "'\n" ;
    }

    // etc.
}
Using OP method, possibly:

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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void toUpper(std::string& str) {
	for (auto& ch : str)
		ch = static_cast<char>(toupper(static_cast<unsigned char>(ch)));
}

int main() {
	string userInput;

	cout << "Enter text: ";
	getline(cin, userInput);

	cout << "You entered: " << userInput << '\n';
	toUpper(userInput);

	if (userInput.find("BFF") != string::npos)
		cout << "BFF: best friend forever\n";

	if (userInput.find("IDK") != string::npos)
		cout << "IDK: I don't know\n";

	if (userInput.find("JK") != string::npos)
		cout << "JK: just kidding\n";

	if (userInput.find("TMI") != string::npos)
		cout << "TMI: too much information\n";

	if (userInput.find("TTFN") != string::npos)
		cout << "TTFN: ta ta for now\n";
}


@zsess,
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Using arrays, then possibly something like this - where it is easier to add new acronyms/phrases:

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
#include <iostream>
#include <string>
#include <cctype>
#include <array>
#include <algorithm>
#include <iterator>

constexpr std::array acronym { "BFF", "IDK", "JK", "TMI", "TTFN" };
constexpr std::array expanded { "best friend forever", "I don't know", "Just kidding", "too much information", "ta ta for now\n" };

static_assert(acronym.size() == expanded.size());

void toUpper(std::string& str) {
	for (auto& ch : str)
		ch = static_cast<char>(toupper(static_cast<unsigned char>(ch)));
}

int main() {
	std::string userInput;

	std::cout << "Enter text: ";
	std::getline(std::cin, userInput);

	std::cout << "You entered: " << userInput << '\n';
	toUpper(userInput);

	if (const auto fnd { std::ranges::find(acronym, userInput) }; fnd != acronym.end())
		std::cout << userInput << ": " << expanded[std::distance(acronym.begin(), fnd)] << '\n';
}

Last edited on
Topic archived. No new replies allowed.