Hello WillliamTries2Prog,
I agree with the
TheIdeasMan you need much better variable names. I have changed a couple of names, but the rest are hard to follow.
I have looked back over your posts and did not find anywhere where you mentioned what IDE and compiler you are using. The IDE is more important. Please mention this.
While testing the program I found:
void getSIWSmallestTUDCWilliamG(int smallestIntWG, int smallestTudWG);
, but looking after "main" I find:
1 2 3 4 5
|
void getSIWSmallestTUDCWilliamG(int smallestIntWG, int smallestTudWG)
// And
int getSIWSmallestTUDCWilliamG(int* arrayWG, int sizeWG)
|
The return values here help make a difference, but it is the 1st parameter that makes them different and the compiler accepts this without any problem. You do need to forward declare the 2nd function and also call it with the proper parameters, which you are not doing.
Inside the function:
int getSIWSmallestTUDCWilliamG(int* arrayWG, int sizeWG)
This line of code:
tempWG % 10;
does nothing. Did you mean
tempWG %= 10;
, but then when you get to:
tempWG /= 10;
, but since "tempWG", say having a value of 5, now has a single digit and dividing by 10 would leave you with "0.5". But this is integer division the ".5" is dropped and only the (0)zero is stored. This can be a problem.
I have yet to fully understand what the for loop and if statement is trying to do.
AS I gain more understand of your code the function
getIntUDCWilliamG
is called 1st, which makes me wonder if
int getSIWSmallestTUDCWilliamG(int* arrayWG, int sizeWG)
is even needed.
Still have some work to do, but these are some of the changes I made. Some may seem small, but they are very helpful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std; // <--- Best not to use.
//Function Prototypes
void getSIWSmallestTUDCWilliamG(int smallestIntWG, int smallestTudWG);
int getSIWSmallestTUDCWilliamG(int* arrayWG, int sizeWG); // <--- Added.
int getIntUDCWilliamG(int integerWG);
void displayClassInfoWilliamG(void); // <--- 2nd "void" not required. This is old C.
void runHW2(void); // <--- 2nd "void" not required. This is old C.
//Application Driver
int main()
{
runHW2();
return 0; // <--- Not required, but makes a good break point for testing.
}
|
Line 16 is to demonstrate how a blank line makes the code more readable and easier to follow.
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
|
void runHW2()
{
constexpr int MAXSIZE{ 1 };
int choice{}; // <--- ALWAYS initialize all your variables.
int arrSize{}; // <--- Changed.
int* arrayWG{ nullptr };
int tempTudWG{ 10 };
int tempWG = 0;
int tudWG = 0;
int smallestIntWG = 0;
int smallestTudWG = 10;
int tinyTudWG = 10;
int loopTimes{}; // <--- Used for testing. Comment or remove when finished.
/*displayClassInfoWilliamG();*/ // <--- This is a function call to a function that does not exist.
do
{
cout <<
"\n"
"*******************************************\n"
"* MENU - HW #2 *\n"
"* (1) Calling getSIWSmallestTUDCWilliamG *\n"
"* (2) Quit *\n"
"*******************************************\n"
" Enter choice: ";
cin >> choice; // <--- The "cin" ALWAYS needs a prompt. See above.
switch (choice)
|
The concept of line 3 is very useful. You need to get use to this. AS you will see it makes changing your code very easy.
The "cout" statement may look like 7 different strings, but to the IDE and compiler it sees just 1 string of 237 characters +1 for the (\0) to mark the end of the string, or const char*, as the IDE considers it.
So you just 1 "cout" for 1 string and 1 (;) to mark the end of the line.
There are 2 advantages to this. First it looks more like what will be printed to the screen. Second it is much easier to change or edit.
Prefer to use the new line, (\n), over the function "endl" as much as possible.
The last advantage is when a "cout" is followed by a "cin" the "cin" will cause the buffer to be flushed of anything, output, that is left before it takes any input.
In your switch case 1 is way to big. Much of what is there can be put into small functions that can be called when needed. The switch should just direct the program flow not be the program.
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
|
switch (choice)
{
case 1:
{
cout << "\nCalling getSIWSmallestTUDCWilliamG\n\n";
// << "What is the size of the array?: ";
//cin >> arrSize;
while
(std::cout << "What is the size of the array? (Must be >= " << MAXSIZE << "): " && !(std::cin >> arrSize) || arrSize < MAXSIZE)
{
if (!std::cin)
{
std::cout << "\n Invalid entry! Must be a number.\n\n";
std::cin.clear();
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cin.ignore(1000, '\n');
}
else if (arrSize < MAXSIZE)
{
cout <<
"\n Invalid input, SizeWG => " << MAXSIZE << " please try again.\n\n";
}
}
|
The while loop is something that you should get use to. I realize this may be ahead of what you have learned, but there is no time like the present to learn something new. I you need a better explanation let me know.
In line 24 "SizeWG" has no meaning as a variable name and even less meaning to a user. Consider changing "SizeWG" to "value" and see how it reads. Think about the perspective of a user who has no knowledge of C++ or any other language. Good advice when writing a prompt or error message.
This is a good example of how "MAXSIZE" can be used. Having only place to change its value you do not have to hunt through your program to find magic numbers to change. You will very likely miss 1 or 2.
After this I did this for testing:
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
|
arrayWG = new int[arrSize];
if (!loopTimes) // <--- Used for testing. Comment or remove when finished. Also removing the (!) can change the order.
{
arrayWG[0] = 1245;
arrayWG[1] = 214;
arrayWG[2] = 222;
loopTimes++;
}
else
{
arrayWG[0] = 532;
arrayWG[1] = 1243;
arrayWG[2] = -12;
loopTimes++;
}
//for (int i = 0; i < arrSize; i++) // <--- Commented for testing. Because we know it works.
//{
// cout << " Value #" << i + 1 << ": ";
// cin >> arrayWG[i];
//}
|
You can change the "else" to "else if (loopTimes == 1)" and add more "else if" statements to create different scenarios. This speeds up testing not having to enter numbers each time you run the program. You can also initialize "arrSize" to 3 and comment the "cin" statement for this.
After this I need to determine if
getIntUDCWilliamG
is enough or if
getSIWSmallestTUDCWilliamG(arrayWG, arrSize);
is needed and wow it needs to be changed.
Andy