Hi, I need some help with an assignment. Got some problems with my while loop. Can't seem to figure out what's wrong with it.

#include <iostream>
#include <string>
#include <bitset>

using namespace std;

int last (int num)
{
    int l;
    l = (num % 10);
    
    return (l);
}

int first (int num)
{
    int f;
    while (num>9)
    { 
          num /=10;
    }
    f = num;
    return (f);
}
  
int total (int num)
{
    int t;
    t = 0;
    
    while (num>0)
    { 
          t++;
          num /=10;
    }
    
    return (t);
}

int odd (int num)
{
    int o = 0;
    while (num>0)
    {
          if (num%2 !=0)
          o++;
          num = num/10;
    }
    
    return (o);
}
          
int even (int num)
{
    int e = 0;
    while (num>0)
    {
          if (num%2 ==0)
          e++;
          num = num/10;
    }
    
    return (e);
}      




int main()
{
    int num;
    
    while (num != 0)
    { 
        cout << "Enter an integer (0 to stop): ";
        cin >> num;
        
        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 << "End of task.\n";
    cout << endl;
    system("pause");
    return 0;   
}

The program works fine but I need it to stop looping when it's 0. Example:

Enter an integer (0 to stop): 0
End of task.

I can't use break or anything that stops the program. My lecturer says it's bad programming...

Another thing, I have 2 assignments to do. One program using the concept of passing by value and the same program using the concept of passing by reference (i.e. functions return types are void)

So if I understand correctly, my program above represents the concept of passing by reference right? Just to confirm my doubts.

Thanks alot for looking here. ^ ^

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.

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.

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

Edit: Just saw Wolfpack's post. I'll take a look. I only know cplusplus.com for a reference C++ website, thinkquest looks interesting.
Hope I can level up my C++ skills from there. =x

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... :'(

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

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?

I need to do a passing by reference for the same program in the 1st post.
Trying to convert it gives me problems though.
Can't seem to get multiple values from 1 value.

#include <iostream>
using namespace std;

void calculation (int x, int t1, int &last, int &first, int &total)
{
     
    last = (x % 10);
    while (x>9) 
    first = x / 10;
    t1 = 0;
    while (x>0)
    { 
          t1++;
          x /=10;
    }
    total = t1;
    
}
int main(void)
{
    int num;
    while (num != 0)
    { 
        cout << "Enter an integer (0 to stop): ";
        cin >> num;
        
        if ( num != 0)
        {               
            calculation(num);
            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;   
    }

Here's what I got so far, but dev c++ keeps giving me (too few agruments to function for the calculation function... :confused:

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)

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

Wow... I didn't realise that I missed out so many things.
I'm going to try get this code right. Thanks all!

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.

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

#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;
    }
}

Hi all, I just solved my assignment with help from here and there.
Thanks for the help! Thought I'll post the result here.

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

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.

commented: That's why I don't get his point. +3

I don't feel that your code works 100% correctly. What's happen when the first digit is number 9? 9 < 9 == false .

commented: good catch. +7

Aaa I missed that, if I changed it to 10 it should work perfectly right?
Anyway, I send it in and my lecturer gave me 10/10 for it... Guess he missed it as well. :)

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

I disagree with the part that you said x should pass by reference. I think passing x by value is a perfect thing to do, since it won't affect the real value of num in the main() function.

I disagree with the part that you said x should pass by reference. I think passing x by value is a perfect thing to do, since it won't affect the real value of num in the main() function.

I see your point in the context of how the OP wrote his code.
I had done it in such a way that there was no operation performed directly on x, so I was able to pass x by reference with no changes.

Doing things like assigning x to alternate variables

first=x;
...
first /=10;

...etc
keeps x intact.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.