1,105,334 Community Members

Sentinel & Count Help

Member Avatar
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello again! I was given an assignment the other day in C++ and I was suppose to use the counter and add a sentinel so the program knew when to stop.

Instructors directions:
"The file for this assignment reads a sequence of positive integers from a file (2 3 5 7 9 10 22 54 66) and prints them out to the screen together with their sum. Create the text file that can be used to input these integers and add to the program a set of instructions that provides the number of integers in the sequence. You are to assume you do not know the number of integers in the sequence."

I have been able to get everything to work except 2 things. I can't get the "2" to print out and the sentinel, "0", has been printed out. I need help with having the "2" print out and for the sentinel, "0", not to.

Here is my code:

using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
int main()
{
    int count = 1;
    int sentinel = 0;   
    double The_Sum;
    double Positive_Integers;
    
    ifstream datain ("Input.txt");
    
    The_Sum = 0;
    
    cout<<"    |   Positive Integers \n"<<endl;
    cout<<"----|--------------------\n"<<endl;
    
    datain >> Positive_Integers;
    
    while (Positive_Integers != sentinel)
    
    {
    
    for (count = 1; count<=Positive_Integers; count ++)
    
    {
          datain>>Positive_Integers;
          cout<<count<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
          The_Sum = The_Sum + Positive_Integers;
    }
    
    }
        
    cout<<endl<<"The Sum = "<<The_Sum<<endl<<endl;
    
    system ("pause");
    return 0;
}

Here is my text from the file:

TEXT FILE:
2
3
5
7
9
10
22
54
66
0

Thanks again guys. So far I'm enjoying this website. :)

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

The first thing your inner loop does is read from the file into Positive_Integers . However, the first number was already read to determine if it was a sentinel in the outer loop. So you're basically discarding the first number. The next number should be read after the current number is displayed and added to the total.

However, your inner loop isn't meaningful (in fact, it can produce erroneous output depending on the contents of the file). It can be removed entirely in favor of a single loop like so:

datain >> Positive_Integers;

while (Positive_Integers != sentinel)
{
    cout<<count<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
    The_Sum = The_Sum + Positive_Integers;
    datain>>Positive_Integers;
}
Member Avatar
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok thanks, I understand now why the 2 got deleted and why it printed the sentinel, but now I don't have a count for the number of items.

Example of what I'm asking for:

|   Positive Integers
----|--------------------
1   |                   2
2   |                   3
3   |                   5
4   |                   7
5   |                   9
6   |                  10
7   |                  22
8   |                  54
9   |                  66
Member Avatar
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok nevermind. I forgot that I needed to add this:

for (count = 1; count<=Positive_Integers; count ++)
{
}

So my final code looks like this:

using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
int main()
{
    int count = 1;
    int sentinel = 0;   
    double The_Sum;
    double Positive_Integers;
    
    ifstream datain ("Input.txt");
    
    The_Sum = 0;
    
    cout<<"    |   Positive Integers \n"<<endl;
    cout<<"----|--------------------\n"<<endl;
    
    datain >> Positive_Integers;

    while (Positive_Integers != sentinel)
    {
          for (count = 1; count<=Positive_Integers; count ++)
          {
          cout<<count<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
          The_Sum = The_Sum + Positive_Integers;
          datain>>Positive_Integers;
          }
    }
        
    cout<<endl<<"The Sum = "<<The_Sum<<endl<<endl;
    
    system ("pause");
    return 0;
}
Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

Increment count in the loop:

while (Positive_Integers != sentinel) {
    cout<<count++<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
    The_Sum = The_Sum + Positive_Integers;
    datain>>Positive_Integers;
}

Ok nevermind. I forgot that I needed to add this:

You've re-added the bug I removed. :icon_rolleyes:

Member Avatar
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh wow that was so much simpler than what I was doing. Thanks!

You've re-added the bug I removed.

I'm not really sure why I re-added it. lol

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

I'm not really sure why I re-added it.

I suspect because you didn't fully understand my first post before running off and playing with your code. I wouldn't recommend mindless copy/paste of code you find on the web.

Member Avatar
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I suspect because you didn't fully understand my first post before running off and playing with your code. I wouldn't recommend mindless copy/paste of code you find on the web.

Well I read your post, but when I was trying to figure out what was different from the one you did and mine I completely forgot that you said to get rid of the for loop.

I wouldn't recommend mindless copy/paste of code you find on the web.

I try not to do this.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: