473,785 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need helping finding highest number from user input.

11 New Member
Hi,
This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot.

I have to figure out the highest number from a users input.

I give them a menu to choose from, and I am stuck on only one of the choices.

When this choice is picked I must allow the user to enter how many numbers he/she wants to enter; then I must figure out which number is the highest.
I have been working on this stupid problem for about 4 days now. I am just plain stuck on this problem. I'm sure it is something simple, but going through my chapter(s) multiple times, and trying multiple ways has not got me through it.

First, I use basic input/output to display the menu; then I use a switch statement for the choice selected. Once they pick, I provide a cin >> limit;, for their amount of numbers; ; then I ask for their first number(cin >> num;); then I begin a counter controlled for loop. Once in the loop I ask them for the rest of their numbers (cin >> num). I am pretty sure I am supposed to use an if or if else statement, but so far I keep messing it up. I cannot consistently get the highest number. All I really need help with is finding the highest number out of say, 5 numbers inputted. Like I said I have most of the work done on my own, just this one little thing is holding me back. How do I accurately and consistently find the correct high number?

here is the code I have trying to get the highest number:
Expand|Select|Wrap|Line Numbers
  1. case A:
  2. cout << "How many numbers do you want to enter? ";
  3. cin >> limit;
  4. cout << "Limit = " << limit << endl;
  5.  
  6. cout "\nEnter number 1: " << endl;
  7.  
  8. cin >> num;
  9. max = num;  // I got max min from another post here. But i can't get it right still.
  10. min = num;
  11. for (counter = 1; counter <= limit; counter ++)
  12. {
  13.   cout << "Enter number " << counter + 1 << ": \n";
  14.   cin >> num;
  15. }
  16.  if...This is pretty much where I am lost.
  17.  
If I do if (max > num), sometimes I get the first input as highest, sometimes the last input as highest, and sometimes I get the correct highest. I don't understand why.

If anyone can see what I am doing wrong or give me some hints as to how to figure this out please let me know.

Thanks,
strife
Sep 11 '07 #1
10 4376
weaknessforcats
9,208 Recognized Expert Moderator Expert
Have you written the logic steps on paper??

Like: If the number entered is greater than the current maximum, replacement the maximim with the current number.

It's always easier to get code working from a spec rather than writing code and fiddling with it until it sort of works.
Sep 11 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
Hi,
This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot.

I have to figure out the highest number from a users input.

I give them a menu to choose from, and I am stuck on only one of the choices.

When this choice is picked I must allow the user to enter how many numbers he/she wants to enter; then I must figure out which number is the highest.
I have been working on this stupid problem for about 4 days now. I am just plain stuck on this problem. I'm sure it is something simple, but going through my chapter(s) multiple times, and trying multiple ways has not got me through it.

First, I use basic input/output to display the menu; then I use a switch statement for the choice selected. Once they pick, I provide a cin >> limit;, for their amount of numbers; ; then I ask for their first number(cin >> num;); then I begin a counter controlled for loop. Once in the loop I ask them for the rest of their numbers (cin >> num). I am pretty sure I am supposed to use an if or if else statement, but so far I keep messing it up. I cannot consistently get the highest number. All I really need help with is finding the highest number out of say, 5 numbers inputted. Like I said I have most of the work done on my own, just this one little thing is holding me back. How do I accurately and consistently find the correct high number?

here is the code I have trying to get the highest number:
Expand|Select|Wrap|Line Numbers
  1. case A:
  2. cout << "How many numbers do you want to enter? ";
  3. cin >> limit;
  4. cout << "Limit = " << limit << endl;
  5.  
  6. cout "\nEnter number 1: " << endl;
  7.  
  8. cin >> num;
  9. max = num;  // I got max min from another post here. But i can't get it right still.
  10. min = num;
  11. for (counter = 1; counter <= limit; counter ++)
  12. {
  13.   cout << "Enter number" << " " << counter + 1 << ": "\n";
  14.   cin >> num;
  15. }
  16.  if...This is pretty much where I am lost.
  17.  
