c++ - 如何在C++中逐行将字符串转换为整数?

标签 c++

我在使用 MinGW 和在线 C++ 编辑器的 Eclipse 上尝试了这段代码: https://www.tutorialspoint.com/compile_cpp_online.php

不知道为什么stoi不行?我正在尝试将文本文档中的字符串逐行转换为整数。是库包含问题还是 MinGW 不支持它?我也尝试过使用 std::和不使用它(我真的不认为我需要它,因为 std 是一个命名空间)。

错误:

08:54:40 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o test.o "..\\test.cpp" 
..\test.cpp: In function 'int main()':
..\test.cpp:30:51: error: 'stoi' was not declared in this scope
                    questionsAmt =  stoi(array[0]);
                                                ^
..\test.cpp:67:48: error: 'stoi' is not a member of 'std'
                    quizpoints[QUESTIONNUM] = std::stoi(array[loop]);
                                                ^
..\test.cpp:87:41: error: 'stoi' is not a member of 'std'
                    AnswerArrayCount = std::stoi(array[loop]);
                                        ^

08:54:41 Build Finished (took 903ms)

代码:

 #include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;
 int main ()
 {
     int MAXSIZE = 1024;
     string array[MAXSIZE]; // creates array to hold names
     string line; //this will contain the data read from the file
     int questionsAmt;
     int loop=0; //short for loop for input
     int QUESTIONNUM = 0; //Question number to keep track in array.
     int isMC = 0; //Question number to keep track in array.
     int AnswerArrayCount = 0; //Question number to keep track in array.
     string AnswerArray[MAXSIZE]; //Question number to keep track in array.

     int isTF = 0; //Question number to keep track in array.
     int quizpoints[MAXSIZE]; //this will contain the data read from the file
     string quizquestions[MAXSIZE]; //this will contain the data read from the file
     string quizanswer[MAXSIZE]; //this will contain the data read from the file

     ifstream myfile ("testquestions.txt"); //opening the file.
     if (myfile.is_open()) //if the file is open
     {
         while (! myfile.eof() ) //while the end of file is NOT reached
         {
             getline (myfile,line); //get one line from the file
             array[loop] = line;//convert get whole line into an array string
                 questionsAmt =  stoi(array[0]);

             for (int i = 0; i < questionsAmt; i++)
             if(line=="TF"){
                 QUESTIONNUM++; //question 1,2,3,
                 isTF = 1;//setup next loop getline
                 loop++;
             }
             if(isTF == 1){
                 //points
                 quizpoints[QUESTIONNUM] = stoi(array[loop]);
                 isTF = 2;//setup next loop getline
                 loop++;
             }
             if(isTF == 2){
                 //TF question
                 quizquestions[QUESTIONNUM] = array[loop];
                 QUESTIONNUM++; //question 1,2,3,
                 isTF = 3;//setup next loop getline

                 loop++;
             }
             if(isTF == 3){
                 //TF answer
                  quizanswer[QUESTIONNUM] = array[loop];
                  QUESTIONNUM++; //question 1,2,3,
                 loop++;
             }


             if(line=="MC"){
                 QUESTIONNUM++; //question 1,2,3,
                 isMC = 1;//setup next loop getline
                 loop++;
             }
             if(isMC == 1){
                 //points
                 quizpoints[QUESTIONNUM] = std::stoi(array[loop]);

                cout<<quizquestions[QUESTIONNUM]<<endl;

                 isMC = 2;//setup next loop getline
                 loop++;
             }
             if(isMC == 2){
                 //MC question
                 quizquestions[QUESTIONNUM] = array[loop];

                cout<<quizquestions[QUESTIONNUM]<<endl;

                 QUESTIONNUM++; //question 1,2,3,
                 isMC = 3;//setup next loop getline

                 loop++;
             }
             if(isMC == 3){
                 //MC answer string array
                 AnswerArrayCount = std::stoi(array[loop]);
                //convert to int
                 string AnswerArray[AnswerArrayCount] = array[loop];
                //Question number to keep track in array.

                cout<<AnswerArray[AnswerArrayCount]<<endl;

                 AnswerArrayCount = AnswerArrayCount - 1 ;
                //Question number to keep track in array.
                 loop++;
             }
             if(AnswerArrayCount >= 1){
                 //MC answer string array
                 AnswerArray[AnswerArrayCount]  = array[loop];
                //Question number to keep track in array.

                cout<<AnswerArray[AnswerArrayCount]<<endl;

                 AnswerArrayCount = AnswerArrayCount - 1 ;
                //Question number to keep track in array.
                 loop++;
             }


            // loop++;
         }
         //cout << array[0] << endl; //and output it
         //int questionsAmt =  array[0]; //and output it

         myfile.close(); //closing the file
     }
     else
         {
         cout << "Unable to open file"; //if the file is not open output

    // system("PAUSE");
         }
     return 0;

 }

文件:

13
TF
5
There exist birds that cannot fly?
true
MC
10
Who was the President of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
TF
10
The city of Boston hosted the 2004 Summer Olympics?
false 

最佳答案

继续项目选项 -> 编译选项并将此 g++ -std=c++11 -o main *.cpp 作为编译命令。

-std=c++11 启用 c++11 特性

您需要这样做,因为 stoi 是一个 c++ 11 函数。

在文档的这一页上: http://www.cplusplus.com/reference/string/stoi/

您可以在页面顶部看到一个小警告图标(黄色标志)。 那就是说你只能在 c++11 上使用它

关于c++ - 如何在C++中逐行将字符串转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40936574/

相关文章:

类中的c++模板

c++ - MySQL createStatement() 内存泄漏

c++ - 使用 -std=gnu++11 时发现了哪些已知的性能差异

c++ - 使用模板类的函数重载

c++ - [C++][SFML 2.2] 奇怪的性能问题 - 移动鼠标会降低 CPU 使用率

c++ - 用于将 void 指针转换为指向类型的指针的操作的术语是什么?

c++ - 对结构进行索引是否合法?

Windows 上 Linux 的 C++ 开发

c++ - Qt 根据参数类型触发不同的信号

c++ - 替换字符串中的重复项