By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 424,827 Members | 2,177 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

# Error displaying minimum input

 P: 2 Dear All Readers, I'm supposed to create a program with a switch and using voids to execute number of codes, that includes finding sum, average, maximum, and minimum, please read my code: Expand|Select|Wrap|Line Numbers #include  #include  #include  using namespace std;   void WELCOME(); void triangleFormula(); void goodbyeMessage(); void arithmeticFormula(); void secondArithmeticFormula(); void thePowerOf(); void goodbyeMessage(); // these are the voids that is used // in the switches   int adminInput; int userInput; int selection (0); int sum (0); int average(0); int inputTimes (0); int base; int exponent; double answerForPower; int numberOfSet; int dataForSystem;   const int SENTINEL_VALUE (-777); // this is used to let the user stop // manually in the arithmetic sets // so that the numbers could be calculated   int main () { //main       WELCOME();     // welcomming message       system("pause");     system("cls");       while (true)     { //while         cout << "Please choose from" << endl;         cout << "1. Draw an inverted triangle of your chosen height using your chosen letter" << endl;         cout << "2. Enter some numbers and learn the sum, average, min, and max of your inputs" << endl;         cout << "3. Same 2, but with a different way to end inputting" << endl;         cout << "4. Let me Calculate a^b (a raised to the b) for whatever a and b you'd like" << endl;         cout << "5. Quit this program" << endl;         // this is the menu for the users to read         // so that the user could input numbers         // from 1 to 5           cout << endl;           cout << "Enter choice " << endl;           cin >> adminInput;           system ("cls");           switch (adminInput)         { // switch         case 1:             triangleFormula ();             selection++;             break;         case 2:             arithmeticFormula ();             selection++;             break;         case 3:             secondArithmeticFormula ();             selection++;             break;         case 4:             thePowerOf ();             selection++;             break;         case 5:             goodbyeMessage ();             return 0;         } // switch     } // while } // main   void WELCOME () { // welcome     cout << "Welcome to my program" << endl;     cout << "I can do several things for you." << endl;     cout << "When you are ready to enter my program" << endl;     cout << "Hit any key" << endl; } // welcome   void triangleFormula () { // triangle formula     int tallness;     char userLetter;       cout << "Which letter to use for the triangle" << endl;     cin >> userLetter;     cout << "How tall?" << endl;     cin >> tallness;       for (int i(0); i<=tallness; i++)     { // first for         for (int j(1); j<=(tallness-i); j++)         { // second for             cout << userLetter;         } // second for         cout << endl;       } // first for     system("pause");     system("cls"); } // triangle formula   void goodbyeMessage () { // goodbye message     cout << "You made " << selection << " selections" << endl;     cout << "Thank you for using my program" << endl;     system("pause");     system("cls"); } // goodbye message   void arithmeticFormula () { // arithmetic Formula     cout << "Please enter a set of integers " << endl;     cout << "Please type -777 to stop" << endl;       while (userInput != SENTINEL_VALUE)     { // while         cin >> userInput;           if (userInput !=SENTINEL_VALUE)         { // if             inputTimes++;               sum+=userInput;           } // if       } // while       int max (0);     int min (0);       if ((max < userInput)&&(min!=SENTINEL_VALUE))     { // if         max = userInput;     } // if     if ((min > userInput)&&(min!=SENTINEL_VALUE))     { //if         min = userInput;     } //if       average = sum / inputTimes;       cout << "The sum of your input is" << endl;     cout << sum << endl;     cout << "The average of your input is" << endl;     cout << average << endl;     cout << "The Maximum input is" << endl;       cout << max << endl;       cout << "The Minimum input is" << endl;       cout << min << endl;   } // arithmetic Formula   void secondArithmeticFormula () { // second arithmetic Formula       int setsOfData(0);     int secondInputTimes (0);     int accum (0);     int secondUserInput(0);     int dataForSystem;       cout << "How many sets of data will be entered?" << endl;     cin >> setsOfData; while (setsOfData !=SENTINEL_VALUE) { //while     for (int i=setsOfData;setsOfData<=i;i++)     { //for     cout << "Please begin entering data set." << endl;     cout << "Enter -777 to indicate you are done with this set." << endl;     cin >> dataForSystem;       cin >> secondUserInput;       secondInputTimes++;       accum+=secondUserInput;     average = sum / secondInputTimes;     } //for } // while     cout << "The sum of your input is" << endl;     cout << sum << endl;     cout << "The average of your input is" << endl;     cout << average << endl;     cout << "The Maximum input is" << endl;       int max(0);     int min(0);       if (max < secondUserInput)     { // if         max = secondUserInput;     } // if     else if (min > secondUserInput)     {         min = secondUserInput;     }     cout << "The Minimum input is" << endl;       cout << min << endl; } // second arithmetic formula   void thePowerOf () { // the power of       cout << "Please enter the first number for the base" << endl;     cin >> base;     cout << "Please enter the second number for the exponent" << endl;     cin >> exponent;     cout << "Your Base and Exponent is" << endl;       cout << base << " "<< "and " << exponent << " which is" << base << "^ (raised to the)" << exponent << endl;       cout << "The number is" << endl;       for (int i=1; i> userInput;           if (userInput !=SENTINEL_VALUE)         { // if             inputTimes++;               sum+=userInput;           } // if       } // while       int max (0);     int min (0);       if ((max < userInput)&&(min!=SENTINEL_VALUE))     { // if         max = userInput;     } // if     if ((min > userInput)&&(min!=SENTINEL_VALUE))     { //if         min = userInput;     } //if       average = sum / inputTimes;       cout << "The sum of your input is" << endl;     cout << sum << endl;     cout << "The average of your input is" << endl;     cout << average << endl;     cout << "The Maximum input is" << endl;       cout << max << endl;       cout << "The Minimum input is" << endl;       cout << min << endl;   } // arithmetic Formula it wouldn't show me the maximum input, it would show a 0 for some reason and my minimum would always be the SENTINEL_VALUE (-777), because I had to input -777 to stop it from reading the sum and average works perfectly. -------------------------------------------------------------------------------------- then there is another problem here: Expand|Select|Wrap|Line Numbers void secondArithmeticFormula () { // second arithmetic Formula       int setsOfData(0);     int secondInputTimes (0);     int accum (0);     int secondUserInput(0);     int dataForSystem;       cout << "How many sets of data will be entered?" << endl;     cin >> setsOfData; while (setsOfData !=SENTINEL_VALUE) { //while     for (int i=setsOfData;setsOfData<=i;i++)     { //for     cout << "Please begin entering data set." << endl;     cout << "Enter -777 to indicate you are done with this set." << endl;     cin >> dataForSystem;       cin >> secondUserInput;       secondInputTimes++;       accum+=secondUserInput;     average = sum / secondInputTimes;     } //for } // while     cout << "The sum of your input is" << endl;     cout << sum << endl;     cout << "The average of your input is" << endl;     cout << average << endl;     cout << "The Maximum input is" << endl;       int max(0);     int min(0);       if (max < secondUserInput)     { // if         max = secondUserInput;     } // if     else if (min > secondUserInput)     {         min = secondUserInput;     }     cout << "The Minimum input is" << endl;       cout << min << endl; } // second arithmetic formula when i entered the sets of data, it would show How many sets of data will be entered? 20 Please begin entering data set. Enter -777 to indicate you are done with this set. 20 20 Please begin entering data set. Enter -777 to indicate you are done with this set. 20 20 Please begin entering data set. Enter -777 to indicate you are done with this set. 20 20 Please begin entering data set. Enter -777 to indicate you are done with this set. it would keep saying the please begin entering data set... I tried multiple of ways to improve this, however, the condition of my program began worse... been doing this seen my C++ class in University since 4:30 PM till right now 12:07 PM, can someone please help me Oct 18 '07 #1
