While loops with sentinels.

Hello all. I am trying to figure out what I did wrong. I keep coming to a brick wall. My professor wants us to use a while loop, and have 3 different people test it. One with 3 sets of numbers, one with two and one with one number. I am needing to figure out the average of each set that is put in. What am I doing wrong? It is used on Visual Studio.

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	ofstream outfile;			// define an output filestream
	outfile.open("a6_adams.txt");	// open the filestream and provide the file name

	int x = 1; // the amount of numbers put in
	int i; // the number put in
	int total = 0; // the numbers added together
	int average; // accumlator
	const int SENTINEL = 9999;

	cout << "This program will average a series of integers entered by the user." << endl;
	cout << "Enter 9999 to mark the end of the set of integers." << endl;
	cout << endl;

	cout << "Enter 9999 to mark the end of the set of integers." << endl;
	cin >> i; 

	while (i != 9999)
	{
		cout << "Enter 9999 to mark the end of the set of integers." << endl;
		total += i;
		x++;
		average = total / x;
		cin >> i;
	}

	cout << "The average of the values you entered is: " << average << endl;
	outfile << "The average of the values you entered is: "<< average << endl;
	outfile.close();


	return 0;
Last edited on
I guess you want to avoid adding your sentinel to your total (and hence your average).

Also, if there are no numbers at all, your average is garbage uninitialised data.
What am I doing wrong?
What is the problem you are facing?
Forget about file output for the moment.

double's instead of int's as shown are better.

So, this might help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>

using namespace std;

int main()
{
    int x = 0; // the number of numbers entered
    double i = 0.0; // the number put in
    
    double total = 0.0; // the numbers added together
    double average = 0.0; // accumlator
    
    const int SENTINEL = 9999;
    
    cout << "This program will average a series of integers entered by the user." << endl;
    cout << "Enter 9999 to mark the end of the set of integers." << endl;
    cout << endl;
    
    
    while (cin >> i && i != SENTINEL)
    {
        total += i;
        x++;
        cout << "Enter 9999 to mark the end of the set of integers." << endl;
    }
    average = total / x;
    cout << "The average of the values you entered is: " << average << endl;
    
    
    return 0;
}


This program will average a series of integers entered by the user.
Enter 9999 to mark the end of the set of integers.

1
Enter 9999 to mark the end of the set of integers.
3
Enter 9999 to mark the end of the set of integers.
3
Enter 9999 to mark the end of the set of integers.
9999
The average of the values you entered is: 2.33333
Program ended with exit code: 0
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
//#include<fstream>

using namespace std;

int main()
{
	//ofstream outfile;			// define an output filestream
	//outfile.open("a6_adams.txt");	// open the filestream and provide the file name

	int x = 0; // the amount of numbers put in     -> 0 <-
	int i; // the number put in
	int total = 0; // the numbers added together
	//int average; // accumlator
	//const int SENTINEL = 9999;

	cout << "This program will average a series of integers entered by the user." << endl;
	//cout << "Enter 9999 to mark the end of the set of integers." << endl;
	//cout << endl;

	cout << "Enter 9999 to mark the end of the set of integers." << endl;
	//cin >> i; 

	while(1)
	{
		//cout << "Enter 9999 to mark the end of the set of integers." << endl;
		cin>>i;
		if(i==9999) break;
		x++;
		total += i;        
	}

	if(x>0) cout << "The average of the values you entered is: " << double(total)/x << endl;
	//outfile << "The average of the values you entered is: "<< average << endl;
	//outfile.close();


	return 0;
}


This program will average a series of integers entered by the user.
Enter 9999 to mark the end of the set of integers.
1
2
9999
The average of the values you entered is: 1.5
Thanks all. She wants me to do it outside of the while statement. So it will post on the text page. Sorry to keep asking she is being super vague. This is what I have so far.

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
ofstream outfile; // define an output filestream
outfile.open("a6_adams.txt"); // open the filestream and provide the file name

int x = 0; // the amount of numbers put in
int i = 0; // the number put in
int total = 0; // the numbers added together
int average; // accumlator
const int SENTINEL = 9999;

