Thread: Need help with a tiny program

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Need help with a tiny program

    Hi.
    I am trying to make a simple program, that receives two values from the user, and then prints a comment concerning the difference between these two values.

    I was thinking something like this:

    Code:
    Please input name 1: ok 
    Please input name 2: sure
    Enter the value of ok: 3
    Enter the value of sure: 5
    
    sure is larger than ok, by 2.
    To add more dimension, I was thinking that the program would react if the difference was very big between the two variables.

    I.e.
    Code:
    Please input name 1: first
    Please input name 2: second
    Enter the value of first: 500
    Enter the value of second: 3
    
    first is much larger than second, by 497
    Any idea or tips how to achieve this? I reason that a couple of 'if' would do the trick, but is there any other way that you could think of?
    I am a beginner in C++, so if you would avoid too complicated solutions it would be greatly appreciated. I've read 300 pages in Stephen Prata's third edition though, so I'm not completely ignorant, but more advanced things I have no knowledge of.

    Thanks in advance

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sure, you could just read four variables in -- two strings and two numbers. Then use something like
    Code:
    if difference > 100 print "over a hundred"
    if difference > 1000 print "over a thousand"
    if difference > 1000000 print "over a million"
    Have you tried to do this, or is it still in the planning stage? Also, why not have the user input a number followed by its name at the same time?
    Code:
    Enter the first number followed by its name: 3 first
    Enter the second number followed by its name: 6 second
    ...
    Or the other way around, of course. Just a thought.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    dwks:

    Thanks for your reply.

    Good idea to merge strings and numbers, that way I can reduce the number of lines a bit at least.

    Concerning the if-sentences I am more curious in alternate ways of solving this. This is a question in a basic C++ course I am reading at the moment, we are supposed to solve this without any if's. It's not part of an exam, but rather a "test yourself" question that I'm stuck at, therefore I am of course not asking for a complete code, but rather some tips or ideas that might help.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, I see. Well, there are several ways to do conditional code without using if statements.

    The first way is to use the short-circuting feature of boolean logic. As you may be aware, with x && y, first x is tested. If it's false, the whole expression is false, no matter what the value of y, so y isn't even evaluated and the expression is given a value of false. If x is true, then y is evaluated. The expression is given a true value if y is true.

    Usually one uses the value of the expression, like this:
    Code:
    if(x && y) do_something();
    But you can use it just like this:
    Code:
    cout << "the difference is";
    difference > 100 && cout << "much ";
    cout << "greater than...";
    The easier way is to use the conditional operator, which is actually designed for this sort of thing. It's C and C++'s only tertiary operator, which means it takes not one or two but three expressions. ! takes one: if(!x). && takes two: if(x && y). But ?: takes three.

    The syntax for the conditional operator is this:
    Code:
    (test) ? (true-expression) : (false-expression);
    First, test is evaluated. If it's true, true-expression is evaluated; otherwise, it's false, and false-expression is evaluated.

    Usually the ?: operator is used in place of code like this:
    Code:
    if(x > 100) y = 100;
    else y = x;
    where it's actually simpler to go
    Code:
    y = x > 100 ? 100 : x;
    But you can use it like this:
    Code:
    cout << "difference is " << (difference > 100 ? "much " : "") << "greater than ...";
    As you can see, the conditional operator works best when you have both a true-expression and a false-expression. If you don't you might as well use a boolean operator like && or ||.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Thank you very much for your reply. I will read it more thoroughly tomorrow.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Have a look at switch statements

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    dwks:
    That was a very informative reply and I understand how it works. We are not allowed to use this yet in the course though, since we must stick to what we've learned up to this point.

    If anyone wants to help me with this, I can write down the things that we've read in the course up until now:
    Basic functions
    Basic data-types
    Arithmetic operators
    Vectors
    Strings
    Structures
    Enumerations
    for, while, do while

    Where should I begin when making this program?
    I figure that both the names and their values should be stored in structures, and that's about as far as I've come.
    I also see the need of something that works like an 'if', but as I said we are not allowed to use 'if', conditional operator nor switch statements. Is it maybe possible to make a loop that works like 'if'?

    Any help is appreciated ;-)

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    After some consideration, it strikes me that a for-loop could be used as an 'if'.
    For example:

    Code:
    for (;x>y;;)
    {cout << "x is bigger than y"}
    
    for (;x<y;;)
    {cout << "y is bigger than x"}
    
    for (;x==y;;)
    {cout << "x and y are even"}
    Shouldn't this be possible, or will it end in neverending loops?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would end up with an infinite loop unless you used a break:
    Code:
    for (; x > y ;)
    {
        cout << "x is bigger than y";
        break;
    }
    Of course, the same trick can be used with a while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well in the absence of anything inside the loop to modify the variables in question, either nothing will happen or it will loop forever.

    Try it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by laserlight View Post
    You would end up with an infinite loop unless you used a break:
    Code:
    for (; x > y ;)
    {
        cout << "x is bigger than y";
        break;
    }
    Of course, the same trick can be used with a while loop.
    I see. Well, we are not allowed to use break yet. ;-)
    Any other ideas how to solve this?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I dunno, maybe you can post a summary of what you've learnt so far...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I see. Well, we are not allowed to use break yet. ;-)
    Any other ideas how to solve this?
    Make a for loop that runs no more than one iteration.

    I dunno, maybe you can post a summary of what you've learnt so far...
    Rahiiyja already did that, actually

    Wait, are you saying that you cannot use boolean operators at all? If so, I think what I suggested also does not work.
    Last edited by laserlight; 07-05-2007 at 09:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    do {
      // stuff
    } while (false);
    Edit: Never mind. I forgot about the condition.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by Salem View Post
    I dunno, maybe you can post a summary of what you've learnt so far...
    Here's a longer list of what I've learnt (roughly):
    using header-files
    functions
    namespaces
    cin
    cout
    using variables
    data-types
    member functions
    const keyword
    arithmetic operands
    vectors
    using strings (c-string and string-class)
    structures
    union datatype
    enum
    for, while, do while and related operands

    Quote Originally Posted by laserlight View Post
    Make a for loop that runs no more than one iteration.


    Wait, are you saying that you cannot use boolean operators at all? If so, I think what I suggested also does not work.
    Sounds reasonable.
    Concerning the usage of boolean operators though, I am not certain if that is allowed. We've read about the bool datatype at least, maybe you can give an example of using boolean operators in this case?
    Last edited by Rahiiyja; 07-05-2007 at 09:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. tiny error with C Program
    By JohnMayer in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 03:09 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM