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

The structure of you program overall is a little off. The option to Quit should really only be contained in the main program loop. So you should present all of the options that the program can do (which includes quit) just in the main menu.

Edited 6 Years Ago by hag++: n/a

When I mean to quit, I mean to return to the main menu, not quit the program. Therefore it wouldn't make sense for that command to be in main().

Because comparing the value in dvar1 with 'q' can be dicey, the best solution would be to make a sub menu (I know it's not exactly what you want but aside from reading in your expression as a string and parsing it out that may be the best way).

The menu would say "enter e for an expression and q to quit." If they select 'e' you can move on to 127, if they select q you should return a value (e.g., 0 but it doesn't have to be) so you can return to main1 without having to call it explicitly (when you are calling main1 like that it's a new copy of main1, not the one you originally came from).


Look up for loops for lines 80-117

thanks for the help... I already thought of a sub menu and it didn't quite work... but that was with a do while loop instead of a for loop. will changing the type of loop make the difference?

The for loop was solely for 80-117 and would actually be inside your do/while loop.

You should be able to have something like

//some pseudocode
WHILE(true) //says same as your while(1==1)
{
"Enter 'e' for expression and 'q' to quit"
 READ IN a character
 if(character is 'e')
 {
      //your other code
 }

 else if (character is 'q')
     return 0;  //since your function returns an int

}

Edited 6 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.