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

时间:2016-12-02 16:15:46

标签: c++

我在Eclipse上使用MinGW和在线c ++编辑器尝试了这个代码: 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 

1 个答案:

答案 0 :(得分:2)

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

-std=c++11启用c ++ 11功能

您需要这样做,因为stoi是c ++ 11的功能。

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

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