Math Solver problems.

Pages: 12
Well im trying to make a math solver program that will solve almost any math question its not currently complete but i want to make it so you dont have to always close and reopen the program.
This is my full code so far
(EDIT)
I Have Replaced the default code with my edited one so it now has the infinate loop and the sqrt code plugged in


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

/* Math Solver with switch cases */

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main(){
	while (true) {//This repeats the program. You close window manually by clicking "x".
		system("CLS");
		cin.clear(); // this will clear any values remain in cin from prior run
		int num;
		cout << ">>>>>>>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<<<<<\n";
		cout << "For Shapes type press 1.\n";
		cout << "For Square Roots press 2.\n";
		cin >> num;
		switch(num) {
			case 1:
				cout << "For Area press 1.\n";
				cout << "For Volume press 2.\n";
				cin >> num;
				switch(num) {
					case 1:
						cout << "Area.\n";
						int lg; // this declares a varable
						int wg; // this declares another varable
						cout << "This is a program to find the area of a Rectangle and Square.\n";
						cout << "Enter The Length: ";
						cin >> lg; // input the length
						cout << "Enter the Width: ";
						cin >> wg; // input the width
						cout << "The area is ";
						cout << lg * wg; // compute area
						break;
					case 2:
						cout << "Volume.\n";
						int l;
						int w;
						int h;
						cout << "Enter the Lenght: ";
						cin >> l;
						cout << "Enter the Width: ";
						cin >> w;
						cout << "Enter the Hight: ";
						cin >> h;
						cout << "The Volume is ";
						cout << l * w * h;
						break;
					}
					cout << "\n\n";
					case 2:
						cout << "Square Roots: "; // Currently in progress
						{
							double param, result;
							param = 1024.0;
							result = sqrt (param);
							printf ("sqrt(%lf) = %lf\n", param, result );
						}
						cout << "\n\n";
						break;
		}
		system ("PAUSE");
	}//this ends while loop
	return 0;
}// this ends your main function.

What can i input to change the code to return to the top
also i would like to input a code that if you type Q or another command anywhere in the program it will ask if you want to quit also how can i change my code to be asking to ex: area is a, volume is v, etc.

also if there are any other codes that would be recommened they would be greatly accepted thanks

<EDIT>
This code works completely and is not a school project its just a project that i'm working on by my self if it looks like a school project i'm sorry and if there is a related topic please send the url (link) to the solution
i'm not asking for someone to fix my program but a little assistance
also if you want you can use my code but if you do get it complete please give me a little credit for starting the code and also send me a message if you got any questions.

also i know that i don't have the square root part of the code plugged into this program im currently stuck on that part
Last edited on
For the square root problem, and for any other mathematical problem you haven't implemented yet, you will probably find most of your answers here: http://www.cplusplus.com/reference/clibrary/cmath/

As for the "Q" problem, to do this you're going to need to change all you're input varibles (ike num, lw, wg etc...) to chars, or strings, and then check to see if it is a q and if not, convert to an integer. Reference here: http://www.cplusplus.com/articles/numb_to_text/

Can you explain the "What can i input to change the code to return to the top" problem further?
Well what i want to do is i want this program to return to the top so ex:when i get to the 59th line the program will return to line 1 and restarts the whole program

unless the user inputs the exit sequence or the "kill" code Q.

ps thanks for the fast reply.
In terms of return to line 1 and restarting the whole program, that can't be done. However, the way you have your program structured now is fine for doing this. All you need to do is switch lines 57 and 58 because at the moment you have an infinite loop, that will quit the program actually before the loop finishes.

Also line 1 says you're using an if-else-if ladder, but you're using switch cases instead :p
It's extra brackets. remove { at line 11 and } at line 59.
yea sorry i was learning with different examples and i forgot to delete the if-else-if thing also i looked into the coding for the sqrt code i built it in a different project but it uses allready given numbers how do i change this so the user can input there own numbers?

Thanks also i cant figure out the numb_to_text example can anyone please give a little example
/* Thanks alot guys */

also i have found that when i run this program for some fun little reason when the first case is complete it goes to the second case.

this is the output before any changes to the code:


>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<
For Shapes type press 1.
for Square Roots press 2.
1
For Area press 1.
For Volume press2.
2
Enter the length: 50
Enter the Width: 24
Enter the Hight: 42
The Volume is 50400Sqare Roots: Press any key to continue . . .


What i want to see is

>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<
For Shapes type press 1.
for Square Roots press 2.
1
For Area press 1.
For Volume press2.
2
Enter the length: 50
Enter the Width: 24
Enter the Hight: 42
The Volume is 50400

To exit press 0, To Continue press 1:


