Tuesday, July 28, 2009

Please help!!! C++ user defined funtions?

im trying to put some functions into my code. i want to create 2 functions in my switch statement one for case A and one for case B but im having trouble with my variables and the user input here is my code im open to any suggestions








#include %26lt;iostream%26gt;





using namespace std;





int main()


{





char choice; // variable for selection


int quantity; // variable for quantity of numbers


int num1; // variable for option A numbers


int num2; // variable for option B numbers


int largest; // variable for largest number


int smallest; // variable for smallest number





do


{ // beginning of do while loop





cout %26lt;%26lt; " Please choose one of the following options" %26lt;%26lt; endl; // instructions telling user what to enter





cout %26lt;%26lt; " A. - find the largest number with a known quantity of numbers" %26lt;%26lt; endl; //


cout %26lt;%26lt; " B. - Find the smallest number with an unknown quantity of numbers" %26lt;%26lt; endl; // display menu options to user


cout %26lt;%26lt; " C. - Quit" %26lt;%26lt; endl; //





cout %26lt;%26lt; " Please enter your choice: - "; //


cin %26gt;%26gt; choice; // ask user for input


cout %26lt;%26lt; endl; //





switch (choice)


{ // beginning of switch statement





case 'a': // case for when user selects option A


case 'A':





cout %26lt;%26lt; " A. - find the largest number with a known quantity of numbers" %26lt;%26lt; endl;


cout %26lt;%26lt; " Please enter the total quantity of numbers you would like to use: ";


cin %26gt;%26gt; quantity;





for (quantity;quantity %26gt; 0;quantity--)


{





cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; largest;


cout %26lt;%26lt; endl;





for (quantity;quantity %26gt; 1;quantity--)


{


cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; endl;





if (largest %26lt;= num1)


largest = num1;


}





}





cout %26lt;%26lt; " the largest number you entered is (" %26lt;%26lt; largest %26lt;%26lt; ")" %26lt;%26lt; endl;





break;





case 'b': // case for when user selects option B


case 'B':





cout %26lt;%26lt; " B. - Find the smallest number with an unknown quantity of numbers" %26lt;%26lt; endl;





cout %26lt;%26lt; " enter your first number - ";


cin %26gt;%26gt; num2;


cout %26lt;%26lt; endl;





smallest = num2;





cout %26lt;%26lt; " to get result and return to main menu enter (-99) for your number" %26lt;%26lt; endl;





while (num2 != -99)


{





cout %26lt;%26lt; " please enter another number - ";


cin %26gt;%26gt; num2;


cout %26lt;%26lt; endl;





if (num2 != -99)


if (num2 %26lt; smallest)


smallest = num2;


}





cout %26lt;%26lt; " the smallest number you entered is (" %26lt;%26lt; smallest %26lt;%26lt; ")" %26lt;%26lt; endl;





break;





case 'c': // case for when user selects to exit the program


case 'C':





exit('c');





default: // case for when user tries to select and option other than a, A, b, B, c, or C





cout %26lt;%26lt; " the letter you entered is not valid" %26lt;%26lt; endl;





} // end of switch statement





} while (choice != 'c'); // end of do while loop





return 0;





}

Please help!!! C++ user defined funtions?
create 2 functions called


void largest()


{


do all your things here;


returns nothing;


}


void smallest()


{


do all your things here;


returns nothing;


}
Reply:I see a problem with this section of code:


You are reading in a value for largest. That should only change when num1 is larger than largest's current value.





for (quantity;quantity %26gt; 0;quantity--)


{





cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; largest;


cout %26lt;%26lt; endl;





for (quantity;quantity %26gt; 1;quantity--)


{


cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; endl;





if (largest %26lt;= num1)


largest = num1;


}





}


No comments:

Post a Comment