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
Bytes IT Community
+ 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
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void WELCOME();
  7. void triangleFormula();
  8. void goodbyeMessage();
  9. void arithmeticFormula();
  10. void secondArithmeticFormula();
  11. void thePowerOf();
  12. void goodbyeMessage();
  13. // these are the voids that is used
  14. // in the switches
  15.  
  16. int adminInput;
  17. int userInput;
  18. int selection (0);
  19. int sum (0);
  20. int average(0);
  21. int inputTimes (0);
  22. int base;
  23. int exponent;
  24. double answerForPower;
  25. int numberOfSet;
  26. int dataForSystem;
  27.  
  28. const int SENTINEL_VALUE (-777);
  29. // this is used to let the user stop
  30. // manually in the arithmetic sets
  31. // so that the numbers could be calculated
  32.  
  33. int main ()
  34. { //main
  35.  
  36.     WELCOME();
  37.     // welcomming message
  38.  
  39.     system("pause");
  40.     system("cls");
  41.  
  42.     while (true)
  43.     { //while
  44.         cout << "Please choose from" << endl;
  45.         cout << "1. Draw an inverted triangle of your chosen height using your chosen letter" << endl;
  46.         cout << "2. Enter some numbers and learn the sum, average, min, and max of your inputs" << endl;
  47.         cout << "3. Same 2, but with a different way to end inputting" << endl;
  48.         cout << "4. Let me Calculate a^b (a raised to the b) for whatever a and b you'd like" << endl;
  49.         cout << "5. Quit this program" << endl;
  50.         // this is the menu for the users to read
  51.         // so that the user could input numbers
  52.         // from 1 to 5
  53.  
  54.         cout << endl;
  55.  
  56.         cout << "Enter choice " << endl;
  57.  
  58.         cin >> adminInput;
  59.  
  60.         system ("cls");
  61.  
  62.         switch (adminInput)
  63.         { // switch
  64.         case 1:
  65.             triangleFormula ();
  66.             selection++;
  67.             break;
  68.         case 2:
  69.             arithmeticFormula ();
  70.             selection++;
  71.             break;
  72.         case 3:
  73.             secondArithmeticFormula ();
  74.             selection++;
  75.             break;
  76.         case 4:
  77.             thePowerOf ();
  78.             selection++;
  79.             break;
  80.         case 5:
  81.             goodbyeMessage ();
  82.             return 0;
  83.         } // switch
  84.     } // while
  85. } // main
  86.  
  87. void WELCOME ()
  88. { // welcome
  89.     cout << "Welcome to my program" << endl;
  90.     cout << "I can do several things for you." << endl;
  91.     cout << "When you are ready to enter my program" << endl;
  92.     cout << "Hit any key" << endl;
  93. } // welcome
  94.  
  95. void triangleFormula ()
  96. { // triangle formula
  97.     int tallness;
  98.     char userLetter;
  99.  
  100.     cout << "Which letter to use for the triangle" << endl;
  101.     cin >> userLetter;
  102.     cout << "How tall?" << endl;
  103.     cin >> tallness;
  104.  
  105.     for (int i(0); i<=tallness; i++)
  106.     { // first for
  107.         for (int j(1); j<=(tallness-i); j++)
  108.         { // second for
  109.             cout << userLetter;
  110.         } // second for
  111.         cout << endl;
  112.  
  113.     } // first for
  114.     system("pause");
  115.     system("cls");
  116. } // triangle formula
  117.  
  118. void goodbyeMessage ()
  119. { // goodbye message
  120.     cout << "You made " << selection << " selections" << endl;
  121.     cout << "Thank you for using my program" << endl;
  122.     system("pause");
  123.     system("cls");
  124. } // goodbye message
  125.  
  126. void arithmeticFormula ()
  127. { // arithmetic Formula
  128.     cout << "Please enter a set of integers " << endl;
  129.     cout << "Please type -777 to stop" << endl;
  130.  
  131.     while (userInput != SENTINEL_VALUE)
  132.     { // while
  133.         cin >> userInput;
  134.  
  135.         if (userInput !=SENTINEL_VALUE)
  136.         { // if
  137.             inputTimes++;
  138.  
  139.             sum+=userInput;
  140.  
  141.         } // if
  142.  
  143.     } // while
  144.  
  145.     int max (0);
  146.     int min (0);
  147.  
  148.     if ((max < userInput)&&(min!=SENTINEL_VALUE))
  149.     { // if
  150.         max = userInput;
  151.     } // if
  152.     if ((min > userInput)&&(min!=SENTINEL_VALUE))
  153.     { //if
  154.         min = userInput;
  155.     } //if
  156.  
  157.     average = sum / inputTimes;
  158.  
  159.     cout << "The sum of your input is" << endl;
  160.     cout << sum << endl;
  161.     cout << "The average of your input is" << endl;
  162.     cout << average << endl;
  163.     cout << "The Maximum input is" << endl;
  164.  
  165.     cout << max << endl;
  166.  
  167.     cout << "The Minimum input is" << endl;
  168.  
  169.     cout << min << endl;
  170.  
  171. } // arithmetic Formula
  172.  
  173. void secondArithmeticFormula ()
  174. { // second arithmetic Formula
  175.  
  176.     int setsOfData(0);
  177.     int secondInputTimes (0);
  178.     int accum (0);
  179.     int secondUserInput(0);
  180.     int dataForSystem;
  181.  
  182.     cout << "How many sets of data will be entered?" << endl;
  183.     cin >> setsOfData;
  184. while (setsOfData !=SENTINEL_VALUE)
  185. { //while
  186.     for (int i=setsOfData;setsOfData<=i;i++)
  187.     { //for
  188.     cout << "Please begin entering data set." << endl;
  189.     cout << "Enter -777 to indicate you are done with this set." << endl;
  190.     cin >> dataForSystem;
  191.  
  192.     cin >> secondUserInput;
  193.  
  194.     secondInputTimes++;
  195.  
  196.     accum+=secondUserInput;
  197.     average = sum / secondInputTimes;
  198.     } //for
  199. } // while
  200.     cout << "The sum of your input is" << endl;
  201.     cout << sum << endl;
  202.     cout << "The average of your input is" << endl;
  203.     cout << average << endl;
  204.     cout << "The Maximum input is" << endl;
  205.  
  206.     int max(0);
  207.     int min(0);
  208.  
  209.     if (max < secondUserInput)
  210.     { // if
  211.         max = secondUserInput;
  212.     } // if
  213.     else if (min > secondUserInput)
  214.     {
  215.         min = secondUserInput;
  216.     }
  217.     cout << "The Minimum input is" << endl;
  218.  
  219.     cout << min << endl;
  220. } // second arithmetic formula
  221.  
  222. void thePowerOf ()
  223. { // the power of
  224.  
  225.     cout << "Please enter the first number for the base" << endl;
  226.     cin >> base;
  227.     cout << "Please enter the second number for the exponent" << endl;
  228.     cin >> exponent;
  229.     cout << "Your Base and Exponent is" << endl;
  230.  
  231.     cout << base << " "<< "and " << exponent << " which is" << base << "^ (raised to the)" << exponent << endl;
  232.  
  233.     cout << "The number is" << endl;
  234.  
  235.     for (int i=1; i<exponent;i++)
  236.     {
  237.         base*=base;
  238.     }
  239.  
  240.     cout << base << endl;
  241.  
  242. } // the power of
