[英]cout not printing after reading files
出於某種原因,我的 cout 無法正常工作。 我認為這與需要刷新的緩沖區有關。 我嘗試在閱讀文件后添加cout << flush
,但這並沒有解決。 我打印的唯一地方是 for 循環中的main()
。 這是我從這些打印中得到的輸出。
Aeberg
Aaren
Aaron
Full Name
Aaron
Aaron 前后,有一個空格。 所以只打印姓氏。
#include <iostream>
#include <fstream>
#include <queue>
#include <time.h>
using namespace std;
const int SIZE = 50000;
void initFirst(string first[]);
void initMiddle(string middle[]);
void initLast(string last[]);
int main()
{
srand(time(NULL));
ifstream in;
ofstream out;
string first[SIZE];
string middle[SIZE];
string last[SIZE];
initFirst(first);
initMiddle(middle);
initLast(last);
string x;
for(int i = 0; i < 1; i++)
{
cout << first[i] << endl;
cout << middle[i] << endl;
cout << last[i] << endl;
cout << "Full Name" << endl;
cout << first[i] << " " << middle[i] << " " << last[i] << endl; // Isn't printing correctly here
}
return 1;
}
void
initFirst(string first[])
{
ifstream file("names.txt");
string line;
int counter = 0;
if(file.is_open())
{
while(getline(file, line))
{
first[counter] = line;
counter++;
}
file.close();
}
int left = SIZE - counter;
int random[left];
for(int i = 0; i < left; i++)
random[i] = rand() % counter;
for(int i = counter; i < SIZE; i++)
first[i] = first[random[i-counter]];
}
void
initMiddle(string middle[])
{
ifstream file("first-names.txt");
string line;
int counter = 0;
if(file.is_open())
{
while(getline(file, line))
{
middle[counter] = line;
counter++;
}
file.close();
}
int left = SIZE - counter;
int random[left];
for(int i = 0; i < left; i++)
random[i] = rand() % counter;
for(int i = counter; i < SIZE; i++)
middle[i] = middle[random[i-counter]];
}
void
initLast(string last[])
{
ifstream file("middle-names.txt");
string line;
int counter = 0;
if(file.is_open())
{
while(getline(file, line))
{
last[counter] = line;
counter++;
}
file.close();
}
int left = SIZE - counter;
int random[left];
for(int i = 0; i < left; i++)
random[i] = rand() % counter;
for(int i = counter; i < SIZE; i++)
last[i] = last[random[i-counter]];
}
我建議稍微改進你的程序。
init
,一個就足夠了。 只需傳遞文件名參數。string first[SIZE]
- 這會導致堆棧溢出。 改用向量。int random[left];
random
數組#include <cstdint>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <time.h>
const size_t SIZE = 50000;
using strings = std::vector<std::string>; // 'array' of strings with dynamic size
void initFromFile(const std::string &fname, strings &arr)
{
std::ifstream file(fname);
std::string line;
while(getline(file, line))
arr.push_back(line);
size_t n = arr.size();
if(n == 0)
return;
for(auto i = 0; i < SIZE-n; i++)
arr.push_back(arr[rand() % n]);
}
int main()
{
using std::cout;
using std::endl;
srand(time(NULL));
strings first, middle, last;
initFromFile("names.txt", first);
initFromFile("first-names.txt", middle);
initFromFile("middle-names.txt", last);
for(auto i = 0U; i < first.size(); i++)
{
cout << first[i] << endl << middle[i] << endl << last[i] << endl;
cout << "Full Name" << endl;
cout << first[i] << " " << middle[i] << " " << last[i] << endl;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.