Looping for calculators - c++

The program will take in floating-point numbers and operators from the user and perform the required calculations, printing out the results. The result of each calculation will serve as an operand for the next calculation.
So, I am not sure if I am doing this correctly by using switch statements ? Is there a better way of doing it ? Maybe, by using a do - while loop ? I am really confused.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float firstOperand;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
int choice;
cin >> choice;
cout << endl;
float secondOperand;
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch (choice)
{
case 1:
{
float resultOne = firstOperand + secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
}
case 2:
{
float thirdOperand;
cout << "Enter another number ";
cin >> thirdOperand;
cout << endl;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
float resultTwo = resultOne + thirdOperand;
cout << "The result of the calculation is " << resultTwo << endl;
}
break;
}
system("pause");
return 0;
}

Try this code:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float firstOperand, secondOperand, result;
int choice, quit = 0;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
while (1){
cout << "The first operand is " << firstOperand << endl;
cout << "\nChoose an instruction code: " << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
if (choice == 8){
cout << "Quitting the program..." << endl;
break;
}
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch(choice){
case 1: result = firstOperand + secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 2: result = firstOperand * secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 3: result = pow(firstOperand, secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 4: result = 0;
cout << "The result has been cleared to " << result << endl;
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << endl;
break;
case 5: result = firstOperand - secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 6: if(secondOperand){
result = firstOperand / secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
}
else{
cout << "Second operand is " << secondOperand << "Choose again!" << endl;
}
case 7: result = sqrt(secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
default:cout << "Invalid input. Enter again!" << endl;
break;
}
}
}
The main code is wrapped inside an infinite while loop with a quit condition. The break statement is used to exit the while loop when the user wishes to.
Note: The input for the operator is the number and not the symbol. You will have to change choice variable to char in case you want to use the symbol as the input.
Update: The following code is for character inputs.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float firstOperand, secondOperand, result;
int quit = 0;
char choice;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
while (1){
cout << "The first operand is " << firstOperand << endl;
cout << "\nChoose an instruction code: " << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
if (choice == 'q'){
cout << "Quitting the program..." << endl;
break;
}
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch(choice){
case '+': result = firstOperand + secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case '*': result = firstOperand * secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 'p': result = pow(firstOperand, secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 'c': result = 0;
cout << "The result has been cleared to " << result << endl;
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << endl;
break;
case '-': result = firstOperand - secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case '/': if(secondOperand){
result = firstOperand / secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
}
else{
cout << "Second operand is " << secondOperand << "Choose again!" << endl;
}
case 's': result = sqrt(secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
default: cout << "Invalid input. Enter again!" << endl;
break;
}
}
}

Here is one possible version on the code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int select_operation()
{
int choice;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
return choice;
}
int main()
{
float secondOperand;
float firstOperand;
float thirdOperand;
float resultOne;
int choice;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
choice = select_operation();
if (choice == 8)
return 0;
else if(choice == 7)
secondOperand = .5;
else{
cout << "Enter the second number: ";
cin >> secondOperand;
}
cout << endl;
while(1)
{
switch (choice) {
case 1:
resultOne = firstOperand + secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 2:
resultOne = firstOperand * secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 3:
resultOne = pow(firstOperand,secondOperand);
cout << "The result of the calculation is " << resultOne << endl;
break;
case 4:
resultOne = 0;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 5:
resultOne = firstOperand - secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 6:
if(secondOperand){
resultOne = firstOperand / secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
}
break;
case 7:
resultOne = pow(firstOperand,secondOperand);
cout << "The result of the calculation is " << resultOne << endl;
break;
}
choice = select_operation();
if (choice == 8)
return 0;
else if(choice == 7)
secondOperand = .5;
else{
cout << "Enter another number ";
cin >> thirdOperand;
secondOperand = thirdOperand;
}
}
cout << endl;
firstOperand = resultOne;
return 0;
}
The above code accepts two inputs at first immediately returns if it is option 8(quit the program). if the operation is square root as it is a unary operation not taking in second operand.
Reading the 3rd operand and operator is continued until it is requested to stop.

Related

How to store multiple string inputs and displaying it after

I'm just starting in studying C++, and I am doing a simple challenge which is GWA Calculator, but I am having a problem finding out how to store the multiple strings input (which is the Subjects/Course) and displaying it after together with the Units and Grades. I am really sorry, but I tried finding out how and I couldn't find an answer. Hope you can help me out.
#include <stdlib.h>
using namespace std;
void calculateGWA();
int main()
{
system("cls");
int input;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| GWA Calculator |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| MENU:\t\t\t\t\t\t\t " << "|" << endl;
cout << "\t\t| 1. Calculate GWA (General Weighted Average)\t\t " << "|" << endl;
cout << "\t\t| 2. Calculate CGWA (Cummulative Weighted Average) " << "|" << endl;
cout << "\t\t| 4. Exit Application\t\t\t\t\t " << "|" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
sub:
cout << "\t\tEnter your choice: ";
cin >> input;
switch(input)
{
case 1:
calculateGWA();
break;
case 2:
//calculateCGPA();
break;
case 3:
main();
break;
case 4:
exit(EXIT_SUCCESS);
break;
default:
cout << "You have entered wrong input.Try again!\n" << endl;
goto sub;
break;
}
}
void calculateGWA()
{
int q;
system("cls");
cout << "-------------- GWA Calculator -----------------"<<endl;
cout << " How many course(s)?: ";
cin >> q;
char c_name[50];
float unit [q];
float grade [q];
cout << endl;
for(int i = 0; i < q; i++)
{
cout << "Enter the Course Name " << i+1 << ": ";
cin >> c_name;
cout << "Enter the Unit " << c_name << ": ";
cin >> unit[i];
cout << "Enter the Grade " << c_name << ": ";
cin >> grade[i];
cout << "-----------------------------------\n\n" << endl;
}
float sum = 0;
float tot;
for(int j = 0; j < q; j++)
{
tot = unit[j] * grade[j];
sum = sum + tot;
}
float totCr = 0;
for(int k = 0; k < q; k++)
{
totCr = totCr + unit[k];
}
system("cls");
// PRINTS OUT THE COURSES - UNITS - GRADES AND GWA //
cout << "\t\t =============================================================== " << endl;
cout << "\t\t| COURSE | UNIT | GRADE |" << endl;
cout << "\t\t =============================================================== " << endl;
cout << "Total Points: " << sum << " \n Total Credits: " << totCr << " \nTotal GPA: " << sum/totCr << " ." << endl;
cout << c_name << "\n" << endl;
cout << "===================================" << endl;
sub:
int inmenu;
cout << "\n\n\n1. Calculate Again" << endl;
cout << "2. Go Back to Main Menu" << endl;
cout << "3. Exit This App \n\n" << endl;
cout << "Your Input: " << endl;
cin >> inmenu;
switch(inmenu)
{
case 1:
calculateGPA();
break;
case 2:
main();
break;
case 3:
exit(EXIT_SUCCESS);
default:
cout << "\n\nYou have Entered Wrong Input!Please Choose Again!" << endl;
goto sub;
}
}

different option throw an unexpected error

when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
here's my program:
void avail_art(void)
{
char c,con;
int quantity,disp=MAX;
bool b = true;
map<string, unordered_set<int>>m{
{"Mouse",{120,disp}},
{"Keyboard",{75,disp}},
{"Monitor",{750,disp}},
{"GPU",{1200,disp}},
{"CPU",{1000,disp}}
};
auto it = m.find("CPU");
cout << "M - for mouse." << endl;
cout << "K - for keyboard." << endl;
cout << "R - for monitor." << endl;
cout << "G - for GPU." << endl;
cout << "C - for CPU." << endl;
cout << "Q - for quit." << endl;
cout << "======================" << endl;
while (b)
{
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 'M':
it = m.find("Mouse");
cout << "You've choosen the Mouse!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
l:
cout << "Enter the quantity: ";
cin >> quantity;
if (quantity > * (it->second.find(disp)))
{
cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
goto l;
}
cout << "Confirm? y/n: ";
cin >> con;
if (con == 'y')
{
auto its=it->second.find(disp);
it->second.insert(disp=(*its - quantity));
it->second.erase(its);
}
break;
case 'K':
it = m.find("Keyboard");
cout << "You've choosen the Keyboard!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Confirm? y/n :";
cin >> con;
if (con == 'y')
{
auto its = it->second.find(disp);
it->second.insert(disp = (*its - quantity));
it->second.erase(its);
}
break;
}
}
}
can someone help me please

How to change the program to enter the data with spaces while using fstream

void Motherboards::add()
{
char x;
int X;
fstream InFileMB("Motherboard_List.txt", ios::in | ios::out | ios::app);
if (!InFileMB)
{
cerr << "Error: Opening File Failed !!";
}
else
{
MotherBoardEntry:
system("cls");
cout << " Enter the name of the Manufacturer :\n";
string MB__Name_Manufacturer;
cin >> MB__Name_Manufacturer;
cout << " Enter Chipset Type :\n";
string MB_Chip;
cin >> MB_Chip;
cout << "Enter Name of the board exactly as it`s written on the box :\n";
string MB_Name;
cin >> MB_Name;
cout << "Enter 3 features of this board :\n";
cout << "1.) ";
string MB_Feature1;
cin >> MB_Feature1;
cout << endl;
cout << "2.) ";
string MB_Feature2;
cin >> MB_Feature2;
cout << endl;
cout << "3.) ";
string MB_Feature3;
cin >> MB_Feature3;
cout << endl;
cout << "Enter Price :\n";
string MB_Price;
cin >> MB_Price;
system("cls");
cout << "Please check the details before conformation :\n";
cout << "Motherboard :\n";
cout << MB__Name_Manufacturer << endl;
cout << MB_Chip << endl;
cout << MB_Name << endl;
cout << "Features :\n";
cout << "1.) " << MB_Feature1 << endl;
cout << "2.) " << MB_Feature2 << endl;
cout << "3.) " << MB_Feature3 << endl;
cout << "Price :\n";
cout << MB_Price;
cout << "\n\n Do You Want To Add The Following Motherboard To Your Stock List ? (y/n)";
cin >> x;
system("cls");
switch (x)
{
case 'y':
InFileMB << "Motherboard :\n" << MB__Name_Manufacturer << endl << MB_Chip << endl << MB_Name << endl << "Price :\n" << MB_Price << endl << "Features :\n" << "1.) " << MB_Feature1 << endl << "2.) " << MB_Feature2 << endl << "3.) " << MB_Feature3 << endl << endl;
break;
case 'Y':
InFileMB << "Motherboard :\n" << MB__Name_Manufacturer << endl << MB_Chip << endl << MB_Name << endl << "Price :\n" << MB_Price << endl << "Features :\n" << "1.) " << MB_Feature1 << endl << "2.) " << MB_Feature2 << endl << "3.) " << MB_Feature3 << endl << endl;
break;
case 'n':
system("cls");
system("pause,2");
cout << " Enter ( 1 ) to try to enter the data again or ( 2 ) to go back to item selection list .. ";
cin >> X;
switch (X)
{
case 1:
goto MotherBoardEntry;
break;
case 2:
break;
}
break;
case 'N':
system("cls");
system("pause,2");
cout << " Enter ( 1 ) to try enter the data again or ( 2 ) to go back to item selection list .. ";
cin >> X;
switch (X)
{
case 1:
goto MotherBoardEntry;
break;
case 2:
break;
}
break;
}
}
}
When I input the data as I am going to show in the snapshots that I provide when I enter the data without any spaces all goes well but when i do enter the data with spaces i cant fill out certain data due to some reason that I don't know as I am a beginner. Can you please tell me exactly where to update and what to update.
[enter image description here][1]
[enter image description here][2]
As you may have noticed in the picture where i have put a space between i7 and 4790k i couldn`t fill out the information for entering the name of the processor and the command window shifted me directly to the next line.

Program builds but displays nothing

I am working on an assignment. The problem I am having is every time I try to run my program to see what it displays, nothing shows up on the command prompt. However, if I press any key and then enter, the program starts looping uncontrollably. The program doesn't even display the initial cout message, just a blinking "_". Thanks
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu();
void SizePrices();
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
double FlavorChoice=0;
int SizeChoice;
int PizzaCountP=(cin >> PizzaCountP, PizzaCountP);
int PizzaCountM = (cin >> PizzaCountM, PizzaCountM);
int PizzaCountL = (cin >> PizzaCountL, PizzaCountL);
int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL);
double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
cout << "Welcome to Joes pizza place!" << endl;
do{
PizzaMenu();
cout << "\nPlease chose a pizza from the menu(1-6): ";
cin >> FlavorChoice;
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? "; cin >> PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?"; cin >> PizzaCountM;
break;
case 3:
cout << "How many large pizzas?"; cin >> PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)"; cin >> SizeChoice;
break;
}
}
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
printf("Your total is: %a", orderTotal);
}
} while (FlavorChoice != 6);
cout << "Thank you for visiting Joes place pizza! "<<endl;
}
void PizzaMenu()
{
cout << "\nSpecialty Pizza Menu" << endl;
cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl;
}
void SizePrices()
{
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
There were a few logical errors in the program. Right now, it should work fine...
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu()
{
cout << "Specialty Pizza Menu:" << endl;
cout << "1) Pizza 1" << endl << "2) Pizza 2" << endl << "3) Pizza 3" << endl << "4) Pizza 4" << endl << "5) Pizza 5" << endl << "6) Exit" << endl;
}
void SizePrices()
{
cout << "Size Prices:" << endl;
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
int FlavorChoice = 0;
int SizeChoice = 0;
int PizzaCountP = 0;
int PizzaCountM = 0;
int PizzaCountL = 0;
int PizzaCountXL = 0;
double orderTotal = 0.0;
cout << "Welcome to Joes pizza place!" << endl;
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
while(FlavorChoice != 6) {
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? ";
cin >> PizzaCountP;
orderTotal += personal * PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?";
cin >> PizzaCountM;
orderTotal += medium * PizzaCountM;
break;
case 3:
cout << "How many large pizzas?";
cin >> PizzaCountL;
orderTotal += large * PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?";
cin >> PizzaCountXL;
orderTotal += xlarge * PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)";
cin >> SizeChoice;
break;
}
}
// orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
// printf("Your total is: %a", orderTotal);
cout << "Your total is: $" << orderTotal << endl;
}
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
}
cout << "Thank you for visiting Joes place pizza! " << endl;
// system("pause");
return 0;
}

While loop and boolean function

Hello guys I'm not an expert on the subject so please excuse my pour skills. I finished my program and it works fine (calculator). The problem is that now I don't know where to locate the while loop in conjunct with the Boolean function to repeat the process once it is done with a task (once the program completes a math operation). Any help, comment or suggestion will be greatly appreciated. Thank you.!!
#include <iostream>
#include <math.h>
#include <cmath>
int main()
{
double a=0.0;
double b=0.0;
double c=0.0;
bool repeat = true;
do {
using namespace std;
int x;
cout << "**********************************" << endl;
cout << "| |" << endl;
cout << "| 0 - Quit |" << endl;
cout << "| 1 - Add |" << endl;
cout << "| 2 - Subtract |" << endl;
cout << "| 3 - Divide |" << endl;
cout << "| 4 - Multiply |" << endl;
cout << "| 5 - Raise X to the power Y |" << endl;
cout << "| 6 - Sine ( x ) |" << endl;
cout << "| 7 - Cosine ( x ) |" << endl;
cout << "| 8 - Tangent ( x ) |" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl;
cin >> x;
switch (x)
{
{
case 1:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter second value " << endl;
cin >> b;
c=a+b;
cout << "The addition of " << a << " and "<< b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 2:
cout << " Enter the first value" << endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a-b;
cout << "The subtraction of " << a << " and " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 3:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a/b;
cout << " The division os " << a << " and " << b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 4:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a*b;
cout << " The product of " << a << " times " << b << " is " << c << endl;
break;
bool repeat = true;
}
{
case 5:
cout << " Enter the value to be exponentiated " <<endl;
cin >> a ;
cout << " Enter the exponent" << endl;
cin >> b;
c= pow(a,b);
cout << a << " Rased to the power of " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 6:
cout << " Enter the value that you want the sine to be taken of" <<endl;
cin >> a ;
c=sin(a);
cout << " The sine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 7:
cout << " Enter the value that you want the cosine to be taken of" <<endl;
cin >> a ;
c=cos(a);
cout << " The cosine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 8:
cout << " Enter the value that you want the tangent to be taken of" <<endl;
cin >> a ;
c=tan(a);
cout << " The tangent of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
return 0;
}
So here is few moments.
Call
using namespace std;
just believe - is bad idea;
In conditions like if() or while() use operator == instead of =. Because "=" - is assigne operator, and return value depended on success of operation. And "==" is compare operator.
Ow and figure one more missunderstanding. Using bool rezult = true; is wrong. You should use rezult = true; Because every time when you write type specifer you create local variable in context of case, and this don`t affect rezult declared in main
My opinion for your question is little change:
from:
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
to
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
repeat = false;}
break;
}
} while (repeat == true);
and if you need a bit more calculations you can wrapped it to new cicle something like:
while(new_condtion == true) {
do {
...
} while(repeat == true);
//change new_condtion
}
Don't redefine repeat within switch case. This creates a different variable named repeat which, although it has the same name, is not the variable named repeat defined before the loop. This is what you get when you copy a definition of the form bool repeat = true; into multiple places.
The continuation condition for the loop (repeat = true) will also loop forever. Comparison is two = signs, not one.