I am fairly new to programming but have been working on putting together a group of functions into one program. It is coming along nicely, but for some reason I can't get the loop on the divisibility function to work properly. When you prompt it to quit to return to the main menu, it interprets q as something you want to know divisibility for, not a prompt to quit. WHY?????
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int divisCalc();
int calculator();
void enterPrompt();
int byteSize();
int main1()
{
do
{
cout << "\n\n~~\n~~\n~~\n~~ \n~~\n\nPress 1 for a calculator\n" << endl;
cout << "Press 2 to test a number for divisibility\n" << endl;
cout << "Press 3 to check size of keyboard character\n" << endl;
unsigned char promptNum;
cin >> promptNum;
cout << "\n" << endl;
cout << "\b" << endl;
switch(promptNum)
{
case '1':
{ do { calculator(); }while(1 == 1); break; }
case '2':
{ do { divisCalc(); }while(1 == 1); break; }
case '3':
{ do { byteSize(); }while(1 == 1); break; }
default:
{ cout << "\nError - choice not recognized\n" << endl; break; }
}}while(1 == 1);
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
void enterPrompt()
{
cout << "\nYou may press 'q' at any time to return to the main menu. " <<
"\n\nPress Enter to continue. . . . .\n\n" << endl;
cin.clear();
cin.ignore(255, '\n');
cin.get();
}
int byteSize()
{
enterPrompt();
do
{
cout << "\t\tEnter any character or number: \n" << endl;
signed char promptChar;
cin >> promptChar;
if(promptChar == 'q')
{
main1();
}
cout << "\n" << promptChar << " takes " << sizeof(promptChar) << " byte(s) of memory.\n" << endl;
}while(1 == 1);
}
int divisCalc()
{
enterPrompt();
long divisNum;
char chdivisNum;
chdivisNum = divisNum;
do
{
cout << "\t\tEnter any number to be tested: \n" << endl;
cin >> divisNum;
switch(divisNum)
{ case 'q': main1(); }
//^^ I only have that as switch because i couldn't get if to work... but neither
// does switch (at least not properly)
cout << "\n" << divisNum << " is divisible by: \n" << endl;
if(divisNum % 2 == 0)
cout << "2\n\n";
if(divisNum % 3 == 0)
cout << "3\n\n";
if(divisNum % 4 == 0)
cout << "4\n\n";
if(divisNum % 5 == 0)
cout << "5\n\n";
if(divisNum % 6 == 0)
cout << "6\n\n";
if(divisNum % 7 == 0)
cout << "7\n\n";
if(divisNum % 8 == 0)
cout << "8\n\n";
if(divisNum % 9 == 0)
cout << "9\n\n";
if(divisNum % 10 == 0)
cout << "10\n\n";
if(divisNum % 11 == 0)
cout << "11\n\n";
if(divisNum % 12 == 0)
cout << "12\n\n";
if(divisNum % 13 == 0)
cout << "13\n\n";
if(divisNum % 14 == 0)
cout << "14\n\n";
if(divisNum % 15 == 0)
cout << "15\n\n";
if(divisNum % 16 == 0)
cout << "16\n\n";
if(divisNum % 17 == 0)
cout << "17\n\n";
if(divisNum % 18 == 0)
cout << "18\n\n";
if(divisNum % 19 == 0)
cout << "19\n\n";
if(divisNum % 20 == 0)
cout << "20\n\n";
}while(1 == 1);
}
int calculator()
{
enterPrompt();
do
{
cout << setprecision(16);
cout << "\t\tEnter any expression with +, -, *, /, ^, %,\n\n\t\t\tor _ " <<
"for square roots \n" << endl;
char operation;
double dvar1, dvar2;
cin >> dvar1 >> operation >> dvar2;
cout << " " << endl;
if(dvar2 == 0)
{
cout << "Undefined Error - Connot divide by Zero\n\n\n" << endl;
}
if(dvar1 == 'q')
{ main1(); }
// ^^ quit function doesn't work! (properly)
switch(operation)
{
case '*':
cout << "= " << dvar1 * dvar2 << "\n\n\n" << endl; break;
case '/':
cout << "= " << dvar1 / dvar2 << "\n" << "OR " << (int)dvar1 / (int)dvar2 <<
" with a remainder of " << (int)dvar1 % (int)dvar2 << "\n\n\n" << endl; break;
case '+':
cout << "= " << dvar1 + dvar2 << "\n\n\n" << endl; break;
case '-':
cout << "= " << dvar1 - dvar2 << "\n\n\n" << endl; break;
case '=':
if(dvar1 == dvar2)
{cout << "Yes, it does!\n\n\n" << endl;}
else
{cout << "No, it doesn't!\n\n\n" << endl;}
case '^':
cout << "= " << dvar1 * ((dvar2 - 1) * dvar1) << "\n\n\n" << endl; break;
case '%': // ^^ exponent function doesn't work!
cout << "= " << (dvar1 / 100) * dvar2 << "\n\n\n" << endl; break;
case '_':
cout << "= " << sqrt(dvar1) << "\n\n\n" << endl; break;
default:
cout << "Error - Operator not accepted\n\n\n" << endl; break;
return 0;
}}while(1 == 1);
}
int main()
{
cout << "~Press 1 for calculator~\n" << endl;
cout << "~Press 2 to test a number for divisibility~\n" << endl;
cout << "~Press 3 to check size of keyboard character~\n" << endl;
unsigned char promptNum;
cin >> promptNum;
cout << "\b" << endl;
switch(promptNum)
{
case '1':
{ do { calculator(); }while(1 == 1); break; }
case '2':
{ do { divisCalc(); }while(1 == 1); break; }
case '3':
{ do { byteSize(); }while(1 == 1); break; }
default:
{ cout << "\nError - choice not recognized\n" << endl; break; }
}
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
I am having trouble with three things in particular:
1) the 'press q to quit' part of the calculator. whenever i try to trigger it it enters an inifinite loop of 'error - operator not accepted'
2) the 'press q to quit' part of the divisibility calculator. same problem as above... with infinite looping and such when prompted to quit. the thing is that although the function of byteSize (the one that tells you how many bytes it takes to store a character on the keyboard) is the only function that doesn't really do what I want it to, but the quit function works... -.-
3) my exponent (^) function of my calculator does not work properly. I don't know why, but it just gives wrong answers