Sam Safary Sam Safary - 1 month ago 14
C++ Question

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.


  • Number of integers per line before 0 (int count).

  • Number of all integers in data file (int totalcount).

  • Number of lines in data file (int lines).



Two examples of unknown inputs from a data file:


  • Example One:

    1 2 3 0 4 5 6 7 0

  • Example Two:

    0 9 11 -11

    1 1 0 0 2

    0



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......7

2......0......7


Correct expected outputs:


1......3......7

2......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.

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.

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:

Currently attempting to use a "temp" variable to substract from "totalcount". I will update my code if attempt is successful.

Answer

totalcount is sum of counts. 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++;
            }
            //Counts how many lines there are in a data file using sentinel 0 (not "/n").

        }
    return 0;
}