[Help] Please Review my Program..

Hi guys,

Please help me with this one. First of all, here's my 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
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
int main( )
	{
	    char expstr[80], expstrDerivative[80], bracketAns;
		char functionAns, functionAdd[80];
	    int NLmethod, iterationNum = 0, bracketNum;
		int secantMethod, choiceAns, illinoisdecimal;   
	    double errorp, errorperv, bracketSize, guess;
		double guess1, x1, x2, x3, fx1, fx2, fx3, xtemp, h, fval, derivative, errorptolerance;    
		double bowl1, bowl2, bowl3, bowl4, adderFunction, adderFunction1;
		
		analyzer equation;
		
	    {
	        
			std::cout << "Enter Equation: " << endl << endl << "     f(x) = ";
	        cin.getline( expstr, 79 );
	     	
	        repeatMethod:
			clrscr();			 
			std::cout << endl << endl << "Evaluate Expression: " << endl << endl << "    f(x) = " << expstr;
			
			std::cout << " - sinh(x)";
			
			cout << "\n\n by " ;
	        
	        if( *expstr == '.' )
	        	{
	 		      return 0 ;
	        	}
	    
		 	cout << endl << endl;
	       	std::cout << "     1. Newton's Method / Newton-Raphson Method" << endl;
	       	std::cout << "     2. Illinois' Method / Modified Regula-Falsi Method" << endl;
	       	std::cout << "     3. Secant Method (Backward, Central, Forward)" << endl;
	       	std::cout << "\n    Enter method: ";
			cin >> NLmethod; 
			NLmethod = NLmethod + 3;
			
			std::cout << "\n\n    (Input 0 for Approx Rel. Error = 0.0000000001 %)";
			std::cout << "\n    Enter Approximate Relative Error Tolerance: ";
			cin >> errorptolerance;
			
			if(errorptolerance <= 0)
			{
				errorptolerance = 0.0000000001;
			}
		
			clrscr();   
			
			do 
				{
					cout << "\n\n    f(x) = " << expstr;
					
					cout << " - sinh(x)";                         //special case..
					
					cout << "\n\n    Initiate Incremental Search Method? (Y/N): ";
					cin >> bracketAns;
										
					if (bracketAns == 'Y' || bracketAns == 'y')
						{
							cout << endl << "    Enter lower limit: ";
							cin >> x1;
							cout << endl << "    Enter upper limit: ";
							cin >> x2;
							cout << endl << "    Enter number of Brackets: ";
							cin >> bracketNum;
							
							if ( x1 >= x2 || bracketNum <= 0)
							{
								cout << endl << endl << "Input ERROR!";
								pause;
							}
							
							double bracket[bracketNum], fxVal[bracketNum];
							
							bracketSize = (x2 - x1) / abs(bracketNum);
							
							cout << endl << endl << "    Lower Bracket                Average                      Upper Braket";
							
							for (int y = 0; y < bracketNum; y++)
							{
								bracket[y] = x1 + (y * bracketSize);
														
								bracket[y+1] = x1 + ((y+1) * bracketSize);
							
								if ((equation.sniff( expstr, bracket[y]) - sinh(bracket[y])) * (equation.sniff( expstr, bracket[y+1]) - sinh(bracket[y+1])) <= 0)     /// Special Case 
								{
									cout << std::fixed;
									cout << std::setprecision(5) << "\n    " << bracket[y];
									cout <<"                      " << (bracket[y] + bracket[y+1])/2  ;
									cout <<"                      " << bracket[y+1];
								}
							}
							cout << endl << "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::";
						}                    
	
					else if (bracketAns == 'N' || bracketAns == 'n')
						{
							break;
						}
					else 
						{
							clrscr();
							cout << "Invalid Input!";
						}
								
				} while ((bracketAns != 'Y') || (bracketAns != 'y') || (bracketAns != 'N') || (bracketAns != 'n'));      
								
								   
			switch (NLmethod)
	              {
		
	                  case NEWTON:
					  	   	{
								clrscr();
							 	cout << "\n\n**\\\\ Newton-Raphson Method //**" ;
							 	cout << endl << endl << endl;
							 	cout << "    f(x) = " << expstr << " - sinh(x)" << endl << endl;  
							 	
							 	cout << endl << "   Enter derivative function, f'(x) = ";
							 	cin >> expstrDerivative;
							 									
								cout << endl << "    f'(x) = " << expstrDerivative << " - cosh(x)"; //special Case
								
								pause;
							
								clrscr();
								cout << "\n\n**\\\\ Newton-Raphson Method //**" << endl << endl << endl;
								cout << "    f(x) = " << expstr << " - sinh(x)"<< endl << endl;   					// Special Case 
								cout << "    f'(x) = " << expstrDerivative << " - cosh (x)" << endl << endl;  		// Special Case
								
							 	cout << endl << "  Enter Starting Value: ";
							 	cin >> guess;
							 	
							 	cout << endl << endl <<"  Iteration #             X                  Approx Relative Error (%)";
								
								iterationNum = 0;
								errorperv = errorptolerance;
								do
								{
										
										fval = equation.sniff( expstr, guess ) - sinh(guess);
										derivative = equation.sniff (expstrDerivative, guess) - cosh(guess);
										
							 	 		cout << endl << endl << "       " << iterationNum;
							 	 			
							 	 		cout << "            ";
							 	 		std::cout << std::fixed << std::setprecision(15) << guess;
										
							 	 		guess1 = guess - (fval/derivative);
							 	 		errorp = abs((guess1 - guess)/guess1)*100;
							 	 							 	 		
							 	 		if (iterationNum < 1)
								 	 		{
									 	 		cout << "           -----------";
											}
											
							 	 		else if (iterationNum >= 1)
							 	 			{
												cout << "           ";
												cout << std::setprecision(4) << errorperv;
											}
										
										if (iterationNum > 10000)
											{
												cout << "\n\n ::::::::::::::::::::::::::::: END OF ANALYSIS ::::::::::::::::::::::::::::::::";
												cout << "\n\n :::::::::::::::::::::::: Divergent at Value Given ::::::::::::::::::::::::::::\a";
												break;
											}
										errorperv = errorp;		
										iterationNum++;
										
										guess = guess1;
										
										
								}while (errorp >= errorptolerance);
								
								cout << "\n\n ::::::::::::::::::::::::::::: END OF ANALYSIS ::::::::::::::::::::::::::::::::\a";
								break;
	   	  		           	}
	   	 
	   	  		           	


Thats not all the code.
Lines 15 and 16 works fine with spaces as input.
Why does line 120 and 121 returns error if a space is inputted. Sometimes, Dev C++ does not read it at all.

Can you help me?

Another thing, how can I parse sinh(x) such that it will solve for x and returns sinh of the function.

Any advise will be appreciated. :)
Why does line 120 and 121 returns error if a space is inputted
operator >> will try to read input to a variable untill whitespace is encountered. That is how formatted input works. Use std::getline() instead.

Another thing, how can I parse sinh(x) such that it will solve for x and returns sinh of the function.
Any advise will be appreciated. :)
Get a math parsing library.
When I use std::getline, the compiler will skip through that code and will not execute that command. :(
Got through your suggestion MiiNiPaa.. Thank you.. :)
But cant understand. I only have basic knowledge about C++.. :(


Edit: Yeah! I did it after quite some reading.. :)
I just forgot to include <limits> in the header files.

Thank you MiiNiPaa.. :)
Last edited on
Topic archived. No new replies allowed.