[英]cout not printing after reading files
For some reason my cout isn't working correctly.出于某种原因,我的 cout 无法正常工作。 I think it has something to do with the buffer needing to be flushed.
我认为这与需要刷新的缓冲区有关。 I tried to add a
cout << flush
after reading the files but that didn't fix it.我尝试在阅读文件后添加
cout << flush
,但这并没有解决。 The only place I print is main()
in the for loop.我打印的唯一地方是 for 循环中的
main()
。 This is the output I get from those prints.这是我从这些打印中得到的输出。
Aeberg
Aaren
Aaron
Full Name
Aaron
Before and after Aaron, there is a space. Aaron 前后,有一个空格。 So only the last name prints.
所以只打印姓氏。
#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]];
}
I would suggest to improve your program a little.我建议稍微改进你的程序。
init
, one is enough.init
,一个就足够了。 Simply pass the file name parameter.string first[SIZE]
- this will cause stack overflow.string first[SIZE]
- 这会导致堆栈溢出。 Use vectors instead.int random[left];
int random[left];
random
array at allrandom
数组#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.