1

I am practicing some C++ but i am having trouble splitting the user input. I am using eclipse ide 2020-03, with mingw32-gcc-g++-bin 9.2.0-1. below is my code,

#include <iostream>
#include <string>
using namespace std;

int main() {
 string orderbook_symbol[500000][8]; //orderid, ordertime, symbol, buy/sell, qty, price, exp time, strike
 string user_order;
 char * pch;
 string done= "done trading";

 while(user_order.compare(done) != 0) {
     cin >> user_order;
     pch = strtok(user_order," ");
 }
}

and when i hit compile i see this error:

23:22:06 **** Incremental Build of configuration Debug for project stasd ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\stasd.o" "..\\src\\stasd.cpp" 
..\src\stasd.cpp: In function 'int main()':
..\src\stasd.cpp:26:9: error: 'strtok' was not declared in this scope; did you mean 'strtol'?
   26 |   pch = strtok(user_order," ");
      |         ^~~~~~
      |         strtol
23:22:06 Build Failed. 1 errors, 0 warnings. (took 560ms)

I took the example here: http://www.cplusplus.com/reference/cstring/strtok/ and i don't understand why a function call is getting not in scope error.

D.Zou
  • 761
  • 8
  • 25
  • `std::string != char*`, which is your issue here. However, I'm voting to close this as a dupe since the dupe explains how to do what you're trying while still using an `std::string`, which I'm guessing you want. – scohe001 May 18 '20 at 03:34

1 Answers1

6

You're including <string>, whereas strtok is part of <string.h> or <cstring>.

See include string or string.h for the differences between these.

Green-Avocado
  • 901
  • 5
  • 20