[英]C++ Basic While Loop: Unknown Inputs and Value Increment
So I'm trying to write a program that reads unknown inputs from a data file that has a sentinel 0 or I guess an termination point for the loop. 所以我正在尝试编写一个程序,从具有标记0的数据文件中读取未知输入,或者我猜测循环的终止点。
Two examples of unknown inputs from a data file: 来自数据文件的两个未知输入示例:
Here is my program (without "count" because that is where my problem lies): 这是我的程序(没有“计数”,因为这是我的问题所在):
int main()
{
//Declaring variables.
int input, lines, count, totalcount, datafile;
lines = 0;
count = 0;
totalcount = 0;
//Loop runs as long as data file has an integer to take in.
while(cin >> datafile)
{
//If datafile is not 0, loop runs until datafile is 0.
while(datafile != 0)
{
//Counts all integers in the file except 0.
totalcount += 1;
cin >> datafile;
}
//Counts how many lines there are in a data file using sentinel 0 (not "/n").
if(datafile == 0)
lines += 1;
//Outputs.
cout << lines << setw(11) << count << setw(11) << totalcount << endl;
}
return 0;
}
Please do not worry about technicality, efficiency, or anything else besides the logic/concept itself as I'm just trying to find the missing link in my knowledge to complete this assignment. 除了逻辑/概念本身之外,请不要担心技术性,效率或其他任何问题,因为我只是想在我的知识中找到缺失的链接来完成这项任务。
With that said, my expected outputs are as formatted: 话虽如此,我的预期输出格式如下:
"Line #" "Counts of integers per line" "Total counts of all integers in data file" “行#”“每行整数计数”“数据文件中所有整数的总计”
Using example one with my current code, I would have outputs (spacing is not exact and '.' is for blanks): 使用我当前代码的示例一 ,我会有输出(间距不精确,'。'代表空白):
1......0......3
1 ...... 0 ...... 3
2......0......72 ...... 0 ...... 7
Correct expected outputs: 正确的预期产出:
1......3......3
1 ...... 3 ...... 3
2......4......72 ...... 4 ...... 7
I would like any hints or explanation of how I can count the integers per line (before sentinel 0) and assign that value to "int count" without the value persisting to the next line. 我想要任何提示或解释我如何计算每行的整数(在sentinel 0之前)并将该值赋给“int count”而不将值保持到下一行。
I'm a student in an introductory C++ class so please show me a basic way of how I may go about this first and then any other advanced options as necessary for future applications. 我是一名入门C ++课程的学生,所以请向我展示一个基本的方法,我将如何首先解决这个问题,然后根据未来的应用需要提供其他任何高级选项。
Code of Conduct Personal Statement: 行为准则个人陈述:
By participating, you are providing necessary knowledge for assignment completion, not completing the assignment itself. 通过参与,您将为完成作业提供必要的知识,而不是完成作业本身。 The example used is generated by me intended for concept demonstration purposes and is only a small part of the final assignment.
我使用的示例是出于概念演示目的而生成的,并且只是最终作业的一小部分。
10/23/2016 9:56PM Update 1: 10/23/2016 9:56 PM更新1:
Currently attempting to use a "temp" variable to substract from "totalcount". 目前正试图使用“temp”变量从“totalcount”中减去。 I will update my code if attempt is successful.
如果尝试成功,我将更新我的代码。
totalcount is sum of counts. totalcount是计数的总和。 This is my suggestion
这是我的建议
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Declaring variables.
int input, lines, count, totalcount, datafile;
lines = 0;
count = 0;
totalcount = 0;
//Loop runs as long as data file has an integer to take in.
while(cin >> datafile)
{
// Add count to totalcount and reset count each time reach 0
if (datafile == 0) {
totalcount += count;
lines += 1;
cout << lines << setw(11) << count << setw(11) << totalcount << endl;
count = 0;
}
//otherwise increase count
else {
count++;
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.