473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Error displaying minimum input

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
3 2054
weaknessforcats
9,208 Expert Mod 8TB
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
manxie
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
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
2
by: dmiller23462 | last post by:
Hey guys, I'm back again....I've got the results displaying that I wanted but instead of having a "next" or "previous" link displayed I am getting the error msg below (I actually get the data that...
3
by: david | last post by:
I implemented RangeValidator for UserID as follows: Type: string Minimum Value: 4 Maximum Value: 16 ================== After compilation, I access to the web page for login and get the following...
13
by: albert_reade | last post by:
Could someone help me figure out why this error keeps occuring thanks for the help. error: cc FordFulkerson.c FordFulkerson.c:117:2: warning: no newline at end of file Code: #include...
12
by: korund | last post by:
How to make javascript alert with non-english text displaying correctly on computers where english only is default system & language settings? For web page the solution is just use meta tags:...
8
by: someone | last post by:
I'm making an program that encodes text, and then writes it into another text file. The problem is that I can't seem to get the "file exists" checking right. If I use the command (coder is the...
1
by: greek | last post by:
Hi! I'm suppode to write a prg to calculate maximum and minimum of 4 intergers by using functions i've writen the code but getting the error: call of a non function..cant see my error help me..here...
2
by: jthep | last post by:
I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this? The function...
6
by: dragiton | last post by:
ASP Code Redirect Error Hello I recently relaunched a website containing asp code which used to work perfectly. However, after resetting up the SQL DB and trying to work out some site bugs I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.