| | |
Looping Problem and some questions
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
When you call the functioncalculation, 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; } }
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.
"I like beating by head against the wall because it feels so good when I stop"
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
Almost!!! I'm now left with placing counter =0 for total, odd and even.
The problem is, I can't find a suitable place to slot them in. Hmmm...
The problem is, I can't find a suitable place to slot them in. Hmmm...
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void calculation (int x, int& last, int& first, int& total, int& odd, int& even); int main(void) { int num; int last, first, total, odd, even; while (num != 0) { cout << "Enter an integer (0 to stop): "; cin >> num; if ( num != 0) { calculation(num, last, first, total, odd, even); cout << "Last digit: " << last << endl; cout << "First digit: " << first << endl; cout << "Total digits: " << total << endl; cout << "Odd digits: " << odd << endl; cout << "Even digits: " << even << endl; cout << "\n"; } } cout << "End of task.\n\n"; system("pause"); return 0; } void calculation (int x, int& last,int& first, int& total, int& odd, int& even) { last = (x % 10); while (x>9) { x /=10; } first = x; while (x>0) { total++; x /=10; } while (x>0) { if (x%2 !=0) odd++; x /= 10; } while (x>0) { if (x%2 ==0) even++; x /= 10; } }
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
Hi all, I just solved my assignment with help from here and there.
Thanks for the help! Thought I'll post the result here.
Thanks for the help! Thought I'll post the result here.
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void calculation (int x, int& last, int& first, int& total, int& odd, int& even); int main(void) { int num, last, first, total, odd, even; while (num != 0) { cout << "Enter an integer (0 to stop): "; cin >> num; if ( num != 0) { calculation(num, last, first, total, odd, even); cout << "Last digit: " << last << endl; cout << "First digit: " << first << endl; cout << "Total digits: " << total << endl; cout << "Odd digits: " << odd << endl; cout << "Even digits: " << even << endl; cout << "\n"; } } cout << "End of task.\n\n"; system("pause"); return 0; } void calculation (int x, int& last, int& first, int& total, int& odd, int& even) { total = 0; odd = 0; even = 0; last = (x % 10); while (x > 0) { total++; if (x % 2 !=0) odd++; else even++; if (x < 9) first = x; x /= 10; } }
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.
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.
"I like beating by head against the wall because it feels so good when I stop"
•
•
•
•
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.
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"!
;-)
"I like beating by head against the wall because it feels so good when I stop"
![]() |
Similar Threads
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- scanf discussion (C)
- Storing dynamic form values in Arrays for display & insert (PHP)
Other Threads in the C++ Forum
- Previous Thread: Homework Help
- Next Thread: Setting Array to Zero with Constructor
Views: 2516 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






