gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:35:55 25 4
gpt4 key购买 nike

我在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com