I need to figure out how to declare variables but my professor is not explaining correctly and can barely understand her

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

// Funtion prototypes:
int getInput();
int findMin();
float findAverage();


int num; //Global Variable, visible in all functions

int main()
{
char choice;
int sum, min_digit;
float average;

do
{
num = getInput();
cout << "The integer entered by the user is " << num << endl;

min_digit = findMin();
cout << "Smallest Digit: " << min_digit << endl;
average = findAverage();
cout << "Average: " << average <<endl;
cout << "Would you like to Continue: ";
cin >> choice;

}while(choice != 'N' && choice != 'n');


return 0;
}
What exactly is the issue you're having?
1
2
3
4
5
int num;

char choice;
int sum, min_digit;
float average;

These are all valid variable declarations, although a global variable like 'num' is highly discouraged.

The code you have shown does not include the definitions for your functions, so it will not link properly as-is.

___________________

If your professor is not helping you learn, I would suggest just trying to learn C++ yourself. Get a good book, or search for online tutorials and such. Learn bit by bit, ask questions when something isn't clear.
Last edited on
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
toupper() requires casts for C++. Also the variables (other than choice) can be defined when they are initialised.

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
#include <iostream>
#include <iomanip>
#include <cctype>

int getInput();
int findMin();
double findAverage();

int main()
{
	char choice {};

	std::cout << std::fixed << std::setprecision(2);

	do {
		const auto num {getInput()};

		std::cout << "The integer entered by the user is " << num << '\n';

		const auto min_digit {findMin()};

		std::cout << "Smallest Digit: " << min_digit << '\n';

		const auto average {findAverage()};

		std::cout << "Average: " << average << '\n';

		std::cout << "Would you like to Continue (Y/N): ";
		std::cin >> choice;

	} while (static_cast<char>(std::toupper(static_cast<unsigned char>(choice))) != 'N');
}

how to declare variables

See tutorial: http://www.cplusplus.com/doc/tutorial/variables/
Topic archived. No new replies allowed.