| | |
help with loops
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
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.
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)
#include <iostream> using namespace std; const int A_QUANTITY = 10; // Amount of #'s entered for option A const int B_SENTINEL = -99; // Sentinal used to end option B int main () { bool again = true; // Used to control main source loop char choice; // Stores the choice made int biggest; // Stores largest # int smallest; // Stores smallest # int temp; // Stores next number for comparison int i; // Controls the For loop while (again) { // Displays instructions to user cout << "A - Find the largest # with a known quantity of numbers" << endl; cout << "B - Find the smallest # with an unknown quantity of numbers" << endl; cout << "C - Exit" << endl; cout << "Please enter your choice" << endl; // Gets input from user cin >> choice; if (choice == B) // Beginning of if statement { i = 0; do { cout << "Enter number (-99 to end): "; cin >> i >> endl; i++; cout << "You entered: " << i << "\i"; }while (i != 0); } else if (choice == A) // Beginning of else if statement { for( i = 0; i < 10; i++) cout << "i = " << i << endl; for(i = 10; i>=0; i--); } {return 0; }
Last edited by Ancient Dragon; Nov 18th, 2007 at 5:21 pm. Reason: add line numbers
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
line 37: delete this because there is no need to increment the value of i.
line 39: should be
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2006
Posts: 73
Reputation:
Solved Threads: 0
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)
#include <iostream> using namespace std; const int A_QUANTITY = 10; // Amount of #'s entered for option A const int B_SENTINEL = -99; // Sentinal used to end option B int main () { bool again = true; // Used to control main source loop char choice; // Stores the choice made int biggest = 0; // Stores largest # int smallest = 0; // Stores smallest # int temp; // Stores next number for comparison int i; // Controls the For loop while (again) { // Displays instructions to user cout << "A - Find the largest # with a known quantity of numbers" << endl; cout << "B - Find the smallest # with an unknown quantity of numbers" << endl; cout << "C - Exit" << endl; cout << "Please enter your choice" << endl; // Gets input from user cin >> choice; if (choice == 'B' || 'b') // Beginning of if statement { i = 0; do { cout << "Enter number: (-99 to exit)" ; //cin >> i; i++; cin >> temp; //this sets temp to what the user inputs //cout << "You entered: " << temp << endl; //<< "\i" <---What was this for? if(i == 1) smallest = temp; if(smallest > temp && temp !=-99) smallest = temp; }while (temp != B_SENTINEL); cout << endl << smallest << endl; } else if (choice == 'A' || 'a') // Beginning of else if statement { for( i = 0; i < 10; i++) cout << "i = " << i << endl; } else if (choice == 'C' || 'c') again = false; } return 0; }
Climbing the learning curve of C++
Becoming an expert seems light years away!
Becoming an expert seems light years away!
•
•
Join Date: Nov 2007
Posts: 8
Reputation:
Solved Threads: 0
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)
#include <iostream> using namespace std; const int A_QUANTITY = 10; const int B_SENTINEL = -99; int main () { bool again = true; char choice; int biggest = 0; int smallest = 0; int temp; int i; while (again) { // Displays instructions to user cout << "A - Find the largest # with a known quantity of numbers" << endl; cout << "B - Find the smallest # with an unknown quantity of numbers" << endl; cout << "C - Exit" << endl; cout << "Please enter your choice" << endl; // Gets input from user cin >> choice; if (choice == 'B' || 'b') { i = 0; do { cout << "Enter number: (-99 to exit)" ; //cin >> i; i++; cin >> temp; //cout << "You entered: " << temp << endl; //<< "\n" if(i == 1) smallest = temp; if(smallest > temp && temp !=-99) smallest = temp; }while (temp != B_SENTINEL); cout << endl << smallest << endl; } else if (choice == 'A' || 'a') { for( i = 0; i < 10; i++) cout << "i = " << i << endl; } else if (choice == 'C' || 'c') again = false; } return 0; }
Last edited by Ancient Dragon; Nov 19th, 2007 at 1:52 am. Reason: add code tags
>>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:
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. Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
>>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.
'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
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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)
#include<iostream> using std::cout; using std::cin; using std::endl; #include<string> using std::string; #include<vector> using std::vector; /* Function Description: Looks through a int vector and finds the largest or smallest int large_small = true if looking for largest int else false to get smallest int */ int compare(bool large_small,vector<int> numbers) { int biggestNum = numbers[0]; if(large_small)// finding largest number { for(int i=0; i<numbers.size();++i) { if(biggestNum < numbers[i]) biggestNum = numbers[i]; } } else// used to find smallest number { for(int i=0; i<numbers.size();++i) { if(biggestNum > numbers[i]) biggestNum = numbers[i]; } } return biggestNum; } /* Function Description: gets largest number */ int getNumbers(bool large_small) { int size; int tmp; vector<int> numbers; cout <<"Enter how many numbers to read."; cin >>size; //reads all numbers in for(int i=0; i < size; ++i) { cout <<(i+1)<<": ";// prompt with numbers entered cin >>tmp;// reads in number numbers.push_back( tmp ); // added number } return compare(large_small,numbers); } int main() { //attributes string str; int largestNum=0; int smallestNum=0; cout <<"Welcome to This appliction :)\n"<<endl;//Welcome message while(true)//never ending loop { //Options presented to cout <<"Please make a selection...."<<endl; cout <<"A - Find the largest number"<<endl; cout <<"B - Find the smallest number"<<endl; cout <<"C - Quit"<<endl; cin >>str;// reading in input if((str != "a") && (str != "A") && (str != "b") && (str != "B") && (str != "c") && (str != "C")) { cout <<"Invalid option..."<<endl; } if((str == "a") || (str == "A")) { cout <<"Largest size: "<< getNumbers(true) <<endl;//returns largest number } if((str == "b") || (str == "B")) { cout <<"Smallest size: "<< getNumbers(false) <<endl; } if((str == "c") || (str == "C")) exit(0);//exiting } return 0; //returning 0 to operating system saying all is good. }
Last edited by zippie; Nov 20th, 2007 at 9:30 am. Reason: Added attachment of file
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- Need Advice on for loops with vowels (Java)
- Loops (C++)
- Need some help with my do-while and while loops (Java)
- Help with loops (Java)
- merged:nesting loops (C++)
- help for program involving switch loops and file (C++)
Other Threads in the C++ Forum
- Previous Thread: problem with classes
- Next Thread: Help with atoi
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






