Begining Programmer stuck!!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Begining Programmer stuck!!!

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    Post Begining Programmer stuck!!!

    I am stuck there is a problem in my code but I do not see where it is.
    The user is suppose to enter three numbers and then they are to be displayed in numerical order.
    This should be simple but I screwed it up. This is homework so I am looking for help, not someone to do my work.

    Code:

    #include <iostream>
    using namespace std;
    int main()

    {
    double num1, num2, num3, first, second, third; // variable declaration
    num1 = num2 = num3 = first = second = third = 0;
    cout << "Please Enter Three Numbers \n";

    cin >> num1 >> num2 >> num3;
    first = num1;
    if (num2 <= first)
    first = num2;
    third = num1;
    if (num3 <= first)
    first = num3;
    if (num1 >= first)
    if (num1 <= third)
    second = num1;
    if (num2 >= first)
    if (num2 <= third)
    second = num2;
    first = num3;
    third = num1;
    if (num3 >= first)
    if (num3 <= third)
    second = num3;
    cout << first << ", " << second << ", " << third << ".";

    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Begining Programmer stuck!!!

    Just a guess, but I think your if statements are missing {}.

  3. #3
    Join Date
    Feb 2009
    Posts
    7

    Unhappy Re: Begining Programmer stuck!!!

    no otherwise it would not compile and work halfway.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,273

    Re: Begining Programmer stuck!!!

    Quote Originally Posted by R.LFreeman View Post
    no otherwise it would not compile and work halfway.
    It would still compile and whose to say what is halfway?

    Btw, it's good programming practice to put braces around if statements.

    That way there isn't any ambiguity.

    For example, the following statement:

    Code:
    if (num2 <= first)
    first = num2;
    third = num1;
    Do you mean
    Code:
    if (num2 <= first)
    {
      first = num2;
    }
    third = num1;
    or
    Code:
    if (num2 <= first)
    {
      first = num2;
      third = num1;
    }

  5. #5
    Join Date
    Feb 2009
    Posts
    7

    Re: Begining Programmer stuck!!!

    Thank you both, I didn't mean to come across rude if I did. I was under the assumption from my instructor that if we left out {} that it would not compile. I tried you advice and it seems to have only made my problem worse.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,273

    Re: Begining Programmer stuck!!!

    Quote Originally Posted by R.LFreeman View Post
    Thank you both, I didn't mean to come across rude if I did. I was under the assumption from my instructor that if we left out {} that it would not compile. I tried you advice and it seems to have only made my problem worse.
    Post the new code inside code tags (i.e [ CODE] [/ CODE] minus the spaces).

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Begining Programmer stuck!!!

    Unfortunately until we know exactly what you did, and what your program did, and what you don't like about that, it's hard to suggest anything further.

  8. #8
    Join Date
    Feb 2009
    Posts
    7

    Re: Begining Programmer stuck!!!

    Here is my updated code that made it worse. The program is supposed to input three numbers from the user and then display them back in numeric order. After I compile it and run it it will duplicate numbers or give me zeros instead of the numbers input.

    Code:

    #include <iostream>
    using namespace std;
    int main()

    {
    double num1, num2, num3, first, second, third; // variable declaration
    num1 = num2 = num3 = first = second = third = 0;
    cout << "Please Enter Three Numbers \n";

    cin >> num1 >> num2 >> num3;
    first = num1;
    if (num2 <= first)
    {
    first = num2;
    third = num1;
    }
    if (num3 <= first)
    {
    first = num3;
    }
    if (num1 >= first)
    if (num1 <= third)
    {
    second = num1;
    }
    if (num2 >= first)
    if (num2 <= third)
    {
    second = num2;
    first = num3;
    }
    if (num3 >= first)
    if (num3 <= third)
    {
    second = num3;
    }
    cout << first << ", " << second << ", " << third << ".";

    return 0;
    }

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Begining Programmer stuck!!!

    Quite a complicated scheme.....which is why real programs wouldn't sort that way. (Imagine trying to write out that sort of code if you had to sort 1 million items rather than 3.) Still, I suppose it's informative as a starting point.

    Your approach isn't terribly principled even so, though. You may want to rethink it a bit.

    I presume when you have two if statements in a row, you're trying to express a logical "AND". You can just use:
    Code:
    if (num1 >= first && num1 <= third)
    As for why you're getting particular values out: Step into the code with your debugger and see why they are what they are.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,273

    Re: Begining Programmer stuck!!!

    Please use code tags.

    You still have the same problem:

    What behavior do you expect from this code
    Code:
     
    if (num1 >= first)
    if (num1 <= third)
    {
    second = num1;
    }
    if (num2 >= first)
    if (num2 <= third)
    {
    second = num2;
    first = num3;
    }
    Code:
     
    if (num1 >= first)
    {
      if (num1 <= third)
      {
        second = num1;
      }
    
      if (num2 >= first)
      {
        if (num2 <= third)
        {
          second = num2;
          first = num3;
        }
      }
    }
    or
    Code:
     
    if (num1 >= first)
    {
      if (num1 <= third)
      {
        second = num1;
      }
    }
    
    if (num2 >= first)
    {
      if (num2 <= third)
      {
        second = num2;
        first = num3;
      }
    }
    And there are several other possibilities. Btw, the whole reason for this exercise is to have you understand the difference between how the code executes differently when an if statement is enclosed within curly braces.

  11. #11
    Join Date
    Feb 2009
    Posts
    7

    Re: Begining Programmer stuck!!!

    Now, I am completely confused. I greatly thank you for the help. But I can't figure it out and now you guys have made me completely unsure of what I wanted! lol

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Begining Programmer stuck!!!

    If you had two numbers, there would only be two cases:
    A B
    B A

    When you introduce C, each of the two cases above can be expanded 3 ways:
    C A B
    A C B
    A B C

    C B A
    B C A
    B A C
    for a total of six possible orderings. Even if you wrote out the conditions required for each explicitly, eg (C <= B && B <= A) for C B A, it wouldn't be substantially more code than you have now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center