Oddcount Evencount and zero count problem. Program wont work?

Hey guys, I am trying to get the number to work and I can't make it seem to work any insight on how to do this? The program runs just fine, but when i enter 'b' on my menu and type in a number it doesn't work and it just freezes any help at all?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #include <iostream>
#include <string> 
#include <iomanip>
#include <fstream>

using namespace std;



int largest(){
	int num, large = 0;
	
	do { 
		cout << "Please enter a number. Enter (-1) to quit: ";
		cin >> num;
		if (large < num)
		large = num;
		
	}while(num!= -1);
	
	cout << "Largest Number: " << large;
	
}

void numberCount(int number, int &oddCount, int &evenCount, int &zeroCount){
	const int QUIT = 0;
	int aux, digit;
	cout << "Enter a number (-1 to exit): ";
	cin >> number;
	while(number!=-1){
		aux = number;
		while(aux!=QUIT){
			digit = aux % 10;
			aux / 10;
			if(digit!=0 && digit%2==0){
				evenCount++;
				
			}else{if(digit==0)
				zeroCount++;
				else
				oddCount++;
			} if(number==0){
			
			zeroCount++;}
		}
	}
	
	cout << "Even: " << evenCount;
	cout << "\nOdd: "<< oddCount;
	cout << "\nZero: " << zeroCount;
	
		zeroCount = 0;
		evenCount = 0;
		oddCount = 0;
		
		cout << "Enter a number (-1 to quit): ";
		cin >> number;	
}

int main ()
{
	int 
	oddCount = 0, 
	evenCount = 0,
	zeroCount = 0,
	number,
	digit,
	aux;
	
	char choice;
	do {
		cout << "/nWelcome to my progam!" << endl << endl;
		cout << "Here are your following options:" <<endl;
		cout << "(a) Find the largest list of positive numbers (-1) to quit" << endl;
		cout << "(b) Given a positive number display the number of even, odd, and zero digits in the number" << endl;
		cout << "(c) quit the program."<< endl;
		cout << "Please enter your choice: ";
		cin >> choice;
		
		switch (choice){
			case 'a':
				largest();
				break;
			case 'b':
				numberCount( number,  oddCount,  evenCount,  zeroCount);
				break;
			case 'c':
			cout  << "Thank you for using my program!" << endl;
			return 0;
			break;	
		}
	}while(choice!='c');
}
Your code Doesnt make much sense to me. Could you explain what you want B and A to do exactly?
closed account (D80DSL3A)
Both of the nested while loops go forever. See comments in code for explanation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
           cout << "Enter a number (-1 to exit): ";
	   cin >> number;
           while(number!=-1){
		aux = number;
		while(aux!=QUIT){
			digit = aux % 10;
			aux / 10;// has no effect. Did you mean aux /= 10 ?
			if(digit!=0 && digit%2==0){
				evenCount++;
				
			}else{if(digit==0)
				zeroCount++;
				else
				oddCount++;
			} if(number==0){
			
			zeroCount++;}
		}
                // number never changes value, so outer while will never end.
                // prompt user for new value of number here.
	}
@tarikneaj

A. is suppose to to find the largest number untill someone enters -1 and then it spits out the largest number.

b. Is your suppose to enter a number like 30027 then the output of that would be
evens:1
Odds: 2
Zeros:2

and okay thank you for the help fun2code.

EDIT: is this right? It's still not wanting to output the even odd or zero count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void numberCount(int number, int &oddCount, int &evenCount, int &zeroCount){
	const int QUIT = 0;
	int aux, digit;
	cout << "Enter a number (-1 to exit): ";
	cin >> number;
	while(number!=-1){
		aux = number;
		while(aux!=QUIT){
			digit = aux % 10;
			if(digit!=0 && digit%2==0){
				evenCount++;
				
			}else{if(digit==0)
				zeroCount++;
				else
				oddCount++;
			} if(number==0){
				zeroCount;} 
		} cout << "Enter a number (-1 to exit): ";
			cin >> number;
	} 
	
	cout << "Even: " << evenCount;
	cout << "\nOdd: "<< oddCount;
	cout << "\nZero: " << zeroCount;
	
		zeroCount = 0;
		evenCount = 0;
		oddCount = 0;
		
	
}
Last edited on
Topic archived. No new replies allowed.