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