How to Restart a Program
Oct 28, 2015 at 12:17am UTC
I have made a calculator that, from the table of contents you can select a calc. However once you make a calc selection you cannot reselect another calculator without closing the program and opening it up again. I want to make it so that the is the user enters 999 as one of the inputs, the program restarts from the beginning.
here is the code if you want to check:
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int ToC;
cout<<"Table of Contents:" <<endl<<endl;
cout<<"Synthetic Division Calc-1" <<endl;
cout<<"Quadratic Equation Calc-2" <<endl;
cout<<"Pythagorean Theorem Calc-3" <<endl;
cout<<"Which Calculator would you like to use?: " ;
cin>>ToC;
cout<<endl<<endl;
//Synthetic Div
double Z = 0.0;
double Y = 0.0;
double T = 0.0;
double D = 0.0;
double E = 0.0;
double R = 0.0;
double SB = 0.0;
double RB = 0.0;
double SC =0.0;
double RC = 0.0;
double SD = 0.0;
double RD = 0.0;
double SE = 0.0;
double RE = 0.0;
double AR = 0.0;
//5 Variables
while (ToC==1)
{
cout<<"Synthetic Division Calculator By Daniel Jomaa" <<endl;
cout<<"Enter Zero for E if there are only 4 Variables" <<endl;
cout<<"A = " ;
cin>>Z;
cout<<"B = " ;
cin>>Y;
cout<<"C = " ;
cin>>T;
cout<<"D = " ;
cin>>D;
cout<<"E = " ;
cin>>E;
cout<<"Remainder: " ;
cin>>R;
cout<<"" <<endl;
if (E==0){
AR = Z;
SB = R * AR;
RB = Y + SB;
SC = RB * R;
RC = T + SC;
SD = RC * R;
RD = SD + D;
cout<<"Coefficients:" <<endl;
cout<<AR<<", " <<RB<<", " <<RC<<", R:" <<RD;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
}
else {
AR = Z;
SB = R * AR;
RB = Y + SB;
SC = RB * R;
RC = T + SC;
SD = RC * R;
RD = SD + D;
SE = RD * R;
RE = SE + E;
cout<<"Coefficients:" <<endl;
cout<<AR<<", " <<RB<<", " <<RC<<", " <<RD<<", R:" <<RE;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
}
}
//Quadratic Calc
double A=0.0;
double B=0.0;
double C=0.0;
double squareroot =0.0;
double negative= 0.0;
double finalanswerone=0.0;
double tophalfone =0.0;
double bottomhalf =0.0;
double tophalftwo =0.0;
double finalanswertwo =0.0;
int Negativex = -1;
double factorxone = 0.0;
double factorxtwo = 0.0;
double imaginarynum = 0.0;
while (ToC==2)
{
cout<<"Quadratic Equation Calculator" <<endl;
cout<<"A = " ;
cin>>A;
cout<<"B = " ;
cin>>B;
cout<<"C = " ;
cin>>C;
squareroot = B* B - 4* A* C;
negative = -1*B;
tophalfone = negative+sqrt(squareroot);
bottomhalf = 2*A;
finalanswerone = tophalfone/bottomhalf;
//first x
if (squareroot<0){
imaginarynum = squareroot * Negativex;
cout<<"X = -" <<imaginarynum<<"i" ;
cout<<"" <<endl;
}
else
cout<<"X = " <<finalanswerone<<endl;
//Second X
tophalftwo = negative-sqrt(squareroot);
finalanswertwo = tophalftwo/bottomhalf;
if (squareroot<0){
imaginarynum = squareroot * Negativex;
cout<<"X = " <<imaginarynum<<"i" ;
}
else
cout<<"X = " <<finalanswertwo<<endl;
cout<<"" <<endl;
cout<<"" <<endl;
}
//Pythag theorem
double AA =0.0;
double BB =0.0;
double CC =0.0;
double Aequalzero =0.0;
double Bequalzero =0.0;
double Cequalzero =0.0;
int error;
while (ToC==3)
{
cout<<"Pythagorean Theorem Calculator by Daniel Jomaa" <<endl;
cout<<"Enter 0 for Unknown Variables" <<endl;
cout<<"" <<endl;
cout<<"Enter A: " ;
cin>>AA;
if (AA!=0)
error++;
cout<<"Enter B: " ;
cin>>BB;
if (BB!=0);
error++;
cout<<"Enter C: " ;
cin>>CC;
if (CC!=0);
error++;
cout<<"" <<endl;
if (error==3){
cout<<"Error; Enter a Zero for one variable" <<endl;
system("pause" );
exit(EXIT_FAILURE);
}
if (AA==0){
Aequalzero = CC*CC-BB*BB;
cout<<"A = " <<sqrt(Aequalzero)<<endl<<endl;
}
if (BB==0){
Bequalzero = CC*CC-AA*AA;
cout<<"B = " <<sqrt(Bequalzero)<<endl<<endl;
}
if (CC==0){
Cequalzero = AA*AA+BB*BB;
cout<<"C = " <<sqrt(Cequalzero)<<endl<<endl;
}
}
system("pause" );
return (0);
}
Oct 28, 2015 at 1:55am UTC
You could use a do-while loop on the program. For example:
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
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
int number{ 0 };
char tryAgain{ ' ' };
do
{
cout << "Please enter a number (0-100)" << endl;
cin >> number;
while (number < 0 || number > 100)
{
cout << "Your input " << number << " is incorrect" << endl;
cout << "Try again" << endl;
cout << "Please enter a number (0-100)" << endl;
cin >> number;
}
cout << "Would you like to try again?" << endl;
cin >> tryAgain;
} while (toupper(tryAgain) == 'Y' );
cout << "Good Bye!" << endl;
return 0;
}
The program asks the user to input a number, validates the number according to the parameters and then ask the user if the user would like to try again. If the user press y, then the program will restart. If the user press n, the program will close.
I hope it helps.
Oct 29, 2015 at 12:48am UTC
Well i wanted it so that the user must only enter something if he/she wants to restart the program. otherwise it should loop as usual.
Thanks
Topic archived. No new replies allowed.