IfJane 2022-06-13 21:34 采纳率: 50%
浏览 22
已结题

关于C++strcmp的问题

C++语言,用的vscode,代码如下


```c++
#include<iostream>
#include<string>
#include <stdio.h>
#include <cstring>
using namespace std;

int main()
{
    short intend;
    string first_name, last_name;
    cout << "Please enter your first name AND last name:" << endl;
    cin >> first_name
        >> last_name;
    cout << "hello,"
         << last_name << first_name
         << " Do you want to play some number series puzzles?(0/1)" << endl;
    cin >> intend;
    if (intend == 0) //未完成部分
    {
        int intend2;
        cout << "Ok, So would you want to play some word games?(0/1)\n";
        cin >> intend2;
        if (intend2 == 1)
        {
            char *word;
            const char *trueword = "twelve plus one";
            cout << "Re-construct the sentence with 'eleven plus two'." << endl;
            cin >> word;
               
            if (strcmp(word, trueword) == 0)
            {
                cout << "You're right!" << endl;
            
            }
            if (strcmp(word, trueword) != 0)
            {
                cout << "false\n";
            }
        }
        
    } 
    else if (intend == 1)
    {
        int num1;
        cout << "1, 1, 2, 3,() ,8, ...What is the next num\n";
        cin >> num1;
        if (num1 == 5)
        {
            cout << "You're right!" << endl;
        }
        else if (num1 != 5)
        {
            cout << "Doesn't seem right..." << endl;
        }
    }  
    else
    {
    cout << "1=YES, 0=NO, You can try again." << endl;
    return 0;
    } 
    cout << " and goodbye!\n";
    system("pause");
    return 0;
}

具体问题:
在未完成部分if (strcmp(word, trueword) == 0)
输入twelve past one(正确答案)后,卡顿约2秒,退出程序。
各位来帮忙看看吧

  • 写回答

2条回答 默认 最新

  • 假装不困 2022-06-13 22:17
    关注

    运行结果如图, 只贴了你需要的那部分,你需要明白,你开始给出来的代码,cin 每次都是按照空格分割,表示当前输入截止,无法帮你录入一个句子

    需要改成 getline,这个时候呢,还有个小问题,参考这里自行理解:https://stackoverflow.com/questions/28956376/when-use-getlinecin-string-cin-automatically-take-its-value-without-ask

    img

     char word[100];
                char trueword[] = "twelve plus one";
                cout << "Re-construct the sentence with 'eleven plus two'." << endl;
                cout << "Input you answer:";
                std::cin.ignore();
                std::cin.getline(word, 100);
    
                if (strcmp(word, trueword) == 0)
                {
                    cout << "You're right!" << endl;
    
                }
                if (strcmp(word, trueword) != 0)
                {
                    cout << "false\n";
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 创建了问题 6月13日

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题