cout << "This program will average a series of integers entered by the user." << endl;
outfile << "This program will average a series of integers entered by the user." << endl;
cout << "Enter 9999 to mark the end of the set of integers." << endl;
outfile << "Enter 9999 to mark the end of the set of integers." << endl;
cout << endl;
cout << "Enter 9999 to mark the end of the set of integers." << endl;
outfile << "Enter 9999 to mark the end of the set of integers." << endl;
cin >> i;

while (i != 9999)
{
cout << "Enter 9999 to mark the end of the set of integers." << endl;
outfile << "Enter 9999 to mark the end of the set of integers." << endl;
total += i;
x++;
cin >> i;
average = total / x;

}


cout << "The average of the values you entered is: " << average << endl;
outfile << "The average of the values you entered is: "<< average << endl;
outfile.close();


return 0;

She wants the text document to look like this:

This program will average a series of integers entered by the user.
Enter 9999 to mark the end of the set of integers.

Enter 9999 to mark the end of the set of integers.
2
Enter 9999 to mark the end of the set of integers.
3
Enter 9999 to mark the end of the set of integers.
4
Enter 9999 to mark the end of the set of integers.
9999
The average of the values you entered is: 3

This program will average a series of integers entered by the user.
Enter 9999 to mark the end of the set of integers.

Enter 9999 to mark the end of the set of integers.
12
Enter 9999 to mark the end of the set of integers.
67
Enter 9999 to mark the end of the set of integers.
9999
The average of the values you entered is: 39.5

This program will average a series of integers entered by the user.
Enter 9999 to mark the end of the set of integers.

Enter 9999 to mark the end of the set of integers.
9999
The average of the values you entered is: 0
She wants me to do it outside of the while statement.


She want what done outside of the while statement?

I think you have a couple of good suggestions above that seem to do what you want.

Why do you calculate average each time through the loop? You only need to calculate it at the end when you are printing it out.

You are using ints for all of your numbers. When you calculate average, you will lose the decimal places due to integer division.
Hello jadams0904,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



jadams0904 wrote:

She wants me to do it outside of the while statement.


A little confused here about what is wanted.

Trying to keep with what you started with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>  // <--- Added.
#include <fstream>
#include <string>   // <--- Added.

using namespace std;

int main()
{
    //ofstream outfile; // define an output filestream
    //outfile.open("a6_adams.txt"); // open the filestream and provide the file name

    std::string outFileName{ "a6_adams.txt" };

    std::ofstream outFile(outFileName);

    if (!outFile)
    {
	    std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;

        //std::cout << "\n File \"" << outFileName << "\" did not open" << std::endl;  // <--- If you can not use the "iomanip" header file.
        
        return 1;
    }

    const int SENTINEL = 9999;
    
    int numsEntered{}; // the amount of numbers put in
    int num{}; // the number put in
    int total{}; // the numbers added together
    double average{}; // accumlator

    std::cout << std::fixed << std::setprecision(3);
    outFile << std::fixed << std::setprecision(3);

    cout << "This program will average a series of integers entered by the user.\n";;
    outFile << "This program will average a series of integers entered by the user.\n";;

    cout << "\nEnter 9999 to mark the end of the set of integers.: ";
    outFile << "\nEnter 9999 to mark the end of the set of integers.: ";

    //cout << "Enter 9999 to mark the end of the set of integers.\n";;  // <--- Dulplicate.
    //outFile << "Enter 9999 to mark the end of the set of integers.\n";;
    cin >> num;

    //std::cout << i;
    outFile << num << '\n';
        
    while (num != SENTINEL)
    {
        total += num;
        numsEntered++;

        cout << "\nEnter 9999 to mark the end of the set of integers.: ";;
        outFile << "\nEnter 9999 to mark the end of the set of integers.: ";;
        cin >> num;

        outFile << num << '\n';

        average = static_cast<double>(total) / numsEntered;  // <--- Should be outside the while loop.
    }


    cout << "\nThe average of the values you entered is: " << average << endl;

    outFile << "\nThe average of the values you entered is: " << average << endl;

    //outFile.close();  // <--- Not needed as the file stream will close when the function looses scope.

    return 0;  // <--- Not required, but makes a good break point.
}

