我想在程序中回答是的时候去切换并做我在那儿写的工作,但不能 [英] I want in my program when the answer is yes go to switch and do a work that i write there but it cannot

查看:52
本文介绍了我想在程序中回答是的时候去切换并做我在那儿写的工作,但不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="xml">#include "StdAfx.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
char str[] ="10 input x";
char * pch;
pch = strtok (str," ,.-");
int i = 0;
while (pch != NULL)
{
  printf ("%s\n",pch);
  if((i == 1) && !strcmp( pch, "input"))
  {
    int accumulator =0;
    int memory[100];
    int counter=0;
    int opCode ;
    int operand ;
    int instructionRegister;
    for(int i=0;i<counter;i++){
        instructionRegister=memory[i];
        opCode =instructionRegister/100;
        operand =instructionRegister%100;
        switch ( opCode){
            case 10:
            cout<<"plz enter a number:\n";
            cin>>memory[operand];
            break;
        }
    }
  }else{
      cout<<"no"<<endl;
  }
  pch = strtok (NULL, " ,.-");
  i++;
}
    return 0;
}



:doh:



:doh:

推荐答案

进行测试.

目前,您开始令牌化:
Move your test.

At present, you start the tokenising:
char str[] ="10 input x";
char * pch;
pch = strtok (str," ,.-");


然后遍历每个令牌:


Then you run through each token:

while (pch != NULL)
{
  printf ("%s\n",pch);
  pch = strtok (NULL, " ,.-");
}


然后尝试检查第二个:


Then you try to check the second one:

if(strcmp( pch++, "input"))
{
    cout<<"yes";
}


问题是,到检查的时间,您已经用完了令牌...
试试这个:


The problem is that by the time to check, you have run out of tokens completely...
Try this:

char str[] ="10 input x";
char * pch;
pch = strtok (str," ,.-");
int i = 0;
while (pch != NULL)
{
  printf ("%s\n",pch);
  if((i == 1) && strcmp( pch, "input"))
  {
    cout<<"yes";
  }
  pch = strtok (NULL, " ,.-");
  i++;
}


这样,您就可以对照匹配字符串检查第二个标记,同时仍然可以找到它! :laugh:


That way, you check the second token against your match string while you can still get at it! :laugh:


这是使用标准C ++库的解决方案.
And here is a solution using The Standard C++ Library.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> vtokens;
    std::string str("10 input x");
    
    // Extract the tokens from string
    std::istringstream iss(str);
    std::copy(std::istream_iterator<std::string>(iss),
              std::istream_iterator<std::string>(),
              std::back_inserter< std::vector<std::string> >(vtokens));
    
    // Now "vtokens" contains all of the tokens from the initial string.
    // Your specific requirement was to check if the second token is "input",
    // Here you go:
    bool banswer = (vtokens.size() > 1) ? vtokens[1] == "input" : false;
    std::string str_answer(banswer ? "yes" : "no");
    std::cout << "Answer: " << str_answer << std::endl;
    
    // Optional: hold the screen to see the results
    std::cin.get();
    return 0;
}



我给你这作为你C风格代码的替代方法.
这是一个演示,说明如何避免使用strtok并在C ++中使用标准算法,容器和流.

:)

[更新]
OP稍微改变了他的问题,答案(来自我和来自OriginalGriff)是针对原始问题的.

到OP:
您正在从未初始化的内存中读取(请参见int memory[100];数组),并且正在使用未初始化的变量(instructionRegister变量).
[/UPDATE]



I give you this as an alternative to your C style code.
It''s a demonstration how can you avoid strtok and use standard algorithms, containers and streams in C++.

:)

[UPDATE]
The OP slightly changed his question and the answers (from me and from OriginalGriff) were for the original question.

To OP:
You are reading from uninitialized memory (see your int memory[100]; array) and you are using uninitialized variables (instructionRegister variable).
[/UPDATE]


这篇关于我想在程序中回答是的时候去切换并做我在那儿写的工作,但不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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