在将我写的内容输出到文件时遇到麻烦
having trouble outputting what i wrote to a file
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout1;
ofstream fout2;
string fnameOdd;
string fnameEven;
int x;
int numEven(0);
int numOdd(0);
cout << "Enter name of file for odd integers: ";
getline(cin, fnameOdd);
fout1.open(fnameOdd.c_str(), ios::out);
cout << "Enter name of file for even integers: ";
getline(cin, fnameEven);
fout2.open(fnameEven.c_str(), ios::out);
if(!fout1.is_open())
{
cerr << "Unable to open file" << fnameOdd << endl;
exit(10);
}
if(!fout2.is_open())
{
cerr << "Unable to open file" << fnameEven << endl;
exit(15);
}
cout << "Enter list of odd and even integers (followed by 0): " << endl;
cin >> x;
while (x != 0)
{
if (x % 2 == 0)
{
numEven++;
}
else
{
numOdd++;
}
}
fout1 << numEven;
fout2 << numOdd;
cout << "File " << fnameOdd << " contains " << numOdd << " odd integers. " <<endl;
cout << "File " << fnameEven << " contains " << numEven << " even integers. " <<endl;
fout1.close();
fout2.close();
return 0;
}
我有麻烦输出任何东西到屏幕,什么都没有发生,它只是输入文件名和整数。我不确定如何输出我所写的文件,阅读我的书没有帮助。
您忘记了您的输入语句cin >> x;
也需要进入循环
cin >> x;
while (x != 0)
{
if (x % 2 == 0)
{
numEven++;
}
else
{
numOdd++;
}
cin >> x; // new line here
}
在输入x的第一个值之后,它不会再改变它的值。所以while循环永远不会结束。这就是为什么您没有看到任何输出。
相关文章:
- 如何从文本文件中读取数值,直到遇到字符类型?
- 创建 Spdlog 异步文件记录器时遇到困难
- 我在使用C++文件时遇到了一些问题
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我遇到使用 c++ 逐行读取输入文件的问题
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 在C++中打开 json 文件时遇到问题
- C++读取文件时遇到困难
- 我在从.txt文件中加起来时遇到了一些问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 在C++中使用 Visual Studio 读取文本文件时遇到问题
- 使用 libzip 打开 zip 文件时遇到问题
- 我在编写从文本文件中读取数据并按升序打印的代码时遇到问题
- 当所有内容都在一个文件中时,为什么会遇到链接器错误
- 当文件遇到特定字符时,如何停止读取文件
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- C++处理文件,在制作程序时遇到麻烦