If I do if (max > num), sometimes I get the first input as highest, sometimes the last input as highest, and sometimes I get the correct highest. I don't understand why.

If anyone can see what I am doing wrong or give me some hints as to how to figure this out please let me know.

Thanks,
strife
Hmm, I have a few issues with this, but I'm going to nitpick a little first, hopefully you'll see why. In the above, line 13. If you notice, everything after it is greyed out. This is because you have an extra set of quotes (right before the \n). Your program won't compile until you fix that.

Secondly, you're using a case statement with letters? Are you sure you want to do that? I can imagine C/C++ being the robust languages they are taking those, compiling them, and using them, but I don't think that's the behavior you are looking for. Did you try to create/compile 'stub' programs (ie the general framework of your program, but with no implementation to make sure it compiles the way you expect it to)? I would recommend doing it in that case, my guess would be that the compiler - if you try to compile the code you posted (if you don't have 'case A' then this might not be an issue) - is converting A into something else, and this condition will rarely, if ever, be met.

A slight step back - is this an entire case? Ie - will you have to replicate the logic of finding the max for each case? If you are just using the case to set 'limit' why not put all your cases at once, and then only duplicate the logic once? Like I said, I'm not too sure about this, as you just posted a snippett, it's just a suggestion.

And as soon as I get out of my meeting, I'll tackle the 'max' function.
Sep 11 '07 #3
strife
11 New Member
Have you written the logic steps on paper??

Like: If the number entered is greater than the current maximum, replacement the maximim with the current number.

It's always easier to get code working from a spec rather than writing code and fiddling with it until it sort of works.
well I kind of sort of did. I tried a couple of times and I am just not getting it.
I am still just a beginner. I am in the first level programming class. I have been doing fine up till this point. The control structures, although easy, are at the same time hard. The if, if...else, and switch statements were easier than the loops for me.
I guess I'll try to get it on paper again, but all I do on paper is write what the code should look like to me, but I will do it the way you mentioned.

Strife
Sep 11 '07 #4
strife
11 New Member
Hmm, I have a few issues with this, but I'm going to nitpick a little first, hopefully you'll see why. In the above, line 13. If you notice, everything after it is greyed out. This is because you have an extra set of quotes (right before the \n). Your program won't compile until you fix that.

Secondly, you're using a case statement with letters? Are you sure you want to do that? I can imagine C/C++ being the robust languages they are taking those, compiling them, and using them, but I don't think that's the behavior you are looking for. Did you try to create/compile 'stub' programs (ie the general framework of your program, but with no implementation to make sure it compiles the way you expect it to)? I would recommend doing it in that case, my guess would be that the compiler - if you try to compile the code you posted (if you don't have 'case A' then this might not be an issue) - is converting A into something else, and this condition will rarely, if ever, be met.

A slight step back - is this an entire case? Ie - will you have to replicate the logic of finding the max for each case? If you are just using the case to set 'limit' why not put all your cases at once, and then only duplicate the logic once? Like I said, I'm not too sure about this, as you just posted a snippett, it's just a suggestion.

And as soon as I get out of my meeting, I'll tackle the 'max' function.
That was supposed to be a space. I thought it looked wrong, :). I fixed it and just made it "Enter number ". That is what you meant right? CORRECTION:
I noticed what you meant after editing the wrong item. It is fixed now.

The case works absolutely fine. I use char choice; and I give a menu of "select A, B, or C, so this is why I am using a switch statement with char cases. I don't think the case is the problem, but I AM a beginner still, so I could be wrong. It seems though, that the case is fine. The only problem I have is figuring out how to display the highest number correctly.

