How To Implement a Word Recognizing Function Written In C++

  • Thread starter Thread starter Deleted User
  • Start date Start date
  • Views Views 1,044
  • Replies Replies 6
D

Deleted User

Guest
I am working on a text based program written in C++, and I am trying to implement a command reading function but I am unsure where to start. I have tried Google but Google has yielded little to no results detailing this problem. For example I am trying to make a program will ask the user a question and then read the character string the user recorded and try to find a valid verb or adjective and based upon this information it would give the user a answer. I was thinking I could use a if statement and do something like this

Code:
if(Command == "(text)")
{
   cout << "(output)" << endl;
}

but not only does this lack flexibility, it also will only read the first part of the string, as strings only record a string of characters and are broken by white-space. I was also thinking I could have the user record their input in stages such as verb adjective then noun but this would only work in certain situations. It is a puzzling dilemma to say the least and I was hoping someone knew a way to implement this in a way that would work. Thank you for any help that you are willing to provide.
 
I am working on a text based program written in C++, and I am trying to implement a command reading function but I am unsure where to start. I have tried Google but Google has yielded little to no results detailing this problem. For example I am trying to make a program will ask the user a question and then read the character string the user recorded and try to find a valid verb or adjective and based upon this information it would give the user a answer. I was thinking I could use a if statement and do something like this

Code:
if(Command == "(text)")
{
   cout << "(output)" << endl;
}

but not only does this lack flexibility, it also will only read the first part of the string, as strings only record a string of characters and are broken by white-space. I was also thinking I could have the user record their input in stages such as verb adjective then noun but this would only work in certain situations. It is a puzzling dilemma to say the least and I was hoping someone knew a way to implement this in a way that would work. Thank you for any help that you are willing to provide.

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
using namespace std;

int main()
{
cout<<"Input any character, then press <Enter> \n";
char input;
cin>>input;
if (input=='f')
{
cout<<"You pressed F, pay respects";
}
else{
cout<<"You pressed " << input <<", no need to pay respects";
}

return 0;
}

https://www.onlinegdb.com/online_c++_compiler

Paste and > Run

Do note cin >> is implementation defined, so if you were to use it in, say a NDS, the method would have to implement a keyboard function and a submit string option (submit to a char buffer). In Unix (or Linux, such as the website I copied), the cin >> is implemented by reading keys from the keyboard and the submit action is the Enter key. fyi

Also char value == 'a' can be done, as it is an atomic operation (https://stackoverflow.com/questions/8290768/is-assignment-operator-atomic). But for char streams you need to use POSIX stuff, or C++ stuff (i'd suggest you learn stuff from C++ through the std::string namespace).
 
Last edited by Coto,
  • Like
Reactions: Deleted User
Strings do have whitespace? They are really just a char array so they can store anything
If you're getting that string from std::cin then you have to use cin in some different way with std::getline or something to get a full line with whitespace
And also I'm not sure if you can just use == to compare strings, you might want to use strcmp from <cstring> but maybe I just didn't know about that or it's some thing with std::string
 
  • Like
Reactions: Deleted User
https://www.onlinegdb.com/online_c++_compiler

Paste and > Run

Do note cin >> is implementation defined, so if you were to use it in, say a NDS, the method would have to implement a keyboard function and a submit string option (submit to a char buffer). In Unix (or Linux, such as the website I copied), the cin >> is implemented by reading keys from the keyboard and the submit action is the Enter key. fyi

Also char value == 'a' can be done, as it is an atomic operation (https://stackoverflow.com/questions/8290768/is-assignment-operator-atomic). But for char streams you need to use POSIX stuff, or C++ stuff (i'd suggest you learn stuff from C++ through the std::string namespace).

Strings do have whitespace? They are really just a char array so they can store anything
If you're getting that string from std::cin then you have to use cin in some different way with std::getline or something to get a full line with whitespace
And also I'm not sure if you can just use == to compare strings, you might want to use strcmp from <cstring> but maybe I just didn't know about that or it's some thing with std::string

Thank you I for your help with this information I shall be able to implement the function
 
I am working on a text based program written in C++, and I am trying to implement a command reading function but I am unsure where to start. I have tried Google but Google has yielded little to no results detailing this problem. For example I am trying to make a program will ask the user a question and then read the character string the user recorded and try to find a valid verb or adjective and based upon this information it would give the user a answer. I was thinking I could use a if statement and do something like this

Code:
if(Command == "(text)")
{
   cout << "(output)" << endl;
}

but not only does this lack flexibility, it also will only read the first part of the string, as strings only record a string of characters and are broken by white-space. I was also thinking I could have the user record their input in stages such as verb adjective then noun but this would only work in certain situations. It is a puzzling dilemma to say the least and I was hoping someone knew a way to implement this in a way that would work. Thank you for any help that you are willing to provide.
Use strtok to split the string into an array with words separated, then you can compare each word against the list.
 
  • Like
Reactions: Deleted User
Thank you I for your help with this information I shall be able to implement the function

Again, https://www.onlinegdb.com/online_c++_compiler

Paste and > Run

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string> //C++! (as opposed to <string.h> which is C)

using namespace std;

int main()
{
std::string strQuestion;
char * strAnswer = "green"; //C (not C++, which means this object holds a pointer to a reference!)
strQuestion = std::string("Try to guess the color ");
cout<< strQuestion <<" write and press <Enter> \n";

std::string input; //C++ (not C, which means this object holds a reference to itself + namespace std::string!)
cin>>input;

#define compareCorrect (int)(0)

if(input.compare(std::string(strAnswer)) != compareCorrect){ //1) Note the cast from C to C++, 2) Note how we use a function from the std::string namespace
cout << "Wrong Answer!" << endl;
}
else{
cout << "Correct Answer!" << endl;
}

return 0;
}

This one should clarify the difference between C++ and C
 

Site & Scene News

Popular threads in this forum