+ 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
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
+ 23
Thank you very much friends for your help.
I am learning OOPS concept. ..that's why ^-^
+ 15
💞ⓢⓦⓐⓣⓘ💞 I have edited your code according to sololearn try this
https://code.sololearn.com/clm5l8noekFK/?ref=app
+ 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');
+ 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.
+ 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;
}
+ 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
+ 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?
+ 3
replace clrscr(): with clrscr();
or better use. system('CLS');
+ 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.
+ 3
slove this program
input:10342
output:Ten thousand three hundred forty two
+ 3
can you help?
+ 2
eu la
+ 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.
+ 2
LOL I DONT EVEN UN DERSTAND
+ 2
plz upvote this because i want to get a badge
+ 1
how to develop yhe while loop programme
+ 1
please accept the question