443,627 Members | 2,339 Online
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

# Need helping finding highest number from user input.

 P: 11 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 case A: cout << "How many numbers do you want to enter? "; cin >> limit; cout << "Limit = " << limit << endl;   cout "\nEnter number 1: " << endl;   cin >> num; max = num;  // I got max min from another post here. But i can't get it right still. min = num; for (counter = 1; counter <= limit; counter ++) {   cout << "Enter number " << counter + 1 << ": \n";   cin >> num; }  if...This is pretty much where I am lost.   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 Replies

 Expert Mod 5K+ P: 9,197 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

 Expert Mod 2.5K+ P: 4,677 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 case A: cout << "How many numbers do you want to enter? "; cin >> limit; cout << "Limit = " << limit << endl;   cout "\nEnter number 1: " << endl;   cin >> num; max = num;  // I got max min from another post here. But i can't get it right still. min = num; for (counter = 1; counter <= limit; counter ++) {   cout << "Enter number" << " " << counter + 1 << ": "\n";   cin >> num; }  if...This is pretty much where I am lost.   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

 P: 11 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

 P: 11 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

 Expert 100+ P: 1,764 You are only checking against last inputed num.Checking for the max should be inside your for loop. Savage Sep 11 '07 #6

 Expert 100+ P: 844 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 if (a > b) { }   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

 100+ P: 153 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                              cout << \nEnter number 1: ";                            cin >> num;                              min = num; //not using atm, only max for now                            max = num;                        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.                        {                         cout << "Enter number " << counter + 1 << ": \n";                         cin >> num;                        }                          if (max > num)                          cout << "The highest number is " << max << "max" << endl;                          else                            cout << "The highest number is " << num << "num" << endl;                    }             }           return 0; }   Strife Again, you have a lost quote! :) Expand|Select|Wrap|Line Numbers 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            ask for input (cin)            say something (cout)            conditional actions (if statement, while loops, for loops)            and compair ( == )   In the part I have above, you told me to:           ask for "number of inputs" (num)           the number of inputs are the same as max and min number (max = num)           get a list of numbers the size of input (for loop)           check to see if max is greater than the "number of inputs" (if statement)                  the two are equal, so I print out num.  I hope that helped and did not confuse you. Sep 11 '07 #8

 P: 11 Again, you have a lost quote! :) Expand|Select|Wrap|Line Numbers 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            ask for input (cin)            say something (cout)            conditional actions (if statement, while loops, for loops)            and compair ( == )   In the part I have above, you told me to:           ask for "number of inputs" (num)           the number of inputs are the same as max and min number (max = num)           get a list of numbers the size of input (for loop)           check to see if max is greater than the "number of inputs" (if statement)                  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

 Expert 2.5K+ P: 3,652 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

 P: 11 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. Yeah, I noticed that right after my last post. I would of edited it myself, but it wouldn't let me edit passed the last post for some reason. Sicarie edited the code spots for me though. Thank you Sicarie. strife Sep 12 '07 #11