You could use a boolean flag to control the loop. Ask for input wihin the loop body and evaluate the result. If the input is zero then change the value of the flag. If not, then do your calculations.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
You can do something like this.
int main()
{
int num;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num != 0)
{
cout << "Last digit: " << last (num) << endl;
cout << "First digit: " << first (num) << endl;
cout << "Total digits: " << total (num) << endl;
cout << "Odd digits: " << odd (num) << endl;
cout << "Even digits: " << even (num) << endl;
cout << "\n";
cout << "Enter an integer (0 to stop): ";
cin >> num;
}
cout << "End of task.\n";
cout << endl;
system("pause");
return 0;
}
Contrary on what your programming lecturer says, using break is not bad programming practice.
And you are not using passing by reference . You are using passing by value.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Aaaa I see. I tried putting if ( num != 0) into the while loop and it works!
Guess I got to do more examples to get the hang of C++
Btw, the program is an example of using the concept of passing by reference right?
Thanks ^^
No, you are NOT passing variables by reference .
also, you are testing the varaible out of sequence try:
do
{
//your program here
}
while(num !=0);
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
Hi, I'm stuck again :( I read up on the passing by reference topic, understood it but don't get the point on extracting multiple values from 1 input.
void calculation (int x, int &last, int &first)
{
last = (x % 10);
while (x>9)
first = x / 10;
}
I got this so far, x being input.
But it doesn't work for me... :'(
What are the problems you are getting? Passing by reference can be used when you want to update the values in last and first . Is that what you want to do?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Hi, I'm stuck again :( I read up on the passing by reference topic, understood it but don't get the point on extracting multiple values from 1 input.
void calculation (int x, int &last, int &first)
{
last = (x % 10);
while (x>9)
first = x / 10;
}
I got this so far, x being input.
But it doesn't work for me... :'(
To make this a void function that does something without a return value, you will need to decare first and last as GLOBAL variables, else they go out of scope when the void function returns.
all you input is:
void calculation (int & x)
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
When you call the function calculation , you will have to pass the variable names that you want to get the output from.
int main(void)
{
int num;
int first, last, total;
while (num != 0)
{
cout << "Enter an integer (0 to stop): ";
cin >> num;
if ( num != 0)
{
calculation(num, last, first, total);
cout << "Last digit: " << last << endl;
cout << "First digit: " << first << endl;
cout << "Total digits: " << total << endl;
}
}
cout << "End of task.\n\n";
system("pause");
return 0;
}
You do not want to pass t1 as an input for calculation, so change it like this. You do not need t1 at all.
void calculation (int x, int &last, int &first, int &total)
{
last = (x % 10);
while (x>9)
first = x / 10;
while (x>0)
{
total++;
x /=10;
}
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
When you call the function calculation , you will have to pass the variable names that you want to get the output from.
int main(void)
{
int num;
int first, last, total;
while (num != 0)
{
cout << "Enter an integer (0 to stop): ";
cin >> num;
if ( num != 0)
{
calculation(num, last, first, total);
cout << "Last digit: " << last << endl;
cout << "First digit: " << first << endl;
cout << "Total digits: " << total << endl;
}
}
cout << "End of task.\n\n";
system("pause");
return 0;
}
You do not want to pass t1 as an input for calculation, so change it like this. You do not need t1 at all.
void calculation (int x, int &last, int &first, int &total)
{
last = (x % 10);
while (x>9)
first = x / 10;
while (x>0)
{
total++;
x /=10;
}
}
The problem is that he's not passing all those variables in the arguments. They are CALCULATED and assigned within the function.
There is only one, "x"
Since it's a void function, the varaibles must exist as globals to 'survive" outside of the fuction scope.
otherwise it needs to be something else like int calculation(...) with a return.
I have this working with a single reference pass void calcultion (int& x) along with a few other minor loop repairs.
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
congratulations! It works, albiet a bit inefficient.
As I said before, the only reference needed was x
and you pulled that one in as a value!
All you are doing by taking in all those other unneccessary references is to bring the variable scopes INSIDE the function. It works just as well if the variables were set as global. Aguments pass things in, not out!
I get the feeling that you have missed this point.
good luck.
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
congratulations! It works, albiet a bit inefficient.
As I said before, the only reference needed was x
and you pulled that one in as a value!
All you are doing by taking in all those other unneccessary references is to bring the variable scopes INSIDE the function. It works just as well if the variables were set as global. Aguments pass things in, not out!
I get the feeling that you have missed this point.
good luck.
No he has not missed the point. You have. Arguments can be used to pass things both in and out. Passing by reference can be used to get things out. It is also memory efficient because another copy of the variable is not created in the stack like it is done when you use passing by value.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
No he has not missed the point. You have. Arguments can be used to pass things both in and out. Passing by reference can be used to get things out. It is also memory efficient because another copy of the variable is not created in the stack like it is done when you use passing by value.
I ABSOLUTELY agree that passing by reference is memory efficient!
However, in his Calculation function, the one value (x) , is not passed by reference, but by value! My point was that only ONE variable actually needed to be passed in and that was x, not all of the ones that were. Variables first, last, etc, could be just as well been set in global scope along with their respective initial values.
I disagree with the fine point about arguments by reference being a way to get things out.
That is the domain of a return.
Passing those addition variables inside was only putting them in SCOPE within the function
braces, which otherwise would not have survived when the function returned or givin an "undefined variable " error.
Since all those other variables were assigned by different operations from the x variable,
it was the only one needed. The others were passive place holders for results.
It worked, but it just was unnecessarily complicated...
In the immortal words of Forrest Gump..."and thats all I've got to say about that"!
;-)
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75