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

07-04-2007

dwks

Sure, you could just read four variables in -- two strings and two numbers. Then use something like

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.

07-04-2007

Rahiiyja

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.

07-04-2007

dwks

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.

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.

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

07-04-2007

Rahiiyja

Thank you very much for your reply. I will read it more thoroughly tomorrow.

07-04-2007

MWAAAHAAA

Have a look at switch statements

07-05-2007

Rahiiyja

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 ;-)

07-05-2007

Rahiiyja

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?

07-05-2007

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.

07-05-2007

Salem

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.

07-05-2007

Rahiiyja

Quote:

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?

07-05-2007

Salem

I dunno, maybe you can post a summary of what you've learnt so far...

07-05-2007

laserlight

Quote:

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.

Quote:

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.

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?