Share this Question
3 Replies

 Expert Mod 5K+ P: 9,197 void secondArithmeticFormula () { // second arithmetic Formula int setsOfData(0); int secondInputTimes (0); int accum (0); int secondUserInput(0); int dataForSystem; cout << "How many sets of data will be entered?" << endl; cin >> setsOfData; while (setsOfData !=SENTINEL_VALUE) <<<<<> dataForSystem; cin >> secondUserInput; secondInputTimes++; accum+=secondUserInput; average = sum / secondInputTimes; } //for } // while cout << "The sum of your input is" << endl; cout << sum << endl; cout << "The average of your input is" << endl; cout << average << endl; cout << "The Maximum input is" << endl; If you look at your loop, you will stay there as long as setsOfData !=SENTINEL_VALUE. But you never change setsOfData inside the loop. Therefore, you going to be in that loop for a very long time. Oct 18 '07 #2

 P: 2 If you look at your loop, you will stay there as long as setsOfData !=SENTINEL_VALUE. But you never change setsOfData inside the loop. Therefore, you going to be in that loop for a very long time. Thank you for replying my message the SENTINEL_VALUE is used to let the user stop inputting data... so it would keep going in the loop until the SENTINEL VALUE is entered... Maybe I mistaken your point, if there is, please correct me Thank You Oct 18 '07 #3

 Expert Mod 5K+ P: 9,197 Maybe I mistaken your point, if there is, please correct me I think so. Here is your code simplified: Expand|Select|Wrap|Line Numbers   while (setsOfData !=SENTINEL_VALUE) {      for (int i=setsOfData;setsOfData<=i;i++)     {        cout << "Please begin entering data set." << endl;        cout << "Enter -777 to indicate you are done with this set." << endl;        cin >> dataForSystem;          cin >> secondUserInput;        }  }   Once you get in this while loop, setsofData never changes. When the for loop completes, the while loop starts over. That while loop cannot stop until the condition setsOfData !=SENTINEL_VALUE is false. And the condtion will never be false unless setsOfData is changed inside the loop. It that's not possible, then setsOfData is not the thing to use to control the loop. Oct 18 '07 #4

### Post your reply

Sign in to post your reply or Sign up for a free account. 