943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1187
  • C++ RSS
Nov 18th, 2007
0

help with loops

Expand Post »
So here is what I am trying to accomplish. I have been trying and trying for a few hours to figure out how to do this.

Write a program that displays a menu with the following choices to the user.
A - Find the largest # with a known quantity of numbers
B - Find the smallest # with an unknown quantity of numbers
C - Quit
Please enter your choice ___

This menu needs to be repeatedly displayed until the user chooses to quit.

If A is chosen, you should ask the user how many numbers he wants to enter. If he enters 5, you should read 5 numbers from him. You should then display the largest number he entered. Use a for loop to read these and display the largest.

If B is chosen, you should keep reading numbers no matter what until the user enters -99. Then you should display the smallest number he entered not counting the -99.





c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int A_QUANTITY = 10; // Amount of #'s entered for option A
  6. const int B_SENTINEL = -99; // Sentinal used to end option B
  7.  
  8. int main ()
  9. {
  10. bool again = true; // Used to control main source loop
  11. char choice; // Stores the choice made
  12. int biggest; // Stores largest #
  13. int smallest; // Stores smallest #
  14. int temp; // Stores next number for comparison
  15. int i; // Controls the For loop
  16.  
  17.  
  18.  
  19. while (again)
  20. {
  21. // Displays instructions to user
  22. cout << "A - Find the largest # with a known quantity of numbers" << endl;
  23. cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
  24. cout << "C - Exit" << endl;
  25. cout << "Please enter your choice" << endl;
  26.  
  27. // Gets input from user
  28. cin >> choice;
  29.  
  30. if (choice == B) // Beginning of if statement
  31. {
  32. i = 0;
  33. do
  34. {
  35. cout << "Enter number (-99 to end): ";
  36. cin >> i >> endl;
  37. i++;
  38. cout << "You entered: " << i << "\i";
  39. }while (i != 0);
  40. }
  41.  
  42. else if (choice == A) // Beginning of else if statement
  43. {
  44. for( i = 0; i < 10; i++)
  45. cout << "i = " << i << endl;
  46.  
  47. for(i = 10; i>=0; i--);
  48. }
  49.  
  50.  
  51.  
  52.  
  53. {return 0;
  54. }
Last edited by Ancient Dragon; Nov 18th, 2007 at 5:21 pm. Reason: add line numbers
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ArrogantLegend is offline Offline
9 posts
since Oct 2007
Nov 18th, 2007
0

Re: help with loops

line 36: delete the endl from that line because it doesn't go with cin. But because i is an int you will probably have to flush the '\n' from the keybord buffer, so add cin.ignore() after line 36 and before line 37.

line 37: delete this because there is no need to increment the value of i.

line 39: should be while( i != -99) because that's what your instructions say.

What you are missing in the loop is a counter that keeps track of the minimum value entered.

>>If A is chosen, you should ask the user how many numbers he wants to enter
You have not done that yet. And you need to use another int variable to keep track of the largest number entered.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 18th, 2007
0

Re: help with loops

Do you have a question?
Reputation Points: 244
Solved Threads: 77
Posting Pro in Training
ravenous is offline Offline
448 posts
since Jul 2005
Nov 18th, 2007
0

Re: help with loops

So im not exactly sure of how to do a counter see I left my book at home and I flew to florida without so i can't look up examples.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ArrogantLegend is offline Offline
9 posts
since Oct 2007
Nov 18th, 2007
0

Re: help with loops

I made some changes to your smallest integer do while loop. I'll leave the rest for you to try to figure out.

C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. const int A_QUANTITY = 10; // Amount of #'s entered for option A
  8. const int B_SENTINEL = -99; // Sentinal used to end option B
  9.  
  10. int main ()
  11. {
  12. bool again = true; // Used to control main source loop
  13. char choice; // Stores the choice made
  14. int biggest = 0; // Stores largest #
  15. int smallest = 0; // Stores smallest #
  16. int temp; // Stores next number for comparison
  17. int i; // Controls the For loop
  18.  
  19.  
  20.  
  21. while (again)
  22. {
  23. // Displays instructions to user
  24. cout << "A - Find the largest # with a known quantity of numbers" << endl;
  25. cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
  26. cout << "C - Exit" << endl;
  27. cout << "Please enter your choice" << endl;
  28.  
  29. // Gets input from user
  30. cin >> choice;
  31.  
  32. if (choice == 'B' || 'b') // Beginning of if statement
  33. {
  34. i = 0;
  35. do
  36. {
  37. cout << "Enter number: (-99 to exit)" ;
  38. //cin >> i;
  39. i++;
  40.  
  41. cin >> temp; //this sets temp to what the user inputs
  42. //cout << "You entered: " << temp << endl; //<< "\i" <---What was this for?
  43.  
  44. if(i == 1)
  45. smallest = temp;
  46.  
  47. if(smallest > temp && temp !=-99)
  48. smallest = temp;
  49.  
  50. }while (temp != B_SENTINEL);
  51.  
  52. cout << endl << smallest << endl;
  53. }
  54.  
  55. else if (choice == 'A' || 'a') // Beginning of else if statement
  56. {
  57. for( i = 0; i < 10; i++)
  58. cout << "i = " << i << endl;
  59.  
  60. }
  61.  
  62. else if (choice == 'C' || 'c')
  63. again = false;
  64. }
  65.  
  66. return 0;
  67. }
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
SHWOO is offline Offline
73 posts
since Jul 2006
Nov 18th, 2007
0

Re: help with loops

I get the program to run but no matter what choice i make it comes up with -99 to exit...
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int A_QUANTITY = 10;
  6. const int B_SENTINEL = -99;
  7.  
  8. int main ()
  9. {
  10. bool again = true;
  11. char choice;
  12. int biggest = 0;
  13. int smallest = 0;
  14. int temp;
  15. int i;
  16.  
  17.  
  18.  
  19. while (again)
  20. {
  21. // Displays instructions to user
  22. cout << "A - Find the largest # with a known quantity of numbers" << endl;
  23. cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
  24. cout << "C - Exit" << endl;
  25. cout << "Please enter your choice" << endl;
  26.  
  27. // Gets input from user
  28. cin >> choice;
  29.  
  30. if (choice == 'B' || 'b')
  31. {
  32. i = 0;
  33. do
  34. {
  35. cout << "Enter number: (-99 to exit)" ;
  36. //cin >> i;
  37. i++;
  38.  
  39. cin >> temp;
  40. //cout << "You entered: " << temp << endl; //<< "\n"
  41.  
  42. if(i == 1)
  43. smallest = temp;
  44.  
  45. if(smallest > temp && temp !=-99)
  46. smallest = temp;
  47.  
  48. }while (temp != B_SENTINEL);
  49.  
  50. cout << endl << smallest << endl;
  51. }
  52.  
  53. else if (choice == 'A' || 'a')
  54. {
  55. for( i = 0; i < 10; i++)
  56. cout << "i = " << i << endl;
  57.  
  58. }
  59.  
  60. else if (choice == 'C' || 'c')
  61. again = false;
  62. }
  63.  
  64. return 0;
  65. }
Last edited by Ancient Dragon; Nov 19th, 2007 at 1:52 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
malathuis is offline Offline
8 posts
since Nov 2007
Nov 19th, 2007
0

Re: help with loops

>>I get the program to run
No you didn't because there are syntax errors on lines 30, 53 and 60. So if you ran a program it was not the code you posted. Line 30 should have been coded like this: if (choice == 'B' || choice == 'b') and the others similar.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 19th, 2007
1

Re: help with loops

>>I get the program to run
No you didn't because there are syntax errors on lines 30, 53 and 60. So if you ran a program it was not the code you posted. Line 30 should have been coded like this: if (choice == 'B' || choice == 'b') and the others similar.
This isn't a syntax error, the code compiles fine. It also runs, but does not have the desired operation since a statement like 'b' always returns true, hence the error. As you said, it should be if (choice == 'B' || choice == 'b') to get the desired behaviour, but it's not a syntax error.

On slightly different point, in the program you go to the trouble of defining B_SENTINEL on line 6. However, on line 45 you test temp == -99 . You should use your B_SENTINEL variable here as it helps keep your code in a maintainable state; that is, you will only have to change the exit value at the top and it will be changed everywhere.
Last edited by ravenous; Nov 19th, 2007 at 7:45 pm. Reason: Added third paragraph
Reputation Points: 244
Solved Threads: 77
Posting Pro in Training
ravenous is offline Offline
448 posts
since Jul 2005
Nov 20th, 2007
0

Re: help with loops

I'll do an example up and get back to you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zippie is offline Offline
5 posts
since Nov 2007
Nov 20th, 2007
0

Re: help with loops

Sorry it took so long to send this example, i was quite busy. I hope this example helps. Note This program doesn't have ANY error checking regarding converting characters to integers, so don't enter any funny stuff at the prompt that asks you for the numbers .

c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5. #include<string>
  6. using std::string;
  7. #include<vector>
  8. using std::vector;
  9.  
  10. /*
  11.   Function Description: Looks through a int vector and finds the largest or smallest int
  12.   large_small = true if looking for largest int else false to get smallest int
  13. */
  14. int compare(bool large_small,vector<int> numbers)
  15. {
  16.  
  17. int biggestNum = numbers[0];
  18. if(large_small)// finding largest number
  19. {
  20. for(int i=0; i<numbers.size();++i)
  21. {
  22. if(biggestNum < numbers[i])
  23. biggestNum = numbers[i];
  24. }
  25. }
  26. else// used to find smallest number
  27. {
  28. for(int i=0; i<numbers.size();++i)
  29. {
  30. if(biggestNum > numbers[i])
  31. biggestNum = numbers[i];
  32. }
  33. }
  34. return biggestNum;
  35. }
  36.  
  37.  
  38. /*
  39.   Function Description: gets largest number
  40. */
  41. int getNumbers(bool large_small)
  42. {
  43. int size;
  44. int tmp;
  45. vector<int> numbers;
  46.  
  47. cout <<"Enter how many numbers to read.";
  48. cin >>size;
  49.  
  50. //reads all numbers in
  51. for(int i=0; i < size; ++i)
  52. {
  53. cout <<(i+1)<<": ";// prompt with numbers entered
  54. cin >>tmp;// reads in number
  55. numbers.push_back( tmp ); // added number
  56. }
  57. return compare(large_small,numbers);
  58. }
  59.  
  60. int main()
  61. {
  62. //attributes
  63. string str;
  64. int largestNum=0;
  65. int smallestNum=0;
  66.  
  67. cout <<"Welcome to This appliction :)\n"<<endl;//Welcome message
  68.  
  69. while(true)//never ending loop
  70. {
  71. //Options presented to
  72. cout <<"Please make a selection...."<<endl;
  73. cout <<"A - Find the largest number"<<endl;
  74. cout <<"B - Find the smallest number"<<endl;
  75. cout <<"C - Quit"<<endl;
  76. cin >>str;// reading in input
  77.  
  78. if((str != "a") && (str != "A") && (str != "b") && (str != "B") && (str != "c") && (str != "C"))
  79. {
  80. cout <<"Invalid option..."<<endl;
  81. }
  82.  
  83. if((str == "a") || (str == "A"))
  84. {
  85. cout <<"Largest size: "<< getNumbers(true) <<endl;//returns largest number
  86. }
  87.  
  88. if((str == "b") || (str == "B"))
  89. {
  90. cout <<"Smallest size: "<< getNumbers(false) <<endl;
  91. }
  92.  
  93. if((str == "c") || (str == "C"))
  94. exit(0);//exiting
  95. }
  96. return 0; //returning 0 to operating system saying all is good.
  97. }
Attached Files
File Type: cpp loops_numbers_example.cpp (2.3 KB, 13 views)
Last edited by zippie; Nov 20th, 2007 at 9:30 am. Reason: Added attachment of file
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zippie is offline Offline
5 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem with classes
Next Thread in C++ Forum Timeline: Help with atoi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC