check out my first program

This is a discussion on check out my first program within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to write a program that reads three integers and determines and prints the largest number numerically ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    Unhappy check out my first program

    Hello,

    I am trying to write a program that reads three integers and determines and prints the largest number numerically in a group in which the values are not necessarily entered in numeric order using "if" statements. this is what i have so far. But my largest number does not print out to the screen. It just gives me random numbers.

    What am I doing wrong?




    Code:
    #include <iostream>
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    
    
    {
    	int number1, number2, number3, large;
    	cout << "Enter three numbers.\n";
    	cout << "First: ";
    	cin >> number1;
    	cout << "\nSecond: ";
    	cin >> number2;
    	cout << "\nThird: ";
    	cin >> number3;
    	cout << "\n";
    
    	if (number1 > number2) 
    		if (number1 < number3)
    		large = number1;
    
    	if (number1 < number2)
    		if(number2 < number3) 
    		large = number2; 
    
    	if (number1 < number3)
    		if(number3 < number2) 
    		large = number3;
    
    	cout << "Large: " << Large;
    	cout << "\n";
         
                    cin.get ();
    
    		return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This shouldn't compile, as "large" and "Large" are not the same thing. Also, it is possible to construct an input where large is never set (for instance, if you enter 8, 6, 4, then none of the cases apply).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    1>------ Build started: Project: Program_2, Configuration: Debug Win32 ------
    1>Compiling...
    1>Program_2.cpp
    1>d:\Program_2.cpp(37) : error C2065: 'Large' : undeclared identifier
    1>d:\Program_2.cpp(48) : fatal error C1075: end of file found before the left brace '{' at 'd:\Program_2.cpp(14)' was matched
    1>Build log was saved at "file://d:\Program_2\Debug\BuildLog.htm"
    1>Assignment_2 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    This shouldn't compile, as "large" and "Large" are not the same thing. Also, it is possible to construct an input where large is never set (for instance, if you enter 8, 6, 4, then none of the cases apply).


    so by removing the "Large/large" from the input it compiles using the integers given when the user inputs them? I don't understand.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CrKrJak View Post
    so by removing the "Large/large" from the input it compiles using the integers given when the user inputs them? I don't understand.
    No, that means you have to be able to type. Pick one: "large" or "Large". Now, use that one that you picked consistently, everywhere in the program.

    Once you have that fixed, then you can actually run the program. (You were never able to actually run this program, so I have no idea how you were getting "random output" -- must have been some other program you were running.) Once that happens, you will notice that you have a logic error, in that the if statements you have are Just Not going to give you the largest number.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    I am basically looking for the identifier to recognize the larger of the integers inputed by a user.

    If I were to remove the "Large/large" from the code it will only ask to input the numbers without giving me any output of the largest number entered. I would of assumed that by using the following code that ithe program translates the greater and lesser than of an integer.

    [code
    if (number1 > number2)
    if (number1 < number3)
    [/code]

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You logic is overly complicated.

    First, assume number1 is the largest:

    large = number1 ;

    Then, compare large against number 2:

    if (large < number2) large = number2 ;

    Then, do the same thing with 3, and you'll be done.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CrKrJak View Post
    If I were to remove the "Large/large" from the code it will only ask to input the numbers without giving me any output of the largest number entered.
    Extremely true. That's why you shouldn't remove it from the code. You just need to type it the same way each time. This is not hard.

    Quote Originally Posted by CrKrJak View Post
    I would of assumed that by using the following code that ithe program translates the greater and lesser than of an integer.

    Code:
    if (number1 > number2) 
    		if (number1 < number3)
    I have no idea why you would have assumed that. It doesn't even look right. Again, try it out with numbers like 8, 6, 4. Notice how number1 is bigger than number2, but number1 is not less than number3, so large will not get set. And then, even when you fix that, what about 7, 4, 7? Or even 7, 7, 7?

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Quote Originally Posted by Dino View Post
    You logic is overly complicated.

    First, assume number1 is the largest:

    large = number1 ;

    Then, compare large against number 2:

    if (large < number2) large = number2 ;

    Then, do the same thing with 3, and you'll be done.


    ok this works but if I use random numbers without the first number being number 1 then what>? Thats where I'm stuck

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Think a little bit yourself before posting, will you.
    What do you think will happen? Read the quote again and follow it in your head as you feed it different numbers.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    FAQ: C++ is case-sensitive.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    ok, I am using this model to enter random numbers at any given point. Out of three random numbers I do not want to set "large = number1" for the code mentioned above.

    I want to place the number in any of the 3 slots and compute the largest number based on the integers that were inputed. I fixed my code to look like this but I only get output of whatever integer I input as my first choice.

    Run this source as a .cpp and you'll see what I am talking about.




    Code:
    #include <iostream>
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    
    
    {
    	int number1, number2, number3, large;
    	cout << "Enter three numbers.\n";
    	cout << "First enter the largest number: ";
    	cin >> number1;
    	cout << "\nSecond enter a random number: ";
    	cin >> number2;
    	cout << "\nThird enter a random number: ";
    	cin >> number3;
    	cout << "\n";
    
    	if (number1 > number2) 
    		if (number1 < number3)
    		large = number1;
    
    	if (number1 < number2)
    		if(number2 < number3) 
    		large = number2; 
    
    	if (number1 < number3)
    		if(number3 < number2) 
    		large = number3;
    
    	cout << "The largest number is: " << number1 ;
    	cout << "\n";
         
                    cin.get ();
    
    		return 0;
    
    }

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    I don't want the largest number to be set as my first input. I want it to be random based on the integers inputed by the user.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by CrKrJak View Post
    but I only get output of whatever integer I input as my first choice.

    Code:
    	cout << "The largest number is: " << number1 ;
    And what do you expect?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
        if (number1 < number3)
            large = number1;
    Shouldn't that be the other way around?

    >> I don't want the largest number to be set as my first input. I want it to be random based on the integers inputed by the user.

    You can either set it to the lowest possible number (eg: std::numeric_limits<int>::max()) or the first number.


    It would actually be much simpler (not to mention much more useful) to use a loop, eg:

    1) Declare a variable 'largest' and set it to INT_MIN.
    2) Set up a loop to read input until the user enters some other 'sentinal' value.
    3) After reading a number in the loop, compare it to 'largest' and reset 'largest' as necessary.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 02:22 PM
  2. Replies: 7
    Last Post: 01-09-2009, 08:45 PM
  3. Suggestions for my Check Book program
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2008, 05:44 PM
  4. program to check hard disk transfer rate
    By shadow99er in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 04:04 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21