When reaching choice 0, how do I stop looping back to menu - c++

This is a menu driven program for calculations.
#include <iostream>
#include <cmath>
using namespace std;
void Choices();
int x;
int n;
float fact;
float cosh(float x, int n);
float sinh(float x, int n);
float factorial( int n );
int main ()
{ char exit;
char choice;
bool option1hasrun = false;
while (exit != 'y' || exit != 'Y')
{
Choices();
cin >> choice;
if (choice == '1' && option1hasrun == false) {
cout << "Please give a value for x: ";
cin >> x;
cout << "Please give a value for the approximation order n: ";
cin >> n;
cout << endl;
option1hasrun = true;
}
else if (choice == '2' && option1hasrun == true)
{
cout << "The hyperbolic sinh of x is: " << sinh(x) << endl;
cout << "Using Taylor series is it: " << sinh(x, n) << endl;
}
else if (choice == '3' && option1hasrun == true)
{
cout << "The hyperbolic cosh of x is: " << cosh(x) << endl;
cout << "Using Taylor series is it: " << cosh(x, n) << endl;
}
else if (choice == '4' && option1hasrun == true)
{
cout << "old value of x = " << x << endl;
cout << "old approximation = " << n << endl;
cout<< "Please give new value of x: " ;
cin >> x;
cout << endl;
cout << "Please give new n: " ;
cout << endl;
cin >> n;
}
if ((choice=='2' || choice=='3' || choice=='4') && option1hasrun==false)
{
cout << "You have to enter a value first!\n\n";
}
if (choice<'0' || choice > '4')
{
cout << "Wrong choice. Only options 1-4 are available.\n\n";
}
else if (choice == '0' && option1hasrun==true)
{
cout << "Are you sure you want to quit? (Y/N) ";
cin >> exit;
if (exit =='y' || exit == 'Y') {
cout << "bye bye!!";
}
}
/* After entering Y I would like the program to stop, but currently it continues to loop*/
}
return 0;
}
float factorial( int n )
{
float fact = 1;
for ( int i = 1; i <= n; i++ )
{
fact = fact * i;
}
return fact;
}
float sinh(float x, int n)
{
float sum = 0;
for ( int i = 0; i <= n; i++ )
{
sum = sum + pow(x, 2*i +1)/factorial(2*i +1);
}
return sum;
}
float cosh(float x, int n)
{
float sum = 0;
for ( int i = 0; i <= n; i++ )
{
sum = sum + pow(x, 2*i)/factorial(2*i);
}
return sum;
}
void Choices ()
{
cout << "MAIN MENU" << endl;
cout << "1. To enter the data. " << endl;
cout << "2. To calculate and approximate the sinh(x)" << endl;
cout << "3. To calculate and approximate the cosh(x) " << endl;
cout << "4. To modify data. " << endl;
cout << "0. to quit." << endl << endl;
cout << "Please make a choice :";
}
Can someone help me figure out why the program continue to loops after choice 0?

Entered characters cannot be neither 'y' nor 'Y' at the same time.
Try changing the exit condition to:
while (exit != 'y' && exit != 'Y')

Related

Repeat checker goes back to menu even after entering N to input

