Going Crazy Over Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vegiitto
    New Member
    • Jul 2007
    • 12

    Going Crazy Over Program

    okay here is the task I must do...but I can't do it for the life of me...can any one of you figure this code out and show it to me?



    3. Modify the above program, so that if the number entered is lower than 10 a message, "That number is lower than 10" is printed. If the number entered is equal to 10, a message, "That number is equal to 10" is printed . If the number entered is higher than 10, a message "That number is valid" followed by the message "Done" is printed. You will need to use a while loop as well as an if-else statement.

    Sample input and output :
    Enter a number larger than 10 : 5
    That number is lower than 10
    Enter a number larger than 10 : 10
    That number is equal to 10
    Enter a number larger than 10 : 15
    That number is valid
    Done
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Vegiitto
    okay here is the task I must do...but I can't do it for the life of me...can any one of you figure this code out and show it to me?



    3. Modify the above program, so that if the number entered is lower than 10 a message, "That number is lower than 10" is printed. If the number entered is equal to 10, a message, "That number is equal to 10" is printed . If the number entered is higher than 10, a message "That number is valid" followed by the message "Done" is printed. You will need to use a while loop as well as an if-else statement.

    Sample input and output :
    Enter a number larger than 10 : 5
    That number is lower than 10
    Enter a number larger than 10 : 10
    That number is equal to 10
    Enter a number larger than 10 : 15
    That number is valid
    Done
    Please read the posting guidelines. It's very easy to get help here if you follow them.

    Comment

    • seforo
      New Member
      • Nov 2006
      • 60

      #3
      where do you get stuck? Read posting guidelines for this site please

      Comment

      • Vegiitto
        New Member
        • Jul 2007
        • 12

        #4
        Okay sorry...well the problem right now with my program is at random times (depending on the number I use) it will finish the program. The code looks okay but obviously is not...I'm fairly new to C++ and am not sure if my While loop is correct:

        cout << "Enter a number greater than 10: ";
        cin >> integer;
        while (integer == 10) {
        cout << "That number is equal to ten" << endl;
        cout << "Enter a number greater than 10: ";
        cin >> integer;

        And if my if else statement is either:

        cout << "Enter a number greater than 10: ";
        cin >> integer;
        if (integer < 10)
        {
        cout << "That number is too small" << endl;
        cout << "Enter a number greater than 10: ";
        cin >> integer;
        }
        else
        {
        cin >> integer;

        That is the whole of my code...except the if else comes before the while.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Vegiitto
          Okay sorry...well the problem right now with my program is at random times (depending on the number I use) it will finish the program. The code looks okay but obviously is not...I'm fairly new to C++ and am not sure if my While loop is correct:

          cout << "Enter a number greater than 10: ";
          cin >> integer;
          while (integer == 10) {
          cout << "That number is equal to ten" << endl;
          cout << "Enter a number greater than 10: ";
          cin >> integer;

          And if my if else statement is either:

          cout << "Enter a number greater than 10: ";
          cin >> integer;
          if (integer < 10)
          {
          cout << "That number is too small" << endl;
          cout << "Enter a number greater than 10: ";
          cin >> integer;
          }
          else
          {
          cin >> integer;

          That is the whole of my code...except the if else comes before the while.
          Please use code tags when posting code.
          You need to keep taking in input until the number is greater than 10 so the
          [CODE=cpp] cout << "Enter a number greater than 10: ";
          cin >> integer;[/CODE]

          Should go into the while
          [CODE=cpp]int number = 0;
          while(number < 10) {
          cout << "Enter a number greater than 10: ";
          cin >> number;
          //your if-else go here
          }[/CODE]

          Comment

          • seforo
            New Member
            • Nov 2006
            • 60

            #6
            Learn a proper use of while loop, also learn who to use break and continue statements. I have modified your code alittle bit
            [CODE=c++]
            cout << "Enter a number greater than 10: ";
            while (true) {
            cin >> integer;
            if(integer==10)
            {
            cout << "That number is equal to ten" << endl;
            cout << "Enter a number greater than 10: ";
            }
            else if(integer < 10)
            {
            cout << "That number is too small" << endl;
            cout << "Enter a number greater than 10: ";
            }
            else
            {
            cout<<"That Number is valid"<<endl;
            cout<<"Done"<<e ndl;
            break;
            }
            }
            [/CODE]

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by seforo
              Learn a proper use of while loop, also learn who to use break and continue statements. I have modified your code alittle bit
              [CODE=c++]
              cout << "Enter a number greater than 10: ";
              while (true) {
              cin >> integer;
              if(integer==10)
              {
              cout << "That number is equal to ten" << endl;
              cout << "Enter a number greater than 10: ";
              }
              else if(integer < 10)
              {
              cout << "That number is too small" << endl;
              cout << "Enter a number greater than 10: ";
              }
              else
              {
              cout<<"That Number is valid"<<endl;
              cout<<"Done"<<e ndl;
              break;
              }
              }
              [/CODE]
              Compare the structure of your solution to mine.
              Generally avoid the while(true) construct if you can.

              Comment

              • Vegiitto
                New Member
                • Jul 2007
                • 12

                #8
                Originally posted by r035198x
                Please use code tags when posting code.
                You need to keep taking in input until the number is greater than 10 so the
                [CODE=cpp] cout << "Enter a number greater than 10: ";
                cin >> integer;[/CODE]

                Should go into the while
                [CODE=cpp]int number = 0;
                while(number < 10) {
                cout << "Enter a number greater than 10: ";
                cin >> number;
                //your if-else go here
                }[/CODE]
                Okay I did this...it seemed to work for not stopping when I put a smaller number or the same...but when I put a bigger number it didn't work...will keep trying things but like I said I'm new to C++...but this was great, thanks for the help

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by Vegiitto
                  Okay I did this...it seemed to work for not stopping when I put a smaller number or the same...but when I put a bigger number it didn't work...will keep trying things but like I said I'm new to C++...but this was great, thanks for the help
                  Keeping trying things is the way to go. Only post when you're sure you've tried the best you can and help will always be available.

                  Comment

                  Working...