Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
// write a program to build a menu
// if choose menu 1: ask to enter a number and check whether the number is a Armstrong's number or not
// if choose menu 2: ask to enter a number and check whether the number is a prime number or not
// if choose menu 3: display the question "Do you want to finish (y/n)?". If enter "y", finish the program; If enter "n", allow to reselect the menu

#include<iostream>
#include<math.h>
using namespace std;
int main() {
	char finishChoice;
	do {
		cout << "\n MENU \n";
		cout << "1. Armstrong" << endl;
		cout << "2. Prime" << endl;
		cout << "3. Finish" << endl;
		int a;
		cout << "------------------" << endl;
		cout << "Please choose (1,2,3): ";
		cin >> a;
		
		switch(a) {
			
			
			case 1: // armstrong
				{
					int m, n;
					cout << "Enter a natural m: ";
					cin >> m;
					cout << "Enter a natural n: ";
					cin >> n;
					
					cout << "Armstrong numbers from " << m << " to " << n << " are: " << endl;
					
					int i = m;
					while(i <= n) {
						int num = i;
						int temp = num;
						int sum = 0;
						int cnt = 0;
						while(num > 0) { // dem so luong chu so trong num
							++cnt;
							num /= 10;
						}
						num = temp;
						while(num != 0) {
							sum += pow(num%10, cnt);
							num /= 10;
						}
						if(sum == i) {
							cout << i << " ";
						}
						++i;	
					}
					cout << "Thanks to use my program <3";
					return 0;
				} break;
		
			case 2: // prime
				{
					int n;
					cout << "Enter number: ";
					cin >> n;
					
					int i = 2;
					int cnt = 0;
					while(i <= sqrt(n)) {
						if(n % i == 0)
							cnt++;
						i++;
					}
					if(cnt == 0 && n > 1)
						cout << n << " is a prime.";
					else
						cout<< n << " is not a prime.";
					cout << "\nThanks to use my program ^.^ " ;
	
					return 0;
				} break;
				
				
			case 3: // finish
				{
					cout << "Do you want to finish (y/n)? ";
					cin >> finishChoice;
					cout << "See you again. Thanks to use my program <3";
				} break;
			
			default:
				cout << "Invalid choice. Please do it again!" << endl; break;
		}
	} while(finishChoice != 'y' || finishChoice != 'Y');
	return 0;
}


What I have tried:

i have some problem with case 3
it doesn't work :((
Posted
Comments
Member 15627495 7-Nov-23 7:57am    
you face the hard thing with a 'break' at the wrong place, sure you quit the 'switch' statement.
but not the whole App.

use 'exit( EXIT_SUCCESS ); '

I haven't ran the code but, shouldn't
} while(finishChoice != 'y' || finishChoice != 'Y');

be
} while(finishChoice != 'y' && finishChoice != 'Y');

?
If you enter y for menu 3 then y != 'Y' and enter Y the Y != 'y'
 
Share this answer
 
Comments
CPallini 7-Nov-23 5:44am    
5.
Như Quỳnh 2023 7-Nov-23 5:50am    
thank youu
Quote:
C++
while(finishChoice != 'y' || finishChoice != 'Y')
You are looping while the finishChoice variable is set to anything other than 'y', OR set to anything other than 'Y'.

Think about that for a moment: if it's 'y', then it's not 'Y', so you loop. If it's 'Y', then it's not 'y', so you loop.

The only time you exit your loop is if the variable is equal to both y and Y at the same time.

Since you're not using a quantum computer, that condition is impossible to satisfy. So you always loop.

Replace || with && and you might have better luck.
 
Share this answer
 
Comments
CPallini 7-Nov-23 5:44am    
5.
Như Quỳnh 2023 7-Nov-23 5:50am    
thanks a lot <3
The previous solutions gave you the correct answer but here's a little tip to avoid the problem all together : use the toupper or tolower function. Those convert a single character to upper case or lower case, respectively. Using one of them can allow your code to look like this :
C++
do {
   // ...
} while( toupper( finishChoce ) != 'Y' );
 
Share this answer
 
I would restructure your program to escape only from the choice 3 if confirmed using a while loop

C++
#include<iostream>
#include<cmath>

using namespace std;
int main() 
{
	while(true)
    {
		cout << "\n MENU \n";
		cout << "1. Armstrong" << endl;
		cout << "2. Prime" << endl;
		cout << "3. Finish" << endl;
		int a;
		cout << "------------------" << endl;
		cout << "Please choose (1,2,3): ";
		cin >> a;
		
		switch(a) {
			
			
			case 1: // armstrong
				{
					int m, n;
					cout << "Enter a natural m: ";
					cin >> m;
					cout << "Enter a natural n: ";
					cin >> n;
					
					cout << "Armstrong numbers from " << m << " to " << n << " are: " << endl;
					
					int i = m;
					while(i <= n) {
						int num = i;
						int temp = num;
						int sum = 0;
						int cnt = 0;
						while(num > 0) { // dem so luong chu so trong num
							++cnt;
							num /= 10;
						}
						num = temp;
						while(num != 0) {
							sum += pow(num%10, cnt);
							num /= 10;
						}
						if(sum == i) {
							cout << i << " ";
						}
						++i;	
					}
					cout << "Thanks to use my program <3";
				} break;
		
			case 2: // prime
				{
					int n;
					cout << "Enter number: ";
					cin >> n;
					
					int i = 2;
					int cnt = 0;
					while(i <= sqrt(n)) {
						if(n % i == 0)
							cnt++;
						i++;
					}
					if(cnt == 0 && n > 1)
						cout << n << " is a prime.";
					else
						cout<< n << " is not a prime.";
					cout << "\nThanks to use my program ^.^ " ;
	
				} break;
				
				
			case 3: // finish
				{
                    char finishChoice;
					cout << "Do you want to finish (y/n)? ";
					cin >> finishChoice;
                    if( toupper( finishChoce ) == 'Y')
                    {
					    cout << "See you again. Thanks to use my program <3";
                        return 0;
                    }
				} break;

			default:
				cout << "Invalid choice. Please do it again!" << endl; break;
		}
	}
	return 1;
}


I also removed the return from the other 2 choices
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900