甚至是文件中的每个第十个数字,甚至是奇数
is every tenth number in file even or odd
我需要在文件中找到每个第10个数字,并定义数字是否偶数或奇数
我只记得如何读取文件,不知道下一步该怎么做
string linia;
fstream plik;
plik.open("przyklad.txt", ios::in);
if(plik.good() == true)
{
while(!plik.eof())
{
getline(plik, linia);
cout << linia << endl;
}
plik.close();
}
system("PAUSE");
return(0);
由于每个人都对这个问题是如此负面,所以我要继续回答。我们不能确定OP是从正确来源学习的,正如他所说,他不记得下一步该怎么做,这意味着他真的没有选择。
带有一些解释评论的工作代码如下:
#include <iostream>
#include <fstream>
using namespace std;
// Returns wether the number is odd
bool isOdd(const int num){
return num % 2 != 0;
}
int main(){
// Open the file in input mode
ifstream inputFile("data.txt");
int count(0); // Represents the current line number we are checking
string line; // Represents the characters contained in the line we are checking
// The loop goes over ever single line
while(getline(inputFile, line)){
//Increase the line count by 1
++count;
//If the line we are on is either 0 (not possible due to instant increment), 10, 20, 30, ...
if(count % 10 == 0){
//Get the number (not safe since it's not guaranteed to be a number, however it's beyond the scope of the question)
int number = stoi(line);
//Ternary statement can be replaced with a simple if/else
cout << "The number " << number << " is " << (isOdd(number) ? "odd" : "even") << 'n';
}
}
return 0;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 如何传递多个 std::文件系统选项?
- C++通过数字比较两个数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 在 txt 文件中显示前两个数字的程序
- 甚至是文件中的每个第十个数字,甚至是奇数
- 如何从文件中获取数字行并忽略第一个数字
- 超过两个数字十六进制
- 按每行同一位置出现的 4 个数字对文本文件中的 200 行进行排序
- 如何从C++文件中读取第一个数字
- 大型输入文本文件,每行有两个数字,如何更快地输入
- 计算文本文件每行的第一个数字
- 将数字文件读入数组,同时每1026个条目跳过前两个值
- 如何存储数十亿个JSON文件并对其进行查询
- 我将如何操作导入的 .text 文件以在屏幕上仅显示员工 SNN 的最后四个数字?