How do you do this in C++?

by 15 replies
19
Okay so we finished the practicals way too early the other day and our professor gave the two of us an extra assignment.

We are still learning the basics of C++ and we had written a program to print three digits entered, in figures. She told us to modify the program so that, when the user enter "000" or "097" it gives output "Not A Valid Three Digit Number." and if he enters "0" or "97" the output would be "Not a Three Digit Number."

It's been 2 weeks and she too hasn't come up with a solution.. lol. Can this be done?

Sumit.
#programming
  • Why can't you put it on StackOverflow.com? For sure, your questions will be flooded with hundreds of replies. The community behind SO is amazing.
    • [ 1 ] Thanks
    • [1] reply
    • Why would you ask an internet marketing forum about your C++ programming homework?

      I might be able to understand if it was php, but... it just seems inappropriate for a number of reasons.
      • [1] reply
  • Assuming that this is a string (assumed because user is entering data), you should be able to use the STL functions to 1) check length. if not equal to 3, return error message. 2) if length is 3, check if first "digit" is 0, if so, error. then check 2nd "digit" for 0, if so, error, and same for 3rd digit. ELSE - we have a 3 digit number

    The key here is the STL functions to check for string length, and to check substrings for values.

    Sorry, I don't remember the exact functions you'll need, but any of the online refs should have it, or your books, etc.

    BTW - in plain C, you can do the same thing using the strlen() and substr() functions...

    Hope that helps...

    Matt
    • [ 1 ] Thanks
    • [1] reply
    • Just a basic nested if statements will do it.

      *note. Replace the (backslash) with the real sign since the BBcode is stripping it out.

      Code:
      #include <iostream.h>
      int main ()
      {
       char digitsentered;
      
      cout << "Enter a Number of Digits: ";
      cin >> digitsentered;
      
      if (digitsentered =='000')
         Cout << "Not a Valid Three Digit Number. (backslash)n";
        
         else if (digitsentered =='097')
         Cout << "Not a Valid Three Digit Number. (backslash)n";
      
         else if (digitsentered =='0')
         Cout << "Not a Three Digit Number. (backslash)n";
      
         else if (digitsentered =='97')
         Cout << "Not a Three Digit Number. (backslash)n";
      
      else
         Cout << "You've entered a valid Digit Number. (backslash)n";
      
      return 0;
      }
      Harrison
      • [ 1 ] Thanks
      • [1] reply
  • Assuming a user's input is an integer without checking that it actually is an integer is not a safe assumption, but let's say the user won't try to input incorrect data.

    You can make it where the input is put into three integers, something similar to this...

    int a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;

    Then you would know the first integer entered is a, second is b, third is c. Then do if statements on each.

    If you have to make all the input go to one integer, you might not be able to do this. I haven't tested it, but I want to say if you put 097 into an integer, it will be stored as "97" and not "097".

    Now you could check a three digit number by dividing by an integer 100 and then checking the result. If it is 0 (I believe divide by integer always takes the floor of the result, so 0.97 = 0 for integer division. If that is not true, take the floor of the result.), then it is less than 100. You could do an if statement for 0 or 97, but the problem is 000 = 00 = 0 and 097 = 97. So without three integers, I'm not sure you could have different output statements for those different cases.
    • [ 1 ] Thanks
    • [1] reply
    • Thank you.. We still haven't figured it out yet either and my wouldn't give up!

      Sumit.
      • [1] reply

  • Well, coming straight to solution..

    you need to use string to take the input number. this way you can check the length (using strlen() ) , once you find the length is 3, check for the 1st character of the string. if its character '0', display its not a valid 3 digit number.
    If the 1st character is not a '0', so its a valid 3 digit number, now you need to convert this 3 digit string to some integer, try using some inbuild functions like atoi()
    thats it...
    • [1] reply
    • Hi,

      This will be silly but try this one....

      The least three digit number is 100 and max number is 999....Ok..

      Now, once you enter a 3 digit number (000 or 001 or 099), add it with 900....

      Now check the answer is greater than or equal to 1000...

      If it is so then it must be a valid 3 digit number else it will be not a 3 digit number...

      Thanks......
      • [ 2 ] Thanks
      • [1] reply
  • Hi Sumit,

    This following is in pseudo code, you can figure out the syntax to use...

    Sounds like two tests are needed.

    1. Test the initial input to ensure it represents numerical characters only before you proceed with the other tests.
    If not numerical - then display the message - "Please enter digits only [100-999]" or something similar and exit

    Else

    2.count the number of characters ( look up how to do that and mau have to typecast it to a string).
    If the character count is == 3 then test to see if it's in the range of 100 to 999 ( after you typecast the input to an int - it's been validated as a numerical number already)

    I think that covers all the bases!

    The problem with programming is not learning the syntax but figuring out the logic behind the problem.. In most cases anyways.

    This is just one way to do it.

    How to actually implement this in C++ should be in your book/ lessons etc or use good ole Google to figure out how to implement it.

    Learning this stuff is best done by doing it yourself... Hope that gives you something to try out.


    Cheers
    Tim
    • [ 1 ] Thanks

Next Topics on Trending Feed

  • 19

    Okay so we finished the practicals way too early the other day and our professor gave the two of us an extra assignment. We are still learning the basics of C++ and we had written a program to print three digits entered, in figures. She told us to modify the program so that, when the user enter "000" or "097" it gives output "Not A Valid Three Digit Number." and if he enters "0" or "97" the output would be "Not a Three Digit Number."