+ 26

How to correct this code?[SOLVED]

#include <iostream.h> #include<conio.h> class Power { double b; int e; double val; public: Power(double base, int exp); double getPower() { return val; } }; Power::Power(double base, int exp) { b = base; e = exp; val = 1; if(exp == 0) return; for( ; exp > 0; exp--) val = val * b; } int main() { clrscr(): Power x(4.0, 2), y(2.5, 1), z(5.0, 0); cout << x.getPower() << " "; cout << y.getPower() << " "; cout << z.getPower() << endl; getch(); return 0; } https://www.sololearn.com/post/26133/?ref=app

22nd Aug 2018, 4:23 PM
💞ⓢⓦⓐⓣⓘ💞
💞ⓢⓦⓐⓣⓘ💞 - avatar
18 Réponses
+ 16
#include <iostream> #include <cstdlib> #include <stdio.h> using namespace std; class Power { double b; int e; double val; public: Power(double base, int exp); double getPower() { return val; } }; Power::Power(double base, int exp) { b = base; e = exp; val = 1; if(exp == 0) return; for( ; exp > 0; exp--) val = val * b; } int main() { system("CLS"); Power x(4.0, 2), y(2.5, 1), z(5.0, 0); cout << x.getPower() << " "; cout << y.getPower() << " "; cout << z.getPower() << endl; getchar(); return 0; } // Replaced clrscr() with system("CLS") included in cstdlib library in c++. Since clrscr won't work on VS c++ and neither in Sololearn CPG // Replaced getch() with getchar() which is included in stdio.h library, because AFAIK conio.h library doesn't work in SoloLearn's CPG
23rd Aug 2018, 9:42 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 23
Thank you very much friends for your help. I am learning OOPS concept. ..that's why ^-^
24th Aug 2018, 5:16 PM
💞ⓢⓦⓐⓣⓘ💞
💞ⓢⓦⓐⓣⓘ💞 - avatar
+ 15
💞ⓢⓦⓐⓣⓘ💞 I have edited your code according to sololearn try this https://code.sololearn.com/clm5l8noekFK/?ref=app
22nd Aug 2018, 5:54 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 14
C++ Soldier (Babak) thanks for letting me know it. I didn't knew that system("CLS") is windows specific. well there is another way it doesn't really clears the screen but prints certain number of new lines and the console gets cleared just like in linux terminals cout << string(100, '\n');
23rd Aug 2018, 1:21 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 10
nAutAxH AhmAd system ("CLS") is a non-portable and Windows-specific system call to clear the screen which also doesn't work on SL's sandboxed compiler. clrscr() and other I/O utility functions in conio.h "compiler extension" is an obsolete facility in almost all modern compilers.
23rd Aug 2018, 9:59 AM
Babak
Babak - avatar
+ 8
Good work 💞ⓢⓦⓐⓣⓘ💞 The program won't give the correct result for negative exponents. To make it happen, you would change the constructor's body to this Power::Power(double base, int exp) { b = base; e = exp; val = 1; bool isNeg = false; if (exp < 0) { exp *= -1; isNeg = true; } else if (exp == 0) return; for ( ; exp > 0; exp--) val = val * b; if (isNeg) val = 1 / val; }
23rd Aug 2018, 7:24 AM
Babak
Babak - avatar
+ 8
nAutAxH AhmAd Good solution. Clear screen effect can be emulated in a variety of ways, indeed. Using well-known libraries like Ncurses/PDCurses ¹ provides cross-platform API to create decent GUI for console-based applications (although using them just for clearing the screen is overkill). ____ ¹ https://en.m.wikipedia.org/wiki/Ncurses
23rd Aug 2018, 3:42 PM
Babak
Babak - avatar
+ 6
First of all, thank you Kirk for your constructive comment. SLs won't accept the sequence and my first guess is, the terminal driver has restricted for interpreting the sequence. On a Linux system with GCC installed on it, the conio.h has two key auxiliary libraries, <sys/select.h> and <termios.h>. And as expected in clrscr()'s body there's nothing but printf("\x1B[2J\x1B[0;0f"); My second guess is, since SL is incorporating GCC on a Windows Server machine to compile and run the code (and naturally doesn't accept those two headers), there's a lack of interface to communicate with the terminal. Am I correct in this regard or is there some other factors are interfering with this process?
24th Aug 2018, 7:46 AM
Babak
Babak - avatar
+ 3
replace clrscr(): with clrscr(); or better use. system('CLS');
24th Aug 2018, 3:03 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
+ 3
[meta: ANSI escapes] The ANSI escape sequence for clearscreen works in the OSs and IDEs in this thread (+ more). // clears terminal printf("\033[2J"); // if necessary, home cursor to 0,0 printf("\033[0;0H"); SoloLearn is excepted from these terminal commands (but it's not a bug). It's tricky to find related Q&A's so I can link a few if asked.
24th Aug 2018, 4:38 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
slove this program input:10342 output:Ten thousand three hundred forty two
24th Aug 2018, 7:19 AM
Kartikey Gupta
Kartikey Gupta - avatar
+ 3
can you help?
24th Aug 2018, 2:11 PM
Muhammad Fiqih Novyastanto
Muhammad Fiqih Novyastanto - avatar
+ 2
eu la
24th Aug 2018, 3:38 PM
Alexandre Douglas
Alexandre Douglas - avatar
+ 2
C++ Soldier (Babak) ANSI: Yep, those are alternative sequences to the same effect. In the context of a workstation you're asking the right question, but neither SoloLearn nor our mobile devices are workstations.
25th Aug 2018, 1:10 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
LOL I DONT EVEN UN DERSTAND
26th Aug 2018, 9:40 AM
Little Cupcake
Little Cupcake - avatar
+ 2
plz upvote this because i want to get a badge
26th Aug 2018, 9:41 AM
Little Cupcake
Little Cupcake - avatar
+ 1
how to develop yhe while loop programme
25th Aug 2018, 5:37 PM
Kuhile Suvarna Bhaginath
+ 1
please accept the question
25th Aug 2018, 5:38 PM
Kuhile Suvarna Bhaginath