# Thread: Need help with a tiny program

1. ## 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
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
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.

2. 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.

3. ## dwks:

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. 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 ||.

6. Have a look at switch statements

7. 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. 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. 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.

10. 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.

11. Originally Posted by laserlight
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. I dunno, maybe you can post a summary of what you've learnt so far...

13. 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...

Wait, are you saying that you cannot use boolean operators at all? If so, I think what I suggested also does not work.

14. Code:
```do {
// stuff
} while (false);```
Edit: Never mind. I forgot about the condition.

15. Originally Posted by Salem
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):
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

Originally Posted by laserlight
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?