Trouble with void function

I have to modify that same code 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. I need to use void greatest(). Any assistance would be greatly appreciated.

#include <iomanip>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

//marks the end of the data being read
const int SENTINEL = -99;

int main()
{

//Declare your variables
int n = 0; //variable to store number
int A = 0; //variable for starting option A's choice & stores the numbers entered
char userinput; //variable store user's choice
int temporary = 0; //variable stores numbers entered for choice A
int B = 0; //variable for starting option B's choice & stores the numbers entered
int C = 0; //variable for starting option C's choice
int smallest = 999999999; //variable to store smallest number for choice B
int largest;

do {
//how the menu choices will be displayed
cout << "++++++++++++++++++++++++++++++++" << endl;
cout << " Program Menu Display " << endl
<< "++++++++++++++++++++++++++++++++" << endl
<< " A) Find the largest number " << endl
<< " B) Find the smallest number " << endl
<< " C) Quit " << endl
<< "+++++++++++++++++++++++++++++" << endl;

//output of user's choice
cout << "Please enter your choice here > "; //enter your choice of A or B or C on the blinking cursor
//then press the enter button to start the program of choice.
cin >> userinput;


switch(userinput)
{
case 'a': //for the use of lowercase letter input
case 'A': //for the use of uppercase letter input

cout << "Please choose how many numbers you want > "; //enter on the blinking cursor how many numbers
//you want to use
cin >> n;

void largest();
for(int i = 0; i < n; i++) //outputs a line of numbers
{
temporary = A;

cout << "Please enter the next number "; //enter each number on the blinking cursor then press enter button.

cin >> A;

if(temporary > A) //if temporary is greater than A
{
A = temporary;
}
}
cout << "The largest number entered was " << A << endl; //after all the numbers are chosen the largest number
//will be generated as the end result.

break; //terminates the loop

case 'b': //for the use of lowercase letter input
case 'B': //for the use of uppercase letter input


cout << "Please pick your first number to start with > "; //output data
cin >> B; //input data


while(B != SENTINEL) //test the loop control variable
{

if(B < smallest) //if B is less than smallest number it replaced
smallest = B;
//(After entering all the numbers you want to use
// enter integer -99 to quit the program)
cout << "Please enter the next number > "; //output data
cin >> B; //input data

}

cout << "The smallest number entered was " << smallest << endl; //after -99 has been entered all the numbers that have
// been entered the smallest number will be generated as the end result.

break; //terminates the loop

case 'c': //for the use of lowercase letter input
case 'C': //for the use of uppercase letter input

cout << "Thank you for running my program " << endl; // when chosing option C the program will end
break; //terminates the loop

default:
cout << "You made an invalid choice " << endl; //if anything besides the choices is entered will receive this message
break; //terminates the loop

}

}while (userinput != 'C' && userinput != 'c'); //while userinput is not equal to C or c will quit the program


return 0; //return statement

}




Can you please clarify your request. and put your code into [/code] blocks to make it easier to read and follow.

What is it exactly that you need to achieve?

Regards,
Z.
I have to create a void function for this program and I am clueless on where that is supposed to go. The example that was given is this:

void greatest()
{

//Initialize final result variable to largest number possible
//Ask user How many numbers he would like to enter
//Read number of numbers user plans to enter
//start a loop, increment from 0 to number of numbers user plans to enter
//Read next number
//Compare number to final result variable
//if number is greater than final result variable, then
//store in final result variable
//end of loop
}void greatest()
{

//Initialize final result variable to largest number possible
//Ask user How many numbers he would like to enter
//Read number of numbers user plans to enter
//start a loop, increment from 0 to number of numbers user plans to enter
//Read next number
//Compare number to final result variable
//if number is greater than final result variable, then
//store in final result variable
//end of loop
}

Thank you.
Last edited on
As per ur reqt

The function for A should have the quantity of numbers passed in as a parameter and needs to return the largest number.

As U need to have the A function that returns void as well you need this method to give you the largest number, so U can use the "pass by reference" concept for the largest variable.

signature of function A may look like

 
void A(int numberOfParameters, int& largestParameter);


It sounds like the assignment is about passing parameters by reference.

Try looking at the tutorial for info on this (http://www.cplusplus.com/doc/tutorial/functions2.html)

This should give you some ideas on where to start, please ask more questions if you still need more help.

It can be difficult to answer general questions like the one above as we do not know what level of answer you require - In C++ there will often be many possible ways to solve a given simple problem so the more help you can give us (in terms of explaining the problem, any ideas for possible solutions etc.) the better we can help you.

When posting code into the forum, please use the 'Insert Code' button (just below the edit box).
it will paste a couple of tags into the edit box, '[ Code ]' and '[ /Code ]' (but without the spaces). Put your code between thes tags and the result is
1
2
3
4
5
6
int main()
{
    //This is a cormatted code example!
    cout << "Hello World" << endl;
    return 0;
}

which is much simpler for everyoen to read :-)
If I was to translate that pseudo into code it'd be:

1
2
3
4
5
6
7
8
9
10
11
12
13
void greatest()
{

//Initialize final result variable to largest number possible
//Ask user How many numbers he would like to enter
//Read number of numbers user plans to enter
//start a loop, increment from 0 to number of numbers user plans to enter
//Read next number
//Compare number to final result variable
//if number is greater than final result variable, then
//store in final result variable
//end of loop
}void greatest()


Into:
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
#include <iostream>

using namespace std;

void greatest() {

 int iBiggest = 0;
 int iCurrent = 0;
 int iNumberToEnter = 0;

 cout << "How many number do you want? ";
 cin >> iNumberToEnter;

 for (int i = 0; i < iNumberToEnter; ++i) {
  cout << "Please enter Number: ";
  cin >> iCurrent;

  if (iCurrent > iBiggest)
    iBiggest = iCurrent;
 }
 
 cout << "The biggest number you entered was " << iBiggest << endl;
}

int main() {
 greatest();

 return 0;
}
Last edited on
i think there should be function overloading....
lets start like this,.....
#include<iostream.h>
#include<conio.h>
class greatest
{
private: int a;
int b;
public: greatest();
greatest(A)
}
greatest::greatest()
{
statement...
return smallest;
}
greatest::greatest(A)
{
statement....
return greatest;
}
int main()
{
//statements and function call be declared here
}
happy programming!!!!!


Last edited on
Hi milee,
I think U have missed something while posting !!! as I can't get U what U r trying to explain.

Don't U think using the [code] block will increase the readability of the post.
Thanking in Advance...
Topic archived. No new replies allowed.