------------------------------------------------------------------------------------------
especially the:
Expand|Select|Wrap|Line Numbers
  1. void arithmeticFormula ()
  2. { // arithmetic Formula
  3.     cout << "Please enter a set of integers " << endl;
  4.     cout << "Please type -777 to stop" << endl;
  5.  
  6.     while (userInput != SENTINEL_VALUE)
  7.     { // while
  8.         cin >> userInput;
  9.  
  10.         if (userInput !=SENTINEL_VALUE)
  11.         { // if
  12.             inputTimes++;
  13.  
  14.             sum+=userInput;
  15.  
  16.         } // if
  17.  
  18.     } // while
  19.  
  20.     int max (0);
  21.     int min (0);
  22.  
  23.     if ((max < userInput)&&(min!=SENTINEL_VALUE))
  24.     { // if
  25.         max = userInput;
  26.     } // if
  27.     if ((min > userInput)&&(min!=SENTINEL_VALUE))
  28.     { //if
  29.         min = userInput;
  30.     } //if
  31.  
  32.     average = sum / inputTimes;
  33.  
  34.     cout << "The sum of your input is" << endl;
  35.     cout << sum << endl;
  36.     cout << "The average of your input is" << endl;
  37.     cout << average << endl;
  38.     cout << "The Maximum input is" << endl;
  39.  
  40.     cout << max << endl;
  41.  
  42.     cout << "The Minimum input is" << endl;
  43.  
  44.     cout << min << endl;
  45.  
  46. } // 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
  1. void secondArithmeticFormula ()
  2. { // second arithmetic Formula
  3.  
  4.     int setsOfData(0);
  5.     int secondInputTimes (0);
  6.     int accum (0);
  7.     int secondUserInput(0);
  8.     int dataForSystem;
  9.  
  10.     cout << "How many sets of data will be entered?" << endl;
  11.     cin >> setsOfData;
  12. while (setsOfData !=SENTINEL_VALUE)
  13. { //while
  14.     for (int i=setsOfData;setsOfData<=i;i++)
  15.     { //for
  16.     cout << "Please begin entering data set." << endl;
  17.     cout << "Enter -777 to indicate you are done with this set." << endl;
  18.     cin >> dataForSystem;
  19.  
  20.     cin >> secondUserInput;
  21.  
  22.     secondInputTimes++;
  23.  
  24.     accum+=secondUserInput;
  25.     average = sum / secondInputTimes;
  26.     } //for
  27. } // while
  28.     cout << "The sum of your input is" << endl;
  29.     cout << sum << endl;
  30.     cout << "The average of your input is" << endl;
  31.     cout << average << endl;
  32.     cout << "The Maximum input is" << endl;
  33.  
  34.     int max(0);
  35.     int min(0);
  36.  
  37.     if (max < secondUserInput)
  38.     { // if
  39.         max = secondUserInput;
  40.     } // if
  41.     else if (min > secondUserInput)
  42.     {
  43.         min = secondUserInput;
  44.     }
  45.     cout << "The Minimum input is" << endl;
  46.  
  47.     cout << min << endl;
  48. } // 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
Share on Google+
3 Replies


weaknessforcats
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) <<<<<<!!!!!!
{ //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;
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

weaknessforcats
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
  1.  
  2. while (setsOfData !=SENTINEL_VALUE)
  3.     for (int i=setsOfData;setsOfData<=i;i++)
  4.     {
  5.        cout << "Please begin entering data set." << endl;
  6.        cout << "Enter -777 to indicate you are done with this set." << endl;
  7.        cin >> dataForSystem;
  8.  
  9.        cin >> secondUserInput;
  10.  
  11.      } 
  12. }
  13.  
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.