Not sure what you have learned so far, but you can do without the "iomanip" header file. The "string" header file is useful for some of the "cout" and "outFile' statements.

When you open a file for input or output it is a very good idea to check if it is open. A little harder to have an output file stream fail, but it can happen. I found this code helpful when I was first learning to work with files.

Personal choice, but I like putting variables defined as "constant" at the top where they are easy to find and change.

In your program you defined "SENTINEL", but never used it. Did you forget about it?

I made "average" a "double" for later use so that you can output something like "39.5".

You did not initialize the variable "average" when you defined it and this caused a problem when you enter "9999" as the first number and "average" has no value to print after the while loop, but a garbage value. This caused a run time error for me.

Always a good idea to initialize your variables when they are defined. If for no other reason than to know that they do not contain a garbage value waiting to cause a problem later.

Line 49. You defined "SENTINEL" here is where you use it.

The comments in the code should explain the rest.

The output I received from the program, with the changes I made is:

If the output file did not open:

 File "C:/Sub Directory/a6_adams.txt" did not open



From the "cout" statements:

This program will average a series of integers entered by the user.

Enter 9999 to mark the end of the set of integers.: 2

Enter 9999 to mark the end of the set of integers.: 3

Enter 9999 to mark the end of the set of integers.: 4

Enter 9999 to mark the end of the set of integers.: 9999

The average of the values you entered is: 3.000



From the output File:

This program will average a series of integers entered by the user.

Enter 9999 to mark the end of the set of integers.: 2

Enter 9999 to mark the end of the set of integers.: 3

Enter 9999 to mark the end of the set of integers.: 4

Enter 9999 to mark the end of the set of integers.: 9999

The average of the values you entered is: 3.000


Unless you are stuck with making your program match the output given it does not take that much work to make it look a little nicer.

Since you neglected to post the actual assignment people can only guess at what you need to do.

Andy
Using a while loop, the code should be like this (excluding output to file):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int main()
{
	const int SENTINEL = 9999;

	int x = 0;		// the number of numbers entered
	double i = 0.0;		// the number put in
	double total = 0.0;	// the numbers added together

	cout << "This program will average a series of integers entered by the user.\n\n";

	while ((cout << "Enter 9999 to mark the end of the set of integers: ") && (cin >> i) && (i != SENTINEL))
	{
		total += i;
		++x;
	}

	cout << "The average of the values you entered is: " << total / x << endl;
}


If your professor wants/expects this to be coded differently (apart from output appearance), then you aren't being taught 'good practice' and potentially 'bad habits'.

If you want the output also to a file, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	const int SENTINEL = 9999;

	const char* const outFileName = "a6_adams.txt";
	const char* const desc = "This program will average a series of integers entered by the user.\n\n";
	const char* const prmpt = "Enter 9999 to mark the end of the set of integers: ";
	const char* const avrg = "The average of the values you entered is: ";

	ofstream outFile(outFileName);

	if (!outFile.is_open()) {
		std::cout << "\nFile \"" << outFileName << "\" did not open." << std::endl;
		return 1;
	}

	int x = 0;		// the number of numbers entered
	double i = 0.0;		// the number put in
	double total = 0.0;	// the numbers added together

	cout << desc;
	outFile << desc;

	while ((cout << prmpt) && (outFile << prmpt) && (cin >> i) && (outFile << i << '\n') && (i != SENTINEL))
	{
		total += i;
		++x;
	}

	cout << avrg << total / x << endl;
	outFile << avrg << total / x << endl;
}



If your professor doesn't 'like' this type of solution, I'd love to see hers - so I can criticise it!
Last edited on
As soon as she gets back to me I will let you all know. She said she hasn't thought this class in 10 years. Thank you all for the input.
Topic archived. No new replies allowed.