Well it was suggested to start with one menu option at a time, so I haven't done 'stub' programs. I still have two more options. Well, its more like one. I have the easier option done (Exit option ((char) (choice != menu choice)). Now I have two more options. I have just been working on this one all week, and I know it will be something simple, that makes me feel like a tard, but I just can't get it to work. If you really want I can post all my code. If I don't turn this assignment in by tomorrow I don't get any credit anyway, so if my teacher finds it and gives me 0, big deal I guess. Tomorrow (Wednes 9/12), he is going to post the anwers, and give 0 credit to those who haven't submitted it.

::Full code removed::

OK. This is what I have for A so far. I know the if...else statement is wrong, it is just where I stopped so far. I also need to make the menu re-display itself after successfully executing A, or B. I haven't put a lot of time in B yet, but I do know it will be a sentinel controlled loop. Main thing is A first. I just cannot get it right, and its driving me crazy, but at least I am new, so it ok to suck right now hehe.
Anyway, if you can help me at all it will be greatly appreciated, you don't need to answer it for me since its homework, just lead me in the right direction if possible.

Thanks again,
Strife
Sep 11 '07 #5
Savage
1,764 Recognized Expert Top Contributor
You are only checking against last inputed num.Checking for the max should be inside your for loop.

Savage
Sep 11 '07 #6
ilikepython
844 Recognized Expert Contributor
That was supposed to be a space. I thought it looked wrong, :). I fixed it and just made it "Enter number ". That is what you meant right? CORRECTION:
I noticed what you meant after editing the wrong item. It is fixed now.

The case works absolutely fine. I use char choice; and I give a menu of "select A, B, or C, so this is why I am using a switch statement with char cases. I don't think the case is the problem, but I AM a beginner still, so I could be wrong. It seems though, that the case is fine. The only problem I have is figuring out how to display the highest number correctly.

Well it was suggested to start with one menu option at a time, so I haven't done 'stub' programs. I still have two more options. Well, its more like one. I have the easier option done (Exit option ((char) (choice != menu choice)). Now I have two more options. I have just been working on this one all week, and I know it will be something simple, that makes me feel like a tard, but I just can't get it to work. If you really want I can post all my code. If I don't turn this assignment in by tomorrow I don't get any credit anyway, so if my teacher finds it and gives me 0, big deal I guess. Tomorrow (Wednes 9/12), he is going to post the anwers, and give 0 credit to those who haven't submitted it.

I guess I will just throw the whole thing in here, or what I have so far. Here is it:
How do you think you have to find the highest number? If you had to find the bigger one of two numbers, that would be easy:
Expand|Select|Wrap|Line Numbers
  1. if (a > b)
  2. {
  3. }
  4.  
But you have to do it for more than two numbers. So you need to check every iteration for the bigger one of two numbers. If one of the numbers is bigger you need to "save" it somehow. If not, don't do anything. Do you know what to do from here?
Sep 11 '07 #7
kreagan
153 New Member
That was supposed to be a space. I thought it looked wrong, :). I fixed it and just made it "Enter number ". That is what you meant right? CORRECTION:
I noticed what you meant after editing the wrong item. It is fixed now.

I guess I will just throw the whole thing in here, or what I have so far. Here is it:
Expand|Select|Wrap|Line Numbers
  1.  
  2.                            cout << \nEnter number 1: ";
  3.                            cin >> num;
  4.  
  5.                            min = num; //not using atm, only max for now
  6.                            max = num;
  7.                        for (counter = 1; counter <= limit - 1;counter ++)//I get 1 extra number because of  cin >> num; twice, so limit - 1, is how I fixed it, hope thats ok.
  8.                        {
  9.                         cout << "Enter number " << counter + 1 << ": \n";
  10.                         cin >> num;
  11.                        }
  12.  
  13.                        if (max > num)
  14.                          cout << "The highest number is " << max << "max" << endl;
  15.  
  16.                        else
  17.                            cout << "The highest number is " << num << "num" << endl;
  18.                    }
  19.             }
  20.           return 0;
  21. }
  22.  
Strife
Again, you have a lost quote! :)

