gpt4 book ai didi

C++程序不会进入for循环

转载 作者:行者123 更新时间:2023-11-28 02:18:18 25 4
gpt4 key购买 nike

当我尝试打印排序后的数据时,我无法让程序进入 for 循环。我该如何解决?这是为那些想要查看其余部分的人准备的完整代码。不起作用的 for 循环是 main 函数中的 for 循环。

#include<string> //provieds strings
#include<iostream> //providescin and cout
#include<algorithm> //provides sort()
#include<vector> // provides vector
#include<fstream> // provides file input and output

using namespace std;
string temp_file_name;
const int NUMBER_OF_RANKS = 13;
const int NUMBER_OF_SUITS = 4;
string ranks[NUMBER_OF_RANKS] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string suits[NUMBER_OF_SUITS] = { "Hearts", "Diamonds", "Spades", "Clubs" };


void GenerateTestData()
{
ofstream output_file;
output_file.open(temp_file_name);
for (int suit_index = 0; suit_index < NUMBER_OF_SUITS; suit_index++)
{
for (int rank_index = 0; rank_index < NUMBER_OF_RANKS; rank_index++)
{
output_file << ranks[rank_index] << "of" << suits[suit_index] << endl;
}
}
output_file.close();
}

vector<string> ReadTestDataBackIn()
{
ifstream input_file;
vector<string> cards;
string buffer;
input_file.open(temp_file_name);
while (getline(input_file, buffer))

{

cards.push_back(buffer);

}
input_file.close();
return cards;
}

int main()
{
vector<string> cards;

GenerateTestData();
cards = ReadTestDataBackIn();

sort(cards.begin(), cards.end());
//This loop
for (int card_index = 0; card_index < cards.size(); card_index++)
{
cout << cards[card_index] << endl;
}

system("pause");
return 0;
}

最佳答案

您的临时文件名字符串未定义,没有任何内容写入磁盘。这使得卡片 vector 为空。

关于C++程序不会进入for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381910/

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