-
Okay, I just came back from a quick shower, and the idea came to me just as I started showering :)
Basically, I was trying to find a way to loop 0 or 1 times, without having to use a control variable other than x and y. For example, this works:
Code:
while (x > y)
{
cout << "x is bigger than y";
x = 0;
y = 1;
}
Unfortunately, as soon as it hits the while (x < y) loop, it will print again, erroneously. So I tried to think of some swapping method that could avert this. However, there appears to be a simpler solution: do not reuse x and y. We could write something like:
Code:
void printGreaterThan(int x, int y)
{
while (x > y)
{
cout << "x is bigger than y";
x = 0;
y = 1;
}
}
void printEqualTo(int x, int y)
{
// ...
}
void printLessThan(int x, int y)
{
// ...
}
// ...
printGreaterThan(x, y);
printEqualTo(x, y);
printLessThan(x, y);
Since x and y within each function is local to that functions, the assignment that ends the loop does not affect the x and y from the caller (e.g., the main() function). As such, only one of the functions when called actually prints something, and only once. This is equivalent to the if - else if - else structure that we would normally use.
-
I meant in terms of different ways of writing a conditional.
-
How about
Code:
for (bool cond=(x > y); cond; cond=false) {
// stuff
}
-
laserlight:
Excellent touch breaking it up into functions and changing the values in the body. That solves my problem.
Thanks everyone for the help. Since I'm getting more and more interested in C++ don't be surprised to see me in other threads in the near future. ;-)
/Rahiiyja
-
You're welcome, though I suspect that robatino's suggestion may be even simpler and easier to use.
-
Here's an update of the situation, and some code I've written.
Code:
#include <iostream>
using namespace std;
int biggerThan(int, int);
int smallerThan (int, int);
int equal (int, int);
int main()
{
cout << "Type in name of value one: ";
string one;
cin >> one;
cout << endl << "Type in name of value two: ";
string two;
cin >> two;
cout << endl << "Type in the value of " << one << ": ";
int onevalue;
cin >> onevalue;
cout << endl << "Type in the value of " << two << ": ";
int twovalue;
cin >> twovalue;
cout << endl << endl;
biggerThan(onevalue, twovalue);
smallerThan(onevalue, twovalue);
equal(onevalue, twovalue);
return 0;
}
int biggerThan(x,y)
{
while(x>y)
{
cout << one << " is bigger than " << two << " with ";
cout << x-y << ".";
x=y;
}
}
int smallerThan(x,y)
{
while(x<y)
{
cout << two " is bigger than " << one << " with ";
cout << y-x << ".";
x=y;
}
}
int equal(x, y)
{
while(x==y)
{
cout << one << " & " << two;
cout << " are equal ";
x=y+1;
}
}
I get a lot of messages from the compiler though. Can anyone tell me, or give me a hint as to what is wrong?
Here are some examples of the messages I get:
Code:
29 int biggerThan' redeclared as different kind of symbol
4 previous declaration of `int biggerThan(int, int)'
29 'int biggerThan'
4 conflicts with previous declaration `int biggerThan(int, int)'
29 `x' was not declared in this scope
29 `y' was not declared in this scope
30 initializer expression list treated as compound expression
30 expected `,' or `;' before '{' token
39 `int smallerThan' redeclared as different kind of symbol
5 previous declaration of `int smallerThan(int, int)'
and it goes on...
I guess most of you experienced people already can see instantly what the problem is, but if you want I can post all the remaining compiler outputs.
Thanks for any help
/Rahiiyja
-
> int biggerThan(x,y)
You need some types in there as well, like
int biggerThan(int x, int y)
Specifically, they must match the prototype.
-
Thanks, that's something I should have tried, it solved most of it. The final problem I had was that the strings weren't declared globally, and therefore couldn't be used within the other functions, but I solved that.
The problem I have now though, is that the program closes after I've typed in all the names and values. I'm using Dev-C++ 5 beta, and have included 'cin.get()' before the 'return0;' in 'int main().' Any clues?
EDIT: When trying to run it again it suddenly worked. I must have forgotten to recompile it after using cin.get(). Thanks for your help.