Я попробовал этот код на Eclipse, с MinGW и онлайн-редактор C++: https://www.tutorialspoint.com/compile_cpp_online.phpКак преобразовать строку в целое число по строке в C++?
Я не знаю, почему стои не работает? Я пытаюсь преобразовать строку в целое число за строкой из текстового документа. Будет ли это проблема с библиотекой или 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
Если это не исправить, вам необходимо предоставить [mcve]. –
Вы должны добавить '-std = C++ 11' в свои флагов компилятора. См. [Здесь] (http://en.cppreference.com/w/cpp/string/basic_string/stol). –
@TopologicalSort _ "stoi является членом строки" _ Nope! –