## swingkitty

I have an assignment for my class that asks to modify my previous assignment to include a function for both A and B. The function for A should have the quantity of numbers passed in as a parameter and needs to return the largest number. The function for B should have no parameters and return the smallest number.

Here is some pseudo-code for your greatest function:

``````int Greatest(int count)
{
// Initialize final result variable to largest number possible
//
// start a loop, increment from 0 to count
// Compare number to final result variable
// if number is greater than final result variable, then
// store in final result variable
// end of loop
// return final result
}// end of function greatest

//Here is what I have so far. The application seems to run well without the Void function. I am new to programming, and I am finding it difficult to handle this assignment. I am looking for a clear path of explanation in order to learn.

#include <iostream>
using namespace std;
void smallest();
int main ()

{
char choice;
int quantity;
int num1;
int num2;
int largest;
int smallest;
double larger(double x, double y);
double smaller(double x, double y);
do
{
cout<<"Please choose one of the following options"<<endl;
cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;
cin>>choice;
cout<<endl;
switch (choice)
{
case'a':
case 'A':
//gave both lower case and capital 'a' to allow for user freedom of choice
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;
for(quantity;quantity > 0; quantity--)
{
cin>>largest;
cout<<endl;
for(quantity;quantity > 1; quantity--)
{
cin>>num1;
cout<<endl;
if(largest<=num1)
largest=num1;
}
}
cout<<"The largest number entered is ("<<largest<<")"<<endl;
break;
case 'b':
case 'B':
//gave both lower case and capital 'b' to allow for user freedom of choice
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cin>>num2;
cout<<endl;
smallest=num2;
//used sentinel to allow user to initiate close of process
while (num2 !=-999)
{
cin>>num2;
cout<<endl;
if (num2 != -999)
if (num2<smallest)
smallest=num2;
}//keeps prompting until sentinel is input
cout<<"The smallest number you entered is: (" <<smallest<<")"<<endl;
break;
case'c':
case'C':
//gave both lower case and capital 'c' to allow for user freedom of choice
exit('c');
default:
cout<<"The letter you entered is not valid"<<endl;
//this works well in case of user input error during the menu.
//Does not help with user input error during the different parts of application.
}
if (choice == 'b')
{
cout << "Enter quantity of numbers: ";
cin >> quantity;
count = quantity;
larger (quantity);
} if (choice == 'b')

{
cout << "Enter quantity of numbers: ";
cin >> qty;
count = qty;
larger (qty);
}
}
while(choice !='c');
//end of do while loop
return 0;
}

double largest (double x, double y)

{
double max = x;
if ( y > max)
max = y;
return max;
}
void smallest()
{
int smallest(int quantity);
for (count = 1; count > qty; count++)
{
cout << "Enter " << qty << " numbers: ";
cin >> num2;
max = smallet(min, num2);
done = false;
}
cout << "The largest number is: " << min;
}void smallest()``````

## VernonDozier 2,218

``````for(quantity;quantity > 0; quantity--)
{
cin>>largest;
cout<<endl;
for(quantity;quantity > 1; quantity--)
{
cin>>num1;
cout<<endl;
if(largest<=num1)
largest=num1;
}
}``````

This code looks strange to me. You have a nested for-loop that uses the same variable name (quantity) as the outer for-loop does. You are also asking for user input inside the inner for-loop as well as before the inner for-loop. When the inner for-loop changes the value of quantity with the command `quantity--` , that also changes `quantity` for the outer for loop. So it looks to me like you are only going to go through the outer for-loop twice, regardless of what the value of quantity is. I don't see the need for a nested loop to find the largest number. A single non-nested loop would do the trick fine.

Regarding the functions, does Greatest ask for the user input using cin statements, or does that happen in main? Currently you have the user inputting numbers in both main and Greatest. If the user inputs the number in main(), what's the point of doing it in Greatest too? You're also apparently calculating the largest number in main too, so what is the point of even having a function called Greatest? You need to give both us and yourself greater clarity on what exactly the program does and what each function does.

It looks to me like you have an earlier program that did everything in main and had no functions. Now you are starting functions and you are having a function do the work that used to be done in main. However you have still left that code in main, so you're kind of halfway into modifying an old program and adding stuff to the new one, the result of which is that it's kind of hard to figure out what is supposed to do what. Also, this line is confusing.

Here is some pseudo-code for your greatest function:

Is this sentence addressed to us or is it something your professor is saying to you. I'm guessing that it's the latter and that the professor provided this code skeleton:

``````int Greatest(int count)
{
// Initialize final result variable to largest number possible
//
// start a loop, increment from 0 to count
// Compare number to final result variable
// if number is greater than final result variable, then
// store in final result variable
// end of loop
// return final result
}// end of function greatest``````

However it's all one big program jumbled together (the new code your professor gave you, your old program, and what you've added), so we can't tell which is which and what your actual question is.

## swingkitty

Thank you for your feedback. I have made some changes, am I going in the right direction? I am trying to get to user-defined functions rather then the repitition control structures. I am uncertain about how to code a void function with an unknown quantity of user input numbers. :-/

However it's all one big program jumbled together (the new code your professor gave you, your old program, and what you've added), so we can't tell which is which and what your actual question is.

``````#include <iostream>
#include <cctype>

using namespace std;
const int SENTINEL = -999;

void greatest();

int main ()
{
char choice, A, B, C;
int quantity;
int num;
double largest(double x, double y);

{
cout<<"Please choose one of the following options"<<endl;

cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;

cin>>choice;
cout<<endl;

case ‘A’:
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;

for(quantity;quantity > 0; quantity--)
{
cin>>max;
cout<<endl;
}
double largest (double x, double y)
{
double max = x;
if ( y > max)
max = y;
}
}
}

cout<<"The largest number entered is :"<<endl;
return max;

break;

case 'B':
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cin>>num;
cout<<endl;

void greatest()
{
int greatest(int num);

{
while (num !=-999)
{
cin>>num;
cout<<endl;
cout << "Enter number: ";
cin >> num;
max = greatest(max, num);
done = false;
}
cout << "The smallest number is: " << num;
cout<<endl;
}
}
break;
case'C':

exit('c');

default:
cout<<"The letter you entered is not valid"<<endl;

}

return 0;
}``````

## VernonDozier 2,218

``````#include <iostream>
#include <cctype>

using namespace std;
const int SENTINEL = -999;

void greatest();

int main ()
{
char choice, A, B, C;
int quantity;
int num;
double largest(double x, double y);

{
cout<<"Please choose one of the following options"<<endl;

cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;

cin>>choice;
cout<<endl;

case ‘A’:
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;

for(quantity;quantity > 0; quantity--)
{
cin>>max;
cout<<endl;
}
double largest (double x, double y)
{
double max = x;
if ( y > max)
max = y;
}
}
}

cout<<"The largest number entered is :"<<endl;
return max;

break;

case 'B':
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cin>>num;
cout<<endl;

void greatest()
{
int greatest(int num);

{
while (num !=-999)
{
cin>>num;
cout<<endl;
cout << "Enter number: ";
cin >> num;
max = greatest(max, num);
done = false;
}
cout << "The smallest number is: " << num;
cout<<endl;
}
}
break;
case'C':

exit('c');

default:
cout<<"The letter you entered is not valid"<<endl;

}

return 0;
}``````

Several problems. The biggest one is this. You cannot have nested functions. You have some brackets problems, so it's not completely clear where you intend one function to stop and the next to begin. Line 15 is a function declaration. It is in the function main, which starts on line 10. You want to move line 15 above line 10. You want all of your function declarations BEFORE main starts.

Lines 39 - 44 is a function. it is inside the main function, so that's a nested function. You cannot do that. Move all of these lines AFTER the main function ends.

Brackets on lines 17 and 45 probably don't hurt anything, but they are unnecessary, so you can delete them. The bracket on line 46 ends the main function, probably too soon. Lines 48 - 57 are thus not inside any function. You cannot do that.

Lines 59 - 78 are the greatest function. Line 61 is another nested function declaration. Put it before the main function. Lines 79 and on are not in any function.

You want your code to look something like this:

``````// #include statements here

using namespace std;

// const declarations and any global variables here

// function declarations here

int main ()
{
// main function code
return 0;
}

// function implementation code is here``````

## swingkitty

Thank you for responding so quickly. I've tried to implement the changes you suggested in the correct format, but I don't think I did it correctly. I'm still coming up with errors, the first of which is: C2406: illegal case on line 27. Will you offer me any further guidance? It is also telling me I have error C2563: mismatch in formal parameter list

``````#include <iostream>
#include <cctype>

using namespace std;

const int SENTINEL = -999;
void greatest();
double largest(double x, double y);
int greatest(int num);
char choice, A, B, C;
int quantity;
int num;