the last line in my output is not required now but the rest is
Thanks again
Last edited on
ok heres a little update in my code i have deleated the to brackets in 11 and 59 thanks alot for that but i still have the square root thing automatically coming back on me but thanks alot on the loop fix

i will also edit my code with a updated code and i know that my spelling is bad in this code but when i finally get this program where or close to where i want i will fix my spelling mistakes.

thanks again
also i have found that when i run this program for some fun little reason when the first case is complete it goes to the second case.

You are not breaking after your first case. Your code at the moment looks like this:


case 1:
   case 1:
   break;
   case 2;
   break;

case2 :
   etc...


It should look like:


case 1:
   case 1:
   break;
   case 2;
   break;
break;

case2 :
   etc...
break;


Show us the code you have for the sqrt problem and the number-text conversion problem. Both code examples and documentation are fairly self explanitory.
i'm still fairly new to programming , trying to teach myself c++ and noticed your problem with wanting to have the program return to the beginning to start over again, instead of
1
2
3
4
5
		}
		system ("PAUSE");
	}//this ends while loop
	return 0;
}// this ends your main function. 

you could use
1
2
3
4
5
		}
		system ("PAUSE");
	}//this ends while loop
	return main();
}// this returns you to your main function. 

i have tried this with a prior program experiment and the only problem i come up with using this is to get a pause before the program resets to show any output that you may have when the program finishes, for some reason system("PAUSE"); only pauses for a split second. you may also be able to use it with some sort of input from the user to determine if the program will return 0 or return main(). like i said though i'm still new to C++ but hopefully this helps.

i attempted to compile your code , i'm running ubuntu and using "code::blocks IDE " to compile , but it would not recognize the system("CLS") code for some reason.
Last edited on
No this won't happen, sorry but this is bad information. Frankly, i'm suprised return main(); even compiled.

i attempted to compile your code , i'm running ubuntu and using "code::blocks IDE " to compile , but it would not recognize the system("CLS") code for some reason.

I only wish more people saw this statement because it is evidence for one of the main reasons why people are told not to use system();; it is OS dependent. Hence why a windows command did not work on a linux system,
ok guys heres a little update on what I now have in my code;

1) return to start works
2) finding of area, volume, and dimater of a circle
3) finding of square roots but unfortuantly my code is a little broke and what happens is when you run this program and chose area after it will automatically jump to the finding of square roots, but when you want to find the square roots off the start it automatically skips the square roots and jumps to the find the power of "__" option that i am sorry if it isnt in here yet thats only because im still working on this code at my home and my laptop has gotten infected by a virus and i printed all of my codes off that are up to date i will try to keep in touch and keep everyone upto date as soon as i can but I can only use this site at school or when im at home on my ipod and its a pain on the ipod trust me so i will try to update later to day and there is only one problem that im having with the power thing. the code
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
// ok this is someone elses code but i need to borrow it to remember what im working on
#include <iostream>
#include <cmath> 
using namespace std;

int calc1 (int x){
	return x;
}
double calc2 (double x){
	return sqrt(x*1.0);
}
int calc3 (int x){
	return pow(x, 2.0);
}
int M1 (int x){
	cout << "Number: " << calc1 (x) << endl;
	cout << "Number's square root: " << calc2 (x) << endl;
	cout << "Number squared: " << calc3 (x) << endl;
	cout << "_________" << endl;
	return x;
}
int main (){
	int a = 1;
cout << "enter first num";
cin >> a;
	a = M1 (a);
int b = 2;
cout << "enter 2nd num";
cin >> b;
	
	b = M1 (b);
	int c = 3;
cout << "enter 3rd num";
cin >> c;
	c = M1 (c);
}


ok now look at int calc3 and int M1, i want to put the power method into it but i want to choose the numbers not the first one so i want the output to look something like

enter base #: 5
enter exponet #: 2

the numbers 5^2 is 25

restart Y/N?
y

enter base #: 9
enter exponet #: 9