The goal is to create a five-in-one program for various math stuff. The individual blocks work. After selecting and using a block, the program should then send a prompt asking the user whether to go back to the menu or terminate the program, this also works.
However, the function which checks whether you've inputted "Y", "y", "N", or "n" does not work. Instead, it automatically returns you to the main menu even after inputting "N" or "n", which should terminate the program.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
// checks if prime is positive or nah
if (x <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < x; i++)
if (x % i == 0)
return false;
return true;
}
void Repeatcheck(char repeat){
//checks whether you inputted anything other than Y, y, N, or n
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
//Option selection
int OptSel;
//repeat selection
char repeat;
repeat = 'Y';
//Optsel 1
int prime, i, primed2=0, flag=0;
//Optsel 2
int j,rows;
//Optsel 3
int t1 = 0, t2 = 1, nextTerm = 0;
//Optsel 4
int factorialinput;
long factorial = 1.0;
do{
system("cls");
repeat = 'Y';
startoptsel:
std::cout << "\n----MATHS PROGRAM----" << std::endl;
std::cout << "\n wats poppin?" << std::endl;
std::cout << "1.) prime number checker" << std::endl;
std::cout << "2.) right triangle drawer" << std::endl;
std::cout << "3.) fibonacci" << std::endl;
std::cout << "4.) factorial" << std::endl;
std::cout << "5.) exit" << std::endl;
std::cin >> OptSel;
//check whether option seleccted is available.
if (!std::cin || OptSel <= 0 || OptSel > 5) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\nThat's not an option, dumbass." << std::endl;
std::cout << "\nInput numbers from 1-5." << std::endl;
goto startoptsel;
}
if (OptSel == 1) {
system("cls");
primechecker:
std::cout << "\n----Prime number checker----" << std::endl;
std::cout << "Enter number: " << std::endl;
std::cin >> prime;
// auto filters everything as not prime below 1
primed2=prime/2;
for(i = 2; i <= primed2; i++){
if(prime % i == 0)
{
cout << "\n " << prime << " is not a prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "\n " << prime << " is a prime."<<endl;
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto primechecker;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 2) {
system("cls");
triangledrawer:
cout << "\n----RIGHT TRIANGLE DRAWER----" << std::endl;
cout << "\nInput number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++){
for(j=1;j<=i;j++)
cout<< ' ' << j;
cout<<endl;
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto triangledrawer;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 3){
system("cls");
fibonacciprinter:
cout << "\n----Fibonacci printer----";
cout << "\nEnter the number of terms: ";
cin >> prime;
cout << "Fibonacci Series: ";
for (int i = 1; i <= prime; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto fibonacciprinter;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 4){
system("cls");
cout << "Enter a positive integer: ";
cin >> factorialinput;
cout << "Factorial of ";
if (factorialinput < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= factorialinput; ++i) {
cout << i << " ";
factorial *= i;
}
cout << "= " << factorial;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
repeat = repeat;
}
if (repeat == 'n' || repeat == 'N'){
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
}while (OptSel != 5);
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
Rewriting the program to instead run when repeat = Y or y and removing the specific instance of terminating the program when N or n is entered instead results in the program terminating automatically no matter what is inputted when the Repeatcheck function is called.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
...
}
void Repeatcheck(char repeat){
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
...
do{
...
}while (OptSel != 5 && repeat == 'Y' || repeat == 'y');
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

Asking the user to input one of the options and depending on the choice, the program shows the answer

How to ask the user to input two numbers, and after that show these options:
sum,
average,
maximum,
minimum,
exit.
Then due to the user choice, the program shows the answer, and this process continues until the user input number 5.
My code does not work properly. Here is the code:
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
int x, y, choice;
char sum, average, max, min;
cout << "Enter two numbers: " << endl;
cin >> x >> y;
cout << "Choose one of these options: \n";
cout << " sum - average - max - min - exit " << endl;
choice == sum || average || max || min;
cin >> choice;
sum == x + y;
max == x > y || y > x;
if (choice == sum) {
cout << "The sum is: " << x + y << endl;
}
if (choice == average) {
cout << "The average is: " << x / y << endl;
}
if (choice == max&& x > y) {
cout << "The maximum is: " << x << endl;
} else
cout << " The maximum is: " << y << endl;
if (choice == min&& x < y) {
cout << "The minimun is: " << x << endl;
} else
cout << " The minimun is: " << y << endl;
while (true) {
string choice;
cin >> choice;
if (choice == "5") {
break;
}
}
return 0;
}
So you want to calculator which should end based on the user choice? If So
You can wrap the Function with a While loop and Keep a set a bool based on the user input. Like :
bool Terminate = false;
while(!Terminate)
{
//Do Your Calculations Here
//Ask for the User Input
std::string UserChoice = "";
std::cout << "Do You Want to Exit [ Y/N ]";
std::cin << UserChoice;
if(UserChoice == "Y" || USerChoice == "y")
{
std::cout << "Ending the Program";
Terminate = true;
}
}
If you have any doubts check this out
https://learn.microsoft.com/en-us/cpp/get-started/tutorial-console-cpp?view=msvc-160

How do I properly run if statements and do while loops with functions?

I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.
I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";
cout << "Pick which program you would like to run." << endl;
cin >> choice;
}
else if (choice == 1){ // -----------Perfect Scores------------
do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
if(a[1]==100)
i++;
return i;
}
{
int score[10];
for(int i=0; i<10; i++)
{
cout << "Enter score " << i+1 << endl;
cin >> score[i];
while(score[i]<0 || score[i] > 100)
{
cout << "Enter score between 1 and 100." << endl;
cin >> score[i];
}
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ------------Larger Than n--------------
else if (choice == 2){
do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
if(A[i]>n)
{
cout << A[i] << endl;
}
}
}
int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------------Roman Numeral Converter------------------
else if (choice == 3){
char repeat;
do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX", "X", "XI", "XII",
"XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit." << endl;
cin >> n;
if(n==0)
exit(0);
do
{
cout << "Enter number between 1 and 20" << endl;
cout << "Enter number or enter 0 to quit" << endl;
cin >> n;
} while(n < 0 || n > 20);
{
cout << "Enter decimal equivalent roman number:" << endl;
cout << "Enter a number between 1 and 20:" << endl;
cin >> n;
if(n==0)
exit(0);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ---------------Monkey Business--------------------
else if(choice == 4){
do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);
{
const int MONKEYS = 3;
double totalFood, averageFood, leastFood, greatestFood;
int foodInfo[MONKEYS][DAYS];
for(int i= 0; i< MONKEYS; i++)
{
cout << "Enter the food information of the monkey" <<
(i + 1) << ":" << endl;
for(int j = 0; j < DAYS; j++)
{
cout << "Day" << (j + 1) << ":" << endl;
cin >> foodInfo[i][j];
while(foodInfo[i][j] < 0)
{
cout << "Day " << (j+1) << ":" << endl;
cin >> foodInfo[i][j];
}
}
cout << endl;
}
totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
averageFood = totalFood / DAYS;
cout << "The average amount of food per day for three monkeys(in pounds):"
<< averageFood << endl;
cout << "The least amount of food per week for monkeys(in pounds) is:"
<< leastFood << endl;
cout << "The greatest amount of food per wek for a monkey is(in pounds):"
<< greatestFood << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
double total = 0;
for(int i = 0; i < mnks; i++)
{
for(int j = 0; j < DAYS; j++)
{
total += food[i][j];
}
}
return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
double least = leastAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal += food[i][j];
}
if(least > weekTotal)
least = weekTotal;
}
return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
double greatest = greatestAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal +=food[i][j];
}
if(greatest < weekTotal)
greatest = weekTotal;
}
return greatest;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------Lottery--------------------
else if (choice == 5){
char repeat;
do{
srand(time(NULL));
int winningDigits[5];
int player[5];
int num;
int matchCount = 0;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 10;
}
cout << "Enter 5 integers in the range of 0 to 9." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
while (num < 0 || num > 9)
{
cout << "Invalid number! It should be in the range of 0 through 9." << endl;
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
}
player[i] = num;
}
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
{
matchCount++;
}
}
cout << "Winning digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << winningDigits[i] << " " << endl;
}
cout << "Player's digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << player[i] << " " << endl;
}
cout << endl << endl << "Number of digits matched: "
<< matchCount << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}
I'm expecting to be able to choose a program to run, repeat it, and repeat the entire program from main menu.
I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:
int choose() { return 0; }
void func1() {}
void func2() {}
int main() {
int choice = 0;
while ( choice = choose() ) {
switch(choice) {
case 1 : func1(); break;
case 2 : func2(); break;
}
}
}
Your main function does not have to be more complex than that.
Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.
Some problems in your code (partly stolen from comments):
you cannot have more than one main
you cannot define functions inside functions
you cannot start an if statement with else if
int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
see here why using namespace std is considered bad practice
if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)

