So, I'm relatively new to C++, and I have to write a program that will take an input and count the even, odd, and zero digits from the input number.
Problem is, I got it to count the evens and odds, but no matter what I do it won't seem to count my zero digits; simply outputs the number of even digits, number of odd digits, and a 0 for the zero digits even if there are zero digits present in the input.
This is what I have at the moment after rewriting it many times:
Mind you, this is incomplete so it doesn't look very good, but I'm just looking to get the main function working properly before I fix it up and what-not.
Note: I am aware that I need a second loop inside of the first while loop, as well as a second input-prompt to repeat the first loop, but I'm not entirely sure what the inside loop parameters should be, or how to get it to actually count zeros.
Works fine for me? Only thing I'd advise is to make int QUIT an actualy constant and to find another method besides system(). Of course, you also need to finish what you said you were going to finish.
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
constint QUIT=0;
int
odd_count=0,
even_count=0,
zero_count=0,
number,
digit,
aux;
cout<<"Enter a number (or -1 to exit): ";
cin>>number;
//SOME CLEAR SCREEN METHOD -i don't need it-
while(number!=-1){
aux=number;
while(aux!=QUIT){
digit=aux%10;
aux/=10;
if(digit!=0 && digit%2==0){
even_count++;
}else{if(digit==0)
zero_count++;
else
odd_count++;
}//end if...else
}//end inner loop while
if(number==0)
zero_count++;
cout<<"Even: "<<even_count
<<"\nOdd: "<<odd_count
<<"\nZero: "<<zero_count
<<'\n';
zero_count=0;
even_count=0;
odd_count=0;
cout<<"Enter a number (or -1 to quit): ";
cin>>number;
}//end outer loop while
cout<<endl;
return 0; //indicates successful termination
}//end main
Enter a number (or -1 to exit): 0
Even: 0
Odd: 0
Zero: 1
Enter a number (or -1 to quit): 30027
Even: 1
Odd: 2
Zero: 2
Enter a number (or -1 to quit): 1
Even: 0
Odd: 1
Zero: 0
Enter a number (or -1 to quit): 300
Even: 0
Odd: 1
Zero: 2
Enter a number (or -1 to quit): 0
Even: 0
Odd: 0
Zero: 1
Enter a number (or -1 to quit): -1
So I tried your method Eyenrique, everything seemed to work, I just changed it around a bit to fit the way I like things styled and such. Thanks for the help guys, I've been trying to get this to work for almost a week now with no luck, and I still don't understand why it wouldn't count my zeros.
#include<iostream>
#include<cstdlib>
#include<iomanip>
usingnamespace std;
int main()
{
//local constants
constint QUIT = 0; //sentinel value
//local variables
int Odd_Count = 0; //Counter for odd digits
int Even_Count = 0; //counter for even digits
int Zero_Count = 0; //counter for zero digits
int Number; //variable for user input
int Digit; //variable for each digit
int Manip_Int; //variable that will actually be manipulated
//Prompt user for input or quit
cout<<"Enter a number (or -1 to quit): ";
cin >> Number;
//clear the screen
system("cls");
//first while loop, determines whether or not to run the program
while(Number != -1)
{
//set the manipulatable variable equal to user input
Manip_Int = Number;
//inner while loop, where actual math is executed
while(Manip_Int != QUIT)
{
//Mods the manip_int by 10 to isolate each digit
Digit = Manip_Int % 10;
//Removes the last digit from the number
Manip_Int /= 10;
//If digit is even
if(Digit != 0 && Digit % 2 == 0)
{
//increment even counter
Even_Count++;
}
//if digit is zero
elseif(Digit == 0)
{
//increment zero counter
Zero_Count++;
}
//if digit is neither even nor zero
else
{
//increment odd counter
Odd_Count++;
}
}
//if the users input is 0 and does not initiate the while loops
if(Number == 0)
{
//increment the zero counter
Zero_Count++;
}
//display heading to screen
cout << "Digit Classifier \n\n\n\n";
//display even count to screen
cout << " Even: " << Even_Count;
//display odd count to screen
cout << "\n Odd: " << Odd_Count;
//display zero count to screen
cout << "\n Zero: " << Zero_Count;
//clear the screen
system("cls");
//reset all counters to zero
Zero_Count = 0;
Even_Count = 0;
Odd_Count = 0;
//Prompt user for next input or quit
cout << "Enter a number (or -1 to quit): ";
cin >> Number;
}
//return 0 to the os
return 0;
}