【问题标题】:1 Token is not getting stored in my array1 令牌未存储在我的数组中
【发布时间】:2015-05-10 13:39:58
【问题描述】:
#include <iostream>
#include<string.h>
#include<cstring>
#include<ctype.h>

using namespace std;

char *Data1[100];
char *operators[20];
char *identifiers[20][20];
int *ascii[100] = {0};
int ascii2[100] = {0};
unsigned int Tcount = 0;
unsigned int i;

int main(void)
{
    char *text = (char*)malloc ( 100 *sizeof( char));
    cout << "Enter the first arrangement of data." << endl;
    cin.getline(text, 100);
    char *token = strtok(text, " ");
    while ( token != NULL )
    {
        token = strtok(NULL, " ");
        Data1[Tcount] = token;
        cout<< Data1[Tcount] << endl;
        Tcount++;

    }
    for(i=0; i < Tcount; i++)
    {

        ascii[i] = (int)Data1[i];

//从指针转换为更小的类型'int'丢失信息错误

        cout << ascii[i] << endl;
    }


    return 0;
}

我首先尝试将令牌存储在一个名为“Data1”的数组中。这样做时,我将数据“X = A + 1”输入到用户输入中。由于某种原因,第一个令牌(X)没有存储在数组中,所以我的第一个问题是如何解决这个问题。
其次,我试图使用存储在数组中的这些标记并将它们转换为 ASCII,以便它们可以用于解析。我收到一个错误“从指针转换为更小的类型‘int’会丢失信息” 我想知道是否有人知道如何解决这些问题,尤其是第一个问题。谢谢。

【问题讨论】:

    标签: arrays parsing ascii stringtokenizer


    【解决方案1】:

    解决方案:

    char data = (char)malloc ( 100*sizeof( char));   
    while ( token != NULL )   
    {   
       data[Tcount++] = *token;  
       token = strtok(NULL, " ");  
       for(i=0; i < (Tcount*2); i++)  
       {  
       ascii[i] = (int)data[i];   
       }  
     }   
    for(i=0; i < (Tcount); i++)  
     {  
        cout << ascii[i] << endl;  
     }   
    return 0;   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2021-11-03
      • 2022-01-15
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多