打印所有空白(实际上不打印任何标记)

Printing all Blanks (literally no tokens are printed)

本文关键字:打印 任何标 空白 实际上      更新时间:2023-10-16

基本上我的代码不打印令牌。它只是打印空白。我做错了什么?我已经就此问题咨询了许多其他指南,但我似乎无法理解我做错了什么。

谢谢。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
#include <stdexcept>
#include <string>
#include <stdlib.h>
#include <cstring>
using namespace std;
int main() {
  stack<double> s;
  string str = "";
  getline(cin, str);
  char *cstr = new char [str.length()+1];
  strcpy(cstr, str.c_str());
  char* strp = strtok(cstr, " ");
  while (strp != 0){
        double n = strtod(cstr, &strp);
        s.push(n);
        cout << strp << endl;
        strp = strtok(0," "); 
        }
  return 0; 
}

这段代码对我有用:

int main()
{
   stack<double> s;
   string str = "";
   getline(cin, str);
   char *cstr = new char [str.length()+1];
   strcpy(cstr, str.c_str());
   char* strp = strtok(cstr, " ");
   while (strp != NULL)
   {
      double n = strtod(strp, NULL);
      s.push(n);
      cout << n << endl;
      strp = strtok(NULL, " "); 
   }
   return 0;
}

但是见鬼,这真的是C和C++的痛苦混合。你应该摆脱那些strcpy,strtok和strod函数。请改用 istringstream。