Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I am currently learning c++ and creating an assistant for myself. I need to make sure the if command checks for a sentence, not a word in a string how do I do that.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
const string YES = "yes";
const string NO = "no";
//ignore this
int main () {
std::string Question;
std::string check;
std::cout << ("Hi sir how may i assist you?\n");
//user asks the question and then a if command checks the question from a list of questions and if the line is the same as in the database it gives a output
getline (cin, Question);
if (Question == "whats the weather?") {
std::cout << ("Well it is...\n");
//then it pulls data from the web and puts it in here
}
}
Output
Hi sir how may I assist you?
whats the weather
(nothing) else happens
In this:
if (Question == whats the weather?)
Add quotes around the sentence. You want to write
if (Question == "whats the weather?")
Also, since you're using namespace std, you don't need std:: before anything.
You can check for specific sentences with a syntax like:
if (Question == "whats the weather?")
However, to make your assistant more quickly usable you may instead want to lead them through some menus in the chat window, with them selecting from topics and functions. Just a suggestion to make developing your assistant easier.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to create an ATM application using c++ and I want to display asterisks (****) when I input the pin code. I haven't tried anything because I have no idea on how to do it.
Could someone help me?
A very small program which should help you get started for Windows would go like this:
int main(void)
{
char character = 0;
while (true)
{
character = _getch();
if (character == 13) //Enter
break;
else
{
putc('*');
//Do whatever
}
}
}
Please note that this code is off the top of my head and still requires the correct includes, but should point you in the right direction.
Please refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-160 for the documentation of _getch and http://www.asciitable.com/ for a list of control codes you might be getting.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
char restart = 'y';
while (restart == 'y')
{
cin >> restart;
cout << "great keep playing\n";
}
cout << "thanks for playing\n";
When I run this code it also displays the code outside of the loop as well, I even tried using a break statement but it didn`t work. How do I fix this?
You could write the loop like this:
while (cin >> restart && restart == 'y')
{
cout << "great keep playing\n";
}
The main issue I see is that you get the value of restart on the 'cin' line, then display 'great keep playing' on the next line, without first checking to see what the value of 'restart' is. You need an if() statement around the 'great keep playing' line, so it will only be displayed if you entered 'y'.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
goto is useful FOR BEGINNERS but not recommended !!!!
I am editing my post as I got the right answer on this website.
In this program where user enters subject marks, (after entering marks)he/she is asked whether he/she wants to enter more marks of subjects after declaring in the initial stage that of how many subjects he wants to enter numbers of and if he/she replies Y then program asks him to enter marks again. Look I am a university student of 1st semester and I found goto easier to make my program go to initial stage of program after using so many loops. All I need is to not use goto but use another loop so how can I do this (problem solved by eerorika who answered me).
#include<iostream>
using namespace std;
int main (){
int subjec;
retran:
cout<<"please enter number of subjects : " ;
cin>>subjec;
int marks[subjec];
for ( int u=0;u<subjec;u++){
cout<<"enter marks of subject "<< u+1 << " ";
cin>>marks[u];
}
char q='Y';
cout<<"do you want TO ENTER MORE MARKS : "<<endl;
cout<<"enter \"Y\" for Yes and \"N\" or any other character for No : ";
cin>>q;
while (q=='Y')
goto retran;
return 0;
Here is a request if you can tell me how I can go to initial stage of program again when user press Y without using goto statement.
GOTO statements are useful ?
Yes. But not for this use case.
how I can go to initial stage of program again when user press Y
There's a control flow structure for going back and repeating. It is called a loop. An example:
do {
// do some stuff
cin>>q;
} while(q=='Y');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm familiar with the Visual Studio IDE on Windows, and I wanted to give VSCode a shot. For my current situation, I am running VSCode on Ubuntu 64-bit. I installed VSCode, and then installed the C/C++ Extension. Next, I wrote a very simple C++ program that outputs "Hello World", and then asks the user to input their name. The program would then say/output hello to that user. Here's the issue: I am having trouble figuring out how to actually provide the user input to the program. During runtime, I see the cursor blinking in the output panel after the "Hello World" output, but when I press any keys on my keyboard to provide user input, nothing appears, and nothing happens. Any help would be greatly appreciated.
#include<iostream>
#include<string>
using namespace std;
int main() {
string name = "";
cout << "HELLO WORLD" << endl << endl;
cin >> name;
cout << "Hello " << name;
return 0;
}
Better use terminal then the Visual Studio Code Run feature
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
when I try to compile a simple program like this:
// i/o example
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i * 2 << ".\n";
return 0;
}
it starts, but when I put the integer value and press ENTER, it closes. I really don't know why it happens since it works good when I use Dev C++. And if I want to just compile my program I need to press ctrl+f5? Thank you.
What is likely happening is that the computer returns the two cout lines and then runs return 0, shutting down the program.
Since there is no pause, it just executes the code and exits, without waiting for further user input. This is what is giving the appearance of the program not working well.
If you put a breakpoint, or run the code in debug mode, or add a pause, your code will show the output as well.
And for future reference, you'll want to keep your questions to just one question per question. The Ctrl + F5 question can be found via Google or by searching SO.