habe ich versucht, diesen Code auf Eclipse mit MinGW und der Online-C++ Editor: https://www.tutorialspoint.com/compile_cpp_online.phpWie konvertiert man in C++ eine Zeichenfolge Zeile für Zeile?
Ich weiß nicht, warum stoi nicht funktioniert? Ich versuche, eine Zeichenfolge Zeile für Zeile aus einem Textdokument in eine Ganzzahl zu konvertieren. Würde es ein Bibliotheks-Problem sein oder unterstützt MinGW es nicht? Ich habe es auch mit std :: und ohne es versucht (ich glaube wirklich nicht, dass ich es brauchte, da die std ein Namespace ist).
Die Fehler:
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)
Der Code:
#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;
}
Die Datei:
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
verwenden können Wenn das Problem nicht behoben wird, müssen Sie eine [mcve] bereitstellen. –
Sie sollten '-std = C++ 11' zu Ihren Compiler-Flags hinzufügen. Siehe [hier] (http://en.cppreference.com/w/cpp/string/basic_string/stol). –
@TopologicalSort _ "Stoi ist ein Mitglied von String" _ Nein! –