How do you do this in C++?

15 replies
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.
  • Profile picture of the author dealbert.net
    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.
    {{ DiscussionBoard.errors[2629107].message }}
    • Profile picture of the author nmarley
      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.
      {{ DiscussionBoard.errors[2629715].message }}
      • Profile picture of the author Sumit Menon
        Originally Posted by dealbert.net View Post

        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.
        Thank You... Let me take a look at that site.

        Originally Posted by nmarley View Post

        Why would you ask an internet marketing forum about your C++ programming homework?
        For three good reasons...

        1) I've been around longer and have seen C++ questions being answered here.

        2) The section of the thread I'm posting in is called, "Programming Talk". Allen Says says, with no ambiguity, "Ask your programming questions in this forum and some Warrior will know the answer.." over here on this thread - http://www.warriorforum.com/programm...iscussion.html

        3) And the most important one... Just because this happened in school, doesn't mean it's homework. Like I already mentioned, we finished our "work" early and were just tossed this question as an added thing to spend the hour and a half that was left. We are not gonna be graded on this one or anything. Our tutor too hasn't come up with a solution. She too have been working with us on this for the past 2 weeks.

        I asked because I wanted to know if this could be done cuz none of the people I've asked were able to solve this.

        And here's a question for you, why would you reply to a thread with a taunting question, when you really have nothing valuable to add?

        Peace,
        Sumit.
        {{ DiscussionBoard.errors[2630293].message }}
        • Profile picture of the author tscott_87
          Sumit,

          What type of data type (integer/string/character array/etc.) are you using to store the user's input? Let me know, and I can probably help you out. Shouldn't be too hard.

          Tim
          {{ DiscussionBoard.errors[2633989].message }}
  • Profile picture of the author kettlewell
    Originally Posted by Sumit Menon View Post

    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.
    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
    {{ DiscussionBoard.errors[2639196].message }}
    • Profile picture of the author Harrison Ortega
      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
      Signature
      NJ web design / NJ Web Designer. MY Wordpress portfolio. 10 years of HTML/CSS - 6 years developing professional Wordpress websites. Currently not available for services.
      {{ DiscussionBoard.errors[2639343].message }}
      • Profile picture of the author Sumit Menon
        Originally Posted by tscott_87 View Post

        Sumit,

        What type of data type (integer/string/character array/etc.) are you using to store the user's input? Let me know, and I can probably help you out. Shouldn't be too hard.

        Tim
        Integer constant....

        Originally Posted by kettlewell View Post

        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
        Like I said, it's just the beginning... They haven't taught us Strings yet. But, thanks anyway.

        Originally Posted by Harrison Ortega View Post

        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
        Thanks but it gives Error that digitsentered should only have a single character while compiling. Also, we have used integers instead of character. Any function to convert it into char like in Ruby?

        Thanks,
        Sumit.
        {{ DiscussionBoard.errors[2644821].message }}
  • Profile picture of the author tscott_87
    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.
    {{ DiscussionBoard.errors[2650352].message }}
    • Profile picture of the author Sumit Menon
      Originally Posted by tscott_87 View Post

      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.
      Thank you.. We still haven't figured it out yet either and my wouldn't give up!

      Sumit.
      {{ DiscussionBoard.errors[2661837].message }}
      • Profile picture of the author tscott_87
        I believe I have an answer.

        Everything in this reply assumes you must use one integer. This is also going to assume the platform you are using uses 32-bit integers. This is probably going to go over stuff you haven't learned yet, but I will try to elaborate...

        In the computer, an integer is stored in bits (0s and 1s). An integer is USUALLY 4 bytes (32 bits), but not always - it depends on the platform. Hopefully you know how to convert decimal numbers to binary... if not, just take it that 0 decimal is 32 0's, and 97 decimal is 27 0's then 1100001. The fundamental problem here is that 0 in binary is the same as 000 in binary... they are both 32 0's. Also, 97 = 097 in binary.

        Now let's suppose instead of the integer being 32 0's, we make it 32 1's. So set the integer to 4294967295.

        So how can we do this and get the user's input at the same time with one integer? Do bit shifting!

        So instead of setting the user's input directly to the integer, we shift it in, bit by bit (So if they put in zero, we will have 31 1's then one 0 at the end). Also, shift in a zero before you shift in the user's input.

        Let's see how this works. We have 32 1's. Then we shift in a zero to have 31 1's then one 0. Next is the user's input shifted in. So when you go to read out the user's input, you will start shifting from the left end (the 1st bit). Keep shifting those bits until you get to a 0. Then everything else will be the user's input.

        Hopefully, 000 will be shift in as three binary 0's rather than one binary 0.







        After writing all that, I still kind of think it won't work, because 000 will be represented as all 0's right as the user enters it in. Then you will not know if the user entered in one 0 or 3 0's. But... this at least gives you something to think about.
        {{ DiscussionBoard.errors[2669161].message }}
  • Profile picture of the author SmartWeb
    Originally Posted by Sumit Menon View Post

    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.

    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...
    {{ DiscussionBoard.errors[2669317].message }}
    • Profile picture of the author ram07
      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......
      {{ DiscussionBoard.errors[2670036].message }}
      • Profile picture of the author SmartWeb
        Originally Posted by ram07 View Post

        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......
        Really Nice solution. Very simple but very correct.
        {{ DiscussionBoard.errors[2670765].message }}
        • Profile picture of the author Tim Brownlaw
          Yep, the above will work ( as suggested by ram07 ) if the number is an integer constant... But if it's input by a user then it's a different matter.

          Not having touched C in any form or shape for over 15 years, I decided to sit down and come up with the code to do this.

          It's not pretty, and it's reminded me how strict C is as opposed to PHP - Yikes - I had to really dig into Google to solve the type issues etc.

          Anyway, I've learned some new tricks and if you are interested in the code - just PM me.

          I won't bother posting it in here...

          As I said - if it is a integer constant, meaning it's hardcoded into your script, then the previous solution suggested by ram07 will do the job.

          Cheers
          Tim
          Reborn C++ Novice
          {{ DiscussionBoard.errors[2670842].message }}
  • Profile picture of the author Tim Brownlaw
    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
    {{ DiscussionBoard.errors[2670149].message }}

Trending Topics