C++ do-while loop won't quit even once the condition is met [duplicate]

This question already has answers here:
conditional statement in while loop
(2 answers)
std::cin of char causing infinite loop
(3 answers)
Closed 9 years ago.
So most of my program works fine. It is a program designed to estimate sine and cosine values using Taylor series. The program is designed to quit once the user inputs 0, and then "Y" or "y" upon being asked if they are sure. The char variable exit is initialized to "n", and then is changed if the user inputs y. But the loop doesn't quit then.
#include <iostream>
#include <cmath>
using namespace std;
double calculateFACT(int n); // function that calculates the factorial
double calculateSIN(float, float); // function that approximates the sine
double calculateCOS(float, float); // function that approximates the cosine
int main()
{
int choice; // menu choice
double angle = 0; // angle user inputs, initialied to zero
double calc; // the calculated sine or cosine value
int order; // order approimation value
char exit = 'n'; // exits for yes
do {
cout << "MAIN MENU" << endl;
cout << "1. To enter the data." << endl;
cout << "2. To calculate the sin(x)" << endl;
cout << "3. To approximate the sin(x)" << endl;
cout << "4. To calculate the cos(x)" << endl;
cout << "5. To approximate the cos(x)" << endl;
cout << "6. To re-enter data." << endl;
cout << "Press 0 to quit." << endl;
cout << "Please make a choice: ";
cin >> choice;
cout << endl;
if (choice != 0 &&
choice != 1 &&
choice != 2 &&
choice != 3 &&
choice != 4 &&
choice != 5 &&
choice != 6)
{
cout << "Wrong Choice. Only options 1-6 are available." << endl << endl;
}
if (choice == 1)
{
if (angle == 0)
{
cout << "Please give a value for the angle: ";
cin >> angle;
cout << endl;
}
else cout << "Please use option 6 to enter a new angle." << endl << endl;
}
if (choice == 2)
{
if (angle == 0)
{
cout << "You have to enter a value first!" << endl << endl;
}
else
{
calc = sin(angle);
cout << "The sine of x is " << calc << endl << endl << endl;
}
}
if (choice == 3)
{
if (angle == 0)
{
cout << "You have to enter a value first!" << endl << endl;
}
else
{
cout << "Please give a value for the approximation order n: ";
cin >> order;
cout << "The approximation of sin(" << angle << ") is: " << calculateSIN(angle, order) << endl << endl;
}
}
if (choice == 4)
{
if (angle == 0)
{
cout << "You have to enter a value first!" << endl << endl;
}
else
{
calc = cos(angle);
cout << "The cosine of x is " << calc << endl << endl << endl;
}
}
if (choice == 5)
{
if (angle == 0)
{
cout << "You have to enter a value first!" << endl << endl; // cosine function not giving the right value
}
else
{
cout << "Please give a value for the approximation order n: ";
cin >> order;
cout << "The approximation of cos(" << angle << ") is: " << calculateCOS(angle, order) << endl << endl;
}
}
if (choice == 6)
{
if (angle == 0)
{
cout << "If this is the first time you run this program please choose option 1." << endl << endl;
}
else
{
cout << "Please give new angle: ";
cin >> angle;
cout << endl << endl;
}
}
if (choice == 0)
{
cout << exit;
cout << endl << endl << "Are you sure you want to quit? (Y/N): "; // Y/N option doesnt work
cin >> exit;
}
cout << exit;
} while (exit != 'Y' || exit != 'y');
if (exit == 'Y' || exit == 'y')
{
cout << endl << "Now quitting.." << endl;
system("pause");
return 0;
}
}
double calculateFACT(int n)
{
double nfact = 1;
for (int i = 2; i <= n; i++)
nfact *= i;
return nfact;
}
double calculateSIN(float angle, float order)
{
double sine = angle;
for (int i = 1; i < order; i++)
{
sine += pow(-1.0, i) * (pow(angle, 2 * i + 1)) / calculateFACT(2 * i + 1);
}
return sine;
}
double calculateCOS(float angle, float order)
{
double cosine = 0;
for (int i = 0; i < order; i++)
{
cosine += pow(-1.0, i) * (pow(angle, 2 * i)) / calculateFACT(2 * i);
}
return cosine;
}
I answered a similar question named Why is my c++ code not working properly?. And the answer is exactly the same. You need to do exit != 'Y' && exit != 'y' otherwise it will always evaluate to true.
remyabel answered the question. Your code says "if the user didn't type 'Y' or 'y'", keep running. Since you're only looking for one character, it will keep running forever, since the character cannot be both 'Y' and 'y' at the same time.
Hence, while (exit != 'Y' && exit != 'y') essentially says "if the user didn't type an exit condition, I'm going to keep executing."