Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<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:
Posted
Updated 9-Feb-11 22:33pm
v4

Move your test.

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

Then you run through each token:
C++
while (pch != NULL)
{
  printf ("%s\n",pch);
  pch = strtok (NULL, " ,.-");
}

Then you try to check the second one:
C++
if(strcmp( pch++, "input"))
{
    cout<<"yes";
}

The problem is that by the time to check, you have run out of tokens completely...
Try this:
C++
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++;
}

That way, you check the second token against your match string while you can still get at it! :laugh:
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Feb-11 10:55am    
5--SA
And here is a solution using The Standard C++ Library.
C++
#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;
}


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]
 
Share this answer
 
v3
Comments
Niklas L 10-Feb-11 4:21am    
Good one!
Nuri Ismail 10-Feb-11 4:33am    
Thanks a lot! :)
Niklas L 10-Feb-11 4:49am    
You could have used 'boolalpha' to save a line of code :p
Nuri Ismail 10-Feb-11 5:00am    
Yes, the original version was exactly with "boolalpha" for "cout". :)
But printing "yes" or "no" was specific requirement from OP, I decided to change my code before posting and sacrificed one line of code with an additional string. =D
Sergey Alexandrovich Kryukov 10-Feb-11 10:55am    
5,--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900