We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,248 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sentinel & Count Help

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

2
Contributors
7
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 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;
}
Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 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;
}
HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 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:

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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

HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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.

HungMob
Newbie Poster
12 posts since Oct 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0837 seconds using 2.74MB