Expand|Select|Wrap|Line Numbers
  1. Secondly, I would suggest writing down psuedo code. Pretend I'm an idiot and you give me a "special" assignment. You want me to ask people to enter X numbers of numbers and I must tell them the highest! I can only do
  2.            ask for input (cin)
  3.            say something (cout)
  4.            conditional actions (if statement, while loops, for loops)
  5.            and compair ( == )
  6.  
  7. In the part I have above, you told me to:
  8.           ask for "number of inputs" (num)
  9.           the number of inputs are the same as max and min number (max = num)
  10.           get a list of numbers the size of input (for loop)
  11.           check to see if max is greater than the "number of inputs" (if statement)
  12.                  the two are equal, so I print out num. 
I hope that helped and did not confuse you.
Sep 11 '07 #8
strife
11 New Member
Again, you have a lost quote! :)

Expand|Select|Wrap|Line Numbers
  1. Secondly, I would suggest writing down psuedo code. Pretend I'm an idiot and you give me a "special" assignment. You want me to ask people to enter X numbers of numbers and I must tell them the highest! I can only do
  2.            ask for input (cin)
  3.            say something (cout)
  4.            conditional actions (if statement, while loops, for loops)
  5.            and compair ( == )
  6.  
  7. In the part I have above, you told me to:
  8.           ask for "number of inputs" (num)
  9.           the number of inputs are the same as max and min number (max = num)
  10.           get a list of numbers the size of input (for loop)
  11.           check to see if max is greater than the "number of inputs" (if statement)
  12.                  the two are equal, so I print out num. 
I hope that helped and did not confuse you.

Lost quote, because I was just trying to get it down for you guys. Is there a way to make it show color coded text while posting? I'm doing it all as if it were in notepad, so it makes it a little harder.

I'll try pseudo code and see if it helps me.

Thanks for the help,
strife
Sep 11 '07 #9
Ganon11
3,652 Recognized Expert Specialist
The color coded portions of text above were using [code=cpp] tags. When entering the text for your posts, there is a # button above the text area. Click and drag over your code text and hit that # button. Now, go back to the top of your code and, right after it says CODE, type in =cpp, so it looks like [code=cpp]. This will make your code look all fancy, like you see in your previous posts.
Sep 11 '07 #10

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

Similar topics

4
1990
by: david.w.anderson1 | last post by:
This is a homework assignment. The code works but does not "catch" all the improper input. The requirement is to be able to accept one input currency, which is error checked as a valid entry, and then display its equivalency in US dollars. The program does detect when a letter is entered as the 1st digit. I need the program to detect when a number-letter combination is entered (example 1a) and show the error.
3
4412
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
15
4644
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
4
2760
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself. the description is the following: the program will read a text file that contains historical price of a stock. The program will allow users to query...
0
3963
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
1
5875
by: coolindienc | last post by:
I got this program running and gives me result that I want (actually not yet). Now I want to find out the lowest and highest numbers I entered. Andy advise??? number = int (input ("Enter a positive number that is below 126: ")) if number > 125: print "The number entered has to be 125 or lower.\n" number = input ("Enter a positive number that is below 126: ") else: total = 0 total += number
0
972
by: Jschraepf | last post by:
I am creating a fairly simple web app but I am having some trouble with the database access at this point. I need to select a single row which is the most current (has the highest record autonumber). . It is selecting a variable table from a database and works fine if I tell it what to select, but I need to know how I can tell it to pick the highest number. The snippet of code is below. "HIGHEST NUMBER" is obviously where i need to know the...
3
4487
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
4
1793
by: Yonih | last post by:
So I am trying to get this Calculator to work. It needs to take in a vaule, and select a shipping Everythin works great except the shipping part. I need it to take the shipping value and add it to the "Downpayment" and also the "Total amount paid" Example: item cost $20.00 , $8.50 shipped selected, Payment 1 = 12 + 8.50 so $20.50 Payments 2-5 = $2.00 Final Payment = $28.50
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.