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