the numbers 9^9 is 387420489
ok guys like i promised i will update my code heres what i have so far

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
/* Math Solver with switch cases */

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main(){
	while (true) {//This repeats the program. You close window manually by clicking "x".
		system("CLS");
		cin.clear(); // this will clear any values remain in cin from prior run
		int num;
		cout << ">>>>>>>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<<<<<\n";
		cout << "For Formulas press 1.\n";
		cout << "For Advanced Numbers press 2.\n";
		cin >> num;
		switch(num) {
			case 1:
				cout << "For Area press 1.\n";
				cout << "For Volume press 2.\n";
				cout << "For Circumfrence press 3.\n";
				cin >> num;
				switch(num) {
					case 1:
						cout << "Area.\n\n";
						int l; // this declares a varable
						int w; // this declares another varable
						cout << "Enter The Length: ";
						cin >> l; // input the length
						cout << "Enter the Width: ";
						cin >> w; // input the width
						cout << "The area is ";
						cout << l * w; // compute area
						break;
					case 2:
						cout << "Volume.\n\n";
						int l1; // this is the length
						int w1; // this is thw width
						int h1; // this is the height
						cout << "Enter the Length: ";
						cin >> l1;
						cout << "Enter the Width: ";
						cin >> w1;
						cout << "Enter the Height: ";
						cin >> h1;
						cout << "The Volume is ";
						cout << l1 * w1 * h1;
						break;
					case 3:
						cout << "Circumfrence.\n\n"; // ((2(r))pi) or C=2 * pi * R
						int r;
						cout << "Enter the radius: ";
						cin >> r;
						cout << "the circumfrence is ";
						cout << 2 * r * 3.14159;
						break;
					break;
					}
				cout << "\n\n";
				case 2:
					switch(num) {
					case 1:
					cout << "Square Roots: "; // Can not access this anymore
					int n00;
					cout << "Enter the number that you want to know the Square Root of: ";
					cin >> n00;
					{
						double param, result;
						param = n00;
						result = sqrt (param);
						printf ("The square root of (%lf) is %lf\n", param, result );
					}
					cout << "\n\n";
					break;
				case 2:
					cout << "Powers: "; // in progress and need a lot of help in this
					int n01;
					int p01;
					break;
					}
		}
		system ("PAUSE");
	}//this ends while loop
	return 0;
}// this ends your main function. 


ok well this is what i have sofar but why does this program automatically skip line 60-74 and automatically jumps into the powers command the one that is not even in a working state and a little help on what i need to change is needed also how can i retype this whole program in so it takes up a lot less lines like the calc that i borrowed i want to beable to sort all of my codes into different little sections thanks again guys and i know that my typing is kinda choppy but its only because i am playing beat the clock against the world and viruses i will check my emails daily to see if anyone provides any suggestions and i have already looked into books on this site they slightly help but not as much thanks again

Skillet out

<EDIT>
um guys don't bother trying to fix the chioces of 1=sqrt, and 2=power i already fixed it by exploring my code's start and end and fixed what i need to fix it was that i forgot to add the "cout << "1=sqrt, 2=power";" and "cin >> num;" command lol but i really really need help on the power thing if you are really want to know what i want to do with the power things is i want the user to plugin there own numbers so youi can plugin 4^6 or 8^2 or whatever numbers the user wishes to use thanks again
Last edited on
ok to test your code i had to completely remove both of the system() in the program , and i thank you mcleano for informing me about return main() as i said i am still new to C++ and that seems to be an important thing to know. i noticed a problem unless it was meant to run this way, when you enter formulas / area .. add your numbers in and get your total it takes you directly to square roots instead of to the main menu, although after square roots it will take you to the main menu selection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>>>>>>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<<<<<
For Formulas press 1.
For Advanced Numbers press 2.
1
For Area press 1.
For Volume press 2.
For Circumfrence press 3.
1
Area.

Enter the Length: 24
Enter the Width: 12
The area is 288

Square Roots: Enter the number that you want to know the Square Root of: 234
The Square root of (234.000000) is 15.297059


>>>>>>>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<<<<<
For Formulas press 1.
For Advanced Numbers press 2. 


that's the output i get from the program.. looking for the why , but wanted to let you know in case you didn't.
Line 57 meant to be after line 58.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double n01, p01, result; // these must be double or float
n01 = p01 = result = 0; // initializing

std::cout << "Please enter the base: ";
std::cin >> n01;
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // must include <clmits> for this

std::cout << "Please enter exponential: ";
std::cin >> p01;
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); 

result = pow(n01, p01);

