Hello frueda75,
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.
|
When I first started with C++ I found this to be a good place to start:
https://www.learncpp.com/
Later I found it to be a good place for reference.
As
Ganado said global variables that do not start with "const" or "constexpr" should be avoided. Now it may seem easy, but in the future when you spend several hours or a day trying to figure out where your global variable got a wrong value you will understand.
It is best to post a complete program, or at least enough to demonstrate the problem, that can be compiled and tested. Also when an input file is needed you should post what you are using. Or at least a good sample and if the input file is a problem the part that is causing the problem.
These days "double" is preferred over "float". Unless there is a really good reason for the "float".
Prefer to use the new line (\n) over the function "endl". Something that works to your advantage is any number of "cout" statements followed by
std::cin >> something;
. The "cin" will flush the buffer of all output before any input is taken. Unfortunately this does not work in reverse.
This is your code fixed up a little with some suggestions. See what you think.
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
|
#include <iostream>
#include <iomanip>
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
// <--- None of these are used in what is here. Should you need any put them back.
// No need to include something that you are not using.
//#include <ctime>
//#include <cstdlib>
//#include <algorithm>
using namespace std; // <--- Best not to use.
// Funtion prototypes:
int getInput();
int findMin();
double findAverage();
//int num; //Global Variable, visible in all functions. Changeable by any line of code that follows.
int main()
{
char choice{}; // <--- ALWAYS initialize your variables.
int num{}, sum{}, min_digit{};
double average{};
std::cout << std::fixed << /*std::showpoint <<*/ std::setprecision(2); // <--- "showpoint" optional if "setprecision" is > 0.
do
{
num = getInput();
cout << "The integer entered by the user is " << num << '\n';
min_digit = findMin();
cout << "Smallest Digit: " << min_digit << '\n';
//cout << "Smallest Digit: " << findMin() << '\n'; // <--- A possible alternative.
average = findAverage();
cout << "Average: " << average << '\n';
cout << "Would you like to Continue (Y/N): ";
cin >> choice;
} while (std::toupper(choice) != 'N' /*&& choice != 'n'*/);
return 0; // <--- Not required, but makes a good break point.
}
|
Andy