By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Output the Highest Number in C

P: 3
Hi guys first post woooo!....can believe some of you guys are doing this for a living with things as complex as software...while I cant get past outputting the highest number a user enters...LOL

But anyway, my teech wants me to write a program where the user enters as many integers as s/he wants until s/he inputs 0.

I think I have the concept down, but I cannot get it to work and I am getting really tired of trying to correctly compile it, the textbook is no help, and the teacher didnt really give any hints on how to solve it...so I come here!!!
Here is the Code I have so far:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. main ()
  6. {
  7.     int int1, int2, int3;
  8.  
  9.     printf("This program find the largest integer in a list.\n");
  10.     printf("Enter 0 to signal the end of the list.\n");
  11.     while (TRUE)
  12.     {
  13.         printf("? ");
  14.         int1=GetInteger();
  15.  
  16.         printf("?");
  17.         int2=GetInteger();
  18.  
  19.  
  20.         if (int1 > int2)
  21.         {    int3 = int1;
  22.             int3=GetInteger();
  23.         }
  24.  
  25.         if (int2 > int1)
  26.             int3= int2;
  27.                     int3=GetInteger();
  28.  
  29.         if (int1 == 0) break;
  30.         if (int2 == 0) break;
  31.  
  32.     }
  33.         printf("The largest value is ", int3);
  34.         }
  35.  
What I thought I was doing was the user would enter at least two numbers in, and the computer would compare them and store the highest number in int3. But when the program compiles...I have to hit 0 like 3 or 4 times for it to show "The Largest value is " and then instead of showing "int3", it is just blank... What am I doing wrong??? Any tips would be helpful or maybe a hint at what type of code to use to get it to work.
THANKS GUYS
Sep 20 '07 #1
Share this Question
Share on Google+
5 Replies


rhitam30111985
100+
P: 112
Hi guys first post woooo!....can believe some of you guys are doing this for a living with things as complex as software...while I cant get past outputting the highest number a user enters...LOL

But anyway, my teech wants me to write a program where the user enters as many integers as s/he wants until s/he inputs 0.

I think I have the concept down, but I cannot get it to work and I am getting really tired of trying to correctly compile it, the textbook is no help, and the teacher didnt really give any hints on how to solve it...so I come here!!!
Here is the Code I have so far:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. main ()
  6. {
  7.     int int1, int2, int3;
  8.  
  9.     printf("This program find the largest integer in a list.\n");
  10.     printf("Enter 0 to signal the end of the list.\n");
  11.     while (TRUE)
  12.     {
  13.         printf("? ");
  14.         int1=GetInteger();
  15.  
  16.         printf("?");
  17.         int2=GetInteger();
  18.  
  19.  
  20.         if (int1 > int2)
  21.         {    int3 = int1;
  22.             int3=GetInteger();
  23.         }
  24.  
  25.         if (int2 > int1)
  26.             int3= int2;
  27.                     int3=GetInteger();
  28.  
  29.         if (int1 == 0) break;
  30.         if (int2 == 0) break;
  31.  
  32.     }
  33.         printf("The largest value is ", int3);
  34.         }
  35.  
What I thought I was doing was the user would enter at least two numbers in, and the computer would compare them and store the highest number in int3. But when the program compiles...I have to hit 0 like 3 or 4 times for it to show "The Largest value is " and then instead of showing "int3", it is just blank... What am I doing wrong??? Any tips would be helpful or maybe a hint at what type of code to use to get it to work.
THANKS GUYS

why are u using getinteger for a simple input ? why not a simple scanf?
anyhow assuming Getinteger is for inputting.. u are inputting int3 again and again in every bock where u r assigning int3 = int1 or i the next block (int3 = int 2)
remove int 3 = getinteger from after all the if conditions

only then int 3 will contain the larger value of the two
Sep 20 '07 #2

P: 3
i removed int3 from the if statements and still get the same problem.....damnit lol
Sep 20 '07 #3

rhitam30111985
100+
P: 112
probably u r making some other mistake... for simple programs like these..
just write simple codes....

spoonfeeding isn't allowed

above is known as a ternery operator... try using google for further basic queries .. helps a lot...
Sep 20 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
When printing the largest number, you forgot to add in a %d for int3, so it is never printed.
Sep 20 '07 #5

P: 3
When printing the largest number, you forgot to add in a %d for int3, so it is never printed.
oh my lanta... Im dumb, I was looking at the screen for so long I just thought it was a math error. But it could as simple as a %d. Thanks for pointing that out to me.
After I get out of work and school, Ill go home and fix that and hopefully everything will be alright.

Thanks Rhitham I wish I could use that, but the professor would look at the code and be like, " Um... we didnt do that in class, NO CODE FOR YOU ONE YEAR!" he might not say that but you get the point.

Thanks guys
Sep 20 '07 #6

Post your reply

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