I am writing this code, and I every time I run question A, no matter what numbers I put in, I get "larger = 0. Do you think you all can help?
Thanks
#include <iostream>
using namespace std;
int awesome ();
int best ();
int crazy ();
int main ()
{
char letter;
int smallestNumber;
int largestNumber;
int large;
int small;
int number;
cout << "Please select one of the following choices " <<endl;
cout << "A: Find the largest number with a known quantity of number " << endl;
cout << "B: Find the smallest number with an unknown quantity of number " << endl;
cout << "C: Quit" << endl;
cout << "Please enter your choice: ";
cin >> letter;
cout << endl;
switch (letter)
{
case 'A':
case 'a':
largestNumber = awesome ();
cout << "Your largest number = " << large << endl;
break;
case 'B':
case 'b':
smallestNumber = best ();
cout << "the smallest number you entered is " <<number <<endl;
break;
case 'c':
case 'C':
while (letter != 'C' && letter != 'c')
cout << endl;
}
system("pause");
return 0;
}
int awesome ()
{
int i;
int counter;
int large;
int largest = -999999;
cout << "How many numbers would you like to enter"<< " ";
cin >> i;
cout << endl;
cout << "Please enter your numbers" << endl;
for (counter = 1; counter <= i; counter++)
{
cin >> large;
// This test is the core of the functionality
if (large > largest)
// If it's larger you make a one-for one replacement
largest = large;
}
return large;
}
const int Sentinel = -99;
int best ()
{
int smallest;
smallest = 999999999;
int number;
cout << "Please enter your numbers with your last number being" <<" " << Sentinel << endl;
cin >> number;
while (number != Sentinel)
{
if (number < smallest)
smallest = number;
cin >> number;
}
case'A':
case'a':
largestNumber = awesome ();
cout << "Your largest number = " << large << endl;
break;
Presumably, you intended to print the value of largestNumber rather than large (which, by the way, never gets used; I'm not sure why that's there).
You have the same problem for your case'B': of your switch statement.
Also, in awesome(), I think you meant to return largest; rather than return large;.
Same thing for best() -- I think you wanted to return smallest; rather than return number;.
By the way, what's up with this?
39 40 41 42
case'c':
case'C':
while (letter != 'C' && letter != 'c')
cout << endl;
That while loop will never be entered (and plus, it would be an infinite loop if you could enter it).