Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将C字符串转换为int数组_C++ - Fatal编程技术网

C++ 将C字符串转换为int数组

C++ 将C字符串转换为int数组,c++,C++,所以对于我的编程任务,我必须 处理输入文件中的数据并对其执行一些操作 以下是参数 一名教师的班级不超过40名学生,每个学生参加6次测试。对于 班上的每个学生在输入文件中都有一行。该行包含该学生的第一行 姓名(不超过10个字符)、姓氏(不超过12个字符)、ID#(6个字符的字符串)和6个整数测试分数。 输入文本文件应命名为 学生输入.dat 还要注意的是 姓名、身份证和等级 可以是任意的,即任何数字。不过至少会有一个空间。 最后一个等级和不可见符号之间可能有一个空格,表示行'/n'的结尾。 每行的

所以对于我的编程任务,我必须

处理输入文件中的数据并对其执行一些操作

以下是参数

一名教师的班级不超过40名学生,每个学生参加6次测试。对于 班上的每个学生在输入文件中都有一行。该行包含该学生的第一行 姓名(不超过10个字符)、姓氏(不超过12个字符)、ID#(6个字符的字符串)和6个整数测试分数。 输入文本文件应命名为 学生输入.dat

还要注意的是 姓名、身份证和等级 可以是任意的,即任何数字。不过至少会有一个空间。 最后一个等级和不可见符号之间可能有一个空格,表示行'/n'的结尾。 每行的字符总数不超过256个,包括字符的结尾 线条符号

此外,我们不能使用任何涉及字符串的东西,所以这几乎限制了我只使用c字符串

我的问题是,我目前一直在尝试将一个提取的字符字符串(分数)转换成一个整数数组,这样我就可以对它们进行数学运算

这是我目前的代码

int main()
{
    char * lastName;
    char * firstName;
    char * idNumber;
    char * testScores;
    char rawData[256] = "";
    ifstream fin;
    ofstream fout;

    fin.open("student_input.dat");

    if (!fin)
    {
        cout << "Program terminated. Input file did not open." << endl;
        fin.close();
        return 1;
    }

    if(fin)
    {
        for( int i = 0; i < 41; i++)
        {
            fin.getline(rawData, 256, '\n');
            lastName = strtok(rawData, " ");
            cout << lastName;
            firstName = strtok(NULL, " ");
            cout << firstName;
            idNumber = strtok(NULL, " ");
            cout << idNumber << "  ";
            for( int j = 0; j < 6; j++)
            {
                testScores = strtok(NULL, " ");
                cout << testScores << " ";
            }
            cout << endl;
        }
    }

    return 0;
}
intmain()
{
char*lastName;
char*名字;
字符*idNumber;
字符*测试分数;
char rawData[256]=“”;
流鳍;
流式流量计;
财务公开(“学生输入数据”);
如果(!fin)
{

如果我正确理解了这个问题,你可能有两个具体问题:

  • 如何从字符串中提取特定的数据值
  • 如何将包含数字字符的字符串转换为数字
  • 我给你两个解决方案(不张贴实际代码,因为这是一个(可能的)学校作业)

    解决方案1:

    1. Let input be a string or array of characters
    2. Read the entire line into input
    3. Set index = 0
    4. while (index is not beyond the end of input)
        4.1. Set start = (first non-whitespace character in input starting from index)
        4.2. Set end = (first whitespace character / end of line character / end of file in input starting from start)
        4.3. Set substring = (string containing the characters in input from indices start to end-1, both end inclusive)
        4.4. Save substring (into an array or whatever).
        4.5. Set index = end
    5. end-while
    
    这样,您就可以将所有以空格分隔的子字符串作为不同的字符串。根据需要处理它们(即,第一个子字符串是名字,第二个子字符串是姓氏,依此类推)

    找到包含分数的字符串后,可以从问题的注释中使用atoi,也可以编写一个函数,将包含单个数字的字符串转换为整数

    解决方案2:

    1. Declare three string (or array of chars) variables firstname, lastname and id
    2. Declare six integers naming grade1 to grade6
    3. Use the >> operator of ifstream to read into the values sequentially, that is, read to firstname, lastname, id, grade1,...grade6
    
    >>操作符应该负责处理字符串或整数问题,您也不必担心空格的数量

    如果您希望避免atoi(我不建议您避免),那么下面是一个将数字集合转换为数字的简单算法:

    1. Let input = array of characters / string containing only numerical letters.
    2. Set index = 0
    3. Set sum = 0
    4. While index is not beyond the last index at input
        4.1. Set c = the character at index
        4.2. Set value = ASCII value of c
        4.3. Set sum = sum * 10 + value;
        4.4. Increase index by 1
    5. End-while
    

    希望这有帮助:)

    您是否尝试过在for循环中访问CString的每个字符,并使用atoi()将它们转换为int,以便将它们保存到int数组中?我只是在编写代码的第二学期,讲师还没有解释过atoi(),我以前从未听说过它?您能否详细说明如何使用atoi()呢?当然:。如果你是一名学生,你应该知道最重要的是要知道它写在哪里。@TienNguyen了解你不知道的东西的一个好方法是谷歌或翻阅你的课本。99~95%的时间你会通过谷歌搜索找到你正在寻找的答案。