std::cout << n01 << "^" << p01 << " = " << result << '\n';
@mobile: Thanks for noticeing my error in my code but im still trying to fix that problem. thanks
@mcleano: i will change the line 57 and 58 and thanks for the help on the power thing it is greatly thanked and im gonna plug in this new info now thanks again.
Well this sucks i think i copyed this code into my code wrong because theres like 16 errors this is my full code
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
/* Math Solver with switch cases */

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main(){
	while (true) {//This repeats the program. You close window manually by clicking "x".
		system("CLS");
		cin.clear(); // this will clear any values remain in cin from prior run
		int num;
		cout << ">>>>>>>>>>>>>>>>>>>Welcome to Math Solver<<<<<<<<<<<<<<<<<<<\n";
		cout << "For Formulas press 1.\n";
		cout << "For Advanced Numbers press 2.\n";
		cin >> num;
		switch(num) {
			case 1:
				cout << "For Area press 1.\n";
				cout << "For Volume press 2.\n";
				cout << "For Circumfrence press 3.\n";
				cin >> num;
				switch(num) {
					case 1:
						cout << "Area.\n\n";
						int l; // this declares a varable
						int w; // this declares another varable
						cout << "Enter The Length: ";
						cin >> l; // input the length
						cout << "Enter the Width: ";
						cin >> w; // input the width
						cout << "The area is ";
						cout << l * w; // compute area
						cout << "\n\n";
						break;
					case 2:
						cout << "Volume.\n\n";
						int l1; // this is the length
						int w1; // this is thw width
						int h1; // this is the height
						cout << "Enter the Length: ";
						cin >> l1;
						cout << "Enter the Width: ";
						cin >> w1;
						cout << "Enter the Height: ";
						cin >> h1;
						cout << "The Volume is ";
						cout << l1 * w1 * h1;
						cout << "\n\n";
						break;
					case 3:
						cout << "Circumfrence.\n\n"; // ((2(r))pi) or C=2 * pi * R
						int r;
						cout << "Enter the radius: ";
						cin >> r;
						cout << "the circumfrence is ";
						cout << 2 * r * 3.14159;
						cout << "\n\n";
						break;
					}
				break;
				cout << "\n\n";
				case 2:
					cout << "sqrt's =1, pwrs=2 ";
					cin >> num;
					switch(num) {
					case 1:
					cout << "Square Roots: "; // Can not access this anymore
					int n00;
					cout << "Enter the number that you want to know the Square Root of: ";
					cin >> n00;
					{
						double param, result;
						param = n00;
						result = sqrt (param);
						printf ("The square root of (%lf) is %lf\n", param, result );
					}
					cout << "\n\n";
					break;
				case 2:
					cout << "Powers: "; // in progress and need a lot of help in this
					double n01, po1, result; // these must be double or float
					n01 = po1 = result = 0; // intializing

					std::cout << "Please enter the Base: ";
					std::cin >> no1;
					std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');

					std::cout << "Please enter Exponntial: ";
					std::cin >> p01;
					std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
					
					result = pow(n01, p01);
					
					std::cout << n01 << "^" << p01 << " = " << result << '\n';
					break;
					}
		}
		system ("PAUSE");
	}//this ends while loop
	return 0;
}// this ends your main function. 

what went wrong i will try to fix what i can and i will update asap

thanks in advance
Skillet
Last edited on
closed account (jwC5fSEw)
Post the errors.
This is the point where I really start liking flex and bison so much more than plain C/C++.

4: You don't use cstdio even once, however you use system() which requires cstdlib (<stdlib.h>).

62: Mal-placed cout.

72 & 77: Redundant brackets.

85-95: You suddenly start using std::something even when you're already using the namespace.

-Albatross

P.S.- Is there any way you can shorten that code?

Last edited on
k i dont know what cstdio or stdlib.h is

what is Mal-placed

??? brackets???

i only started using std::____________; only because someone else told be to try that out

also my errors are:


1>------ Build started: Project: Math Solver, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(84) : error C2371: 'n01' : redefinition; different basic types
1>        c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(82) : see declaration of 'n01'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(85) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(88) : error C2065: 'no1' : undeclared identifier
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(89) : error C2039: 'numeric_limits' : is not a member of 'std'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(89) : error C2065: 'numeric_limits' : undeclared identifier
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(89) : error C2275: 'std::streamsize' : illegal use of this type as an expression
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\iosfwd(25) : see declaration of 'std::streamsize'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(89) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(3390) : see declaration of 'std::max'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(89) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(3382) : see declaration of 'std::max'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(93) : error C2039: 'numeric_limits' : is not a member of 'std'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(93) : error C2065: 'numeric_limits' : undeclared identifier
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(93) : error C2275: 'std::streamsize' : illegal use of this type as an expression
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\iosfwd(25) : see declaration of 'std::streamsize'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(93) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(3390) : see declaration of 'std::max'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(93) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(3382) : see declaration of 'std::max'
1>c:\users\mskillet\documents\visual studio 2008\projects\math solver\math solver\main.cpp(97) : error C2088: '<<' : illegal for class
1>Build log was saved at "file://c:\Users\MSkillet\Documents\Visual Studio 2008\Projects\Math Solver\Math Solver\Debug\BuildLog.htm"
1>Math Solver - 13 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It looks like you can't use n01 as the name of a variable.

I'm not sure why you're using cin.ignore() at all at 87 and 91. If you eliminated those and all the code inside, (and renamed n01 to no1) all your errors would probably be gone.

-Albatross
Pages: 12