如何使C ++程序与计算机通信 [英] How to make C++ program to talk with computer

查看:87
本文介绍了如何使C ++程序与计算机通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的策略是在用户给出的任何字符串中,如果某个特定单词将匹配,那么将会有特定的输出

我尝试了但是发生了一些逻辑错误尝试

输入

1)你好

2)东西elese



请帮忙先生



我的尝试:



What my strategy is in any string given by a user if a particular word will mmatch then there will be particular output
I have tried but some logical errors are occurring try
Input
1) hi there
2)something elese

Pls help sir

What I have tried:

#include<iostream.h>
#include<string.h>
using namespace std;
int main(){
char q;
    cout<<("Hello world!");
string s1;
string s2="hi";
string s11;
string s22="brain ";

cout<<"n ask something";
cin>>s1;

if((s1.find(s2)==0) !=string::npos)
{cout <<" hi ! nice to see you actually there was none to talk with me ";
cout<<"whats upp";
cin>>q;
cout<<"Thats interesting! I am doing fine myself! ";

}
 if((s1.find(s22)==0) !=string::npos) 
{cout<<" don't ask me such silly questions what if I ask you where is your brain";
}

else
{cout <<" what! Ask senceful question";
}
    return 0;
}

推荐答案

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


嗨会员13151654,



这个line有一个错误:

Hi Member 13151654,

This line has an error:
if((s1.find(s2)==0) !=string::npos)

它应该是:

It should be:

if (s1.find(s2) != string::npos)

find 用于在另一个字符串(此处为s1)中返回字符串(此处为s2)的位置。如果没有找到它返回-1(或4294967295为unsigned int)。所以基本上上面的行是检查 find 是否成功。



你的条件分支还不完全清楚。第一个IF检查输入字符串中是否有hi。第二个IF检查大脑是否存在,并且与第一个IF完全分离,并且是一个单独的分支本身。输入'q'也不会在任何地方使用。也许这就是你想要它的方式。请确定。

find is used to return the position of a string (s2 here) in another string (s1 here). If none is found that it returns -1 (or 4294967295 as unsigned int). So basically the above line is checking if a find was successful.

Your conditional branching is not fully clear. The first IF is checking if "hi" is in the input string. The second IF that checks if "brain" is there, and is completely separated from the first IF and is a separate branch itself. Also the input 'q' is not used anywhere. Maybe that's how you want it to be. Just be sure.


这篇关于如何使C ++程序与计算机通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