int main ()
{
cout<<"Please choose one of the following options"<<endl;

cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;

cin>>choice;
cout<<endl;

case ‘A’:
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;

for(quantity;quantity > 0; quantity--)
{
cin>>max;
cout<<endl;
}

cout<<"The largest number entered is :"<<endl;
return max;
break;

case 'B':
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cin>>num;
cout<<endl;

case'C':
exit('c');

default:
cout<<"The letter you entered is not valid"<<endl;

return 0;
}

void greatest()
{

{
while (num !=-999)
{
cin>>num;
cout<<endl;
cout << "Enter number: ";
cin >> num;
max = greatest(max, num);
done = false;
}
cout << "The smallest number is: " << num;
cout<<endl;
}
break;
}

double largest (double x, double y)
{
double max = x;
if ( y > max)
max = y;
}``````

## VernonDozier 2,218

Better. Still a few problems. Regarding the "illegal case" error, you must only use the word "case" inside of a "switch" statement. If you look back at your ORIGINAL code, you'll notice that you had this line:

``switch(choice)``

This line was deleted in your most recent draft. You want it back in. I'm guessing that those extra brackets were part of the original "switch" statement. The format will be something like this:

``````cin >> choice;
switch(choice)
{
case 'a': case 'A':
// code for if the user enters 'a' or 'A'
break;
case 'b': case 'B':
// code for if the user enters 'b' or 'B'
break;
case 'c': case 'C':
// code for if the user enters 'c' or 'C'
break;
default:
// code for if the user enters something else
}``````

See this link, towards the bottom, for an example of a correct switch statement.

Correct one error at a time, then recompile. Each error gives you a line number usually. make sure to tell us that line number, make sure it's the same line number as the line number of the code you are posting here (or point out the new line number), and tell us as well the precise error message. Fix your brackets(I'm not seeing any bracket errors here at first glance, but the brackets on lines 61 and 75 are not necessary). Your function from 79 to 84 says that it is returning a double in line 79, but you have no "return" statement in that function. For now, and you'll change this later, to make it compile, add this line right before the bracket on line 84:

``return 0.0;``

## swingkitty

That is great, one error down. Thanks for the link. The more sources I have the better, maybe if I read it in different ways, it will click. ;)

I am showing the following three errors (one of course which is fatal) now after cleaning and re-building:

Line 36: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)

//Do I need to initialize 'max'?

Line 41: error C2563: mismatch in formal parameter list
Line 41: fatal error C1903: unable to recover from previous error(s); stopping compilation

I re-inserted the switch statement, fixed and checked the brackets agains and added the return for the void function.

``````#include <iostream>
#include <cctype>

using namespace std;

const int SENTINEL = -999;
void greatest();
double largest(double x, double y);
int greatest(int num);
char choice, A, B, C;
int quantity;
int num;

int main ()
{
cout<<"Please choose one of the following options"<<endl;

cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;

cin>>choice;
cout<<endl;

switch(choice)
{
case 'A':
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;

for(quantity;quantity > 0; quantity--)
{
cin>>max;
cout<<endl;
}

cout<<"The largest number entered is :"<<endl;
return max;
break;

case 'B':
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cin>>num;
cout<<endl;
break;

case'C':
exit('c');
break;

default:
cout<<"The letter you entered is not valid"<<endl;

return 0;
}
}

void greatest()
{
while (num !=-999)
{
cin>>num;
cout<<endl;
cout << "Enter number: ";
cin >> num;
max = greatest(max, num);
done = false;
}
cout << "The smallest number is: " << num;
cout<<endl;
break;
}

double largest (double x, double y)
{
double max = x;
if ( y > max)
max = y;
return 0.0;
}``````

## VernonDozier 2,218

``````case 'A':
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;

for(quantity;quantity > 0; quantity--)
{
cin>>max;
cout<<endl;
}

cout<<"The largest number entered is :"<<endl;
return max;
break;``````

You don't need to initialize max to get this to compile, but you do have to declare max. But you really should change the name to something like theMax since max is a keyword in C++

I guess the numbers in your program are doubles, right, so make max a double and stick this line right before main with your other global variable declarations.

``double theMax;  // or whatever.  But don't call it "max".``

Change all references to max to theMax (or whatever it is). You are also almost certainly going to be changing the code above. It continually writes over max without doing any calculations. You're going to want to end up putting most, if not all of the work this code is doing into a function, so start thinking about that. Figure out exactly what the program does and what each function does. In particular, you need to know whether you are going to ask the user to input the values inside of a function or inside main. Looks to me like you want it in your functions, but make sure you know where to put things.

## swingkitty

Thank you for all your help, Your direction and references have been very clear. I'll work on it and see what I can figure out with the functions and post again on Tuesday.
Have a good night.