I am using Dev C++ and I have been told not to use the system("PAUSE") because my antivirus was flagging my code. I am using cin.get(); instead, but I still cannot see the output.
What am I doing wrong?
Code:
#include<iostream>
#include<cmath>
#include <limits>
using namespace std;
double series(double , int);
double c;
int n;
int main(){
cout<<"Enter 2 numbers, c and n :";
cin>> c >> n;
series(c,n);
cin.get();
//std::cout << "Press ENTER to continue...";
//std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
};
double series(double c, int n){
double term=0;
double sum=0;
for(int i=0;i<=n;i++){
term=pow(c,i) ;
sum+=term;
}
return sum;
will read the two numbers but not the terminating lf/cr. This is still in the buffer. So when you do
Code:
cin.get();
after outputting the answer, this takes the lf/cr already present and hence your program terminates immediately instead of waiting for your input. You need to clear the input of any characters first, reset any error on input and then get the input.
Code:
int main() {
char ch;
cout << "Enter 2 numbers, c and n :";
cin >> c >> n;
cout << series(c,n) << endl;
cin.clear();
while (cin.get(ch) && (ch != '\n'));
cout << "Press ENTER to continue...";
cin.clear();
cin.get();
return 0;
}
will read the two numbers but not the terminating lf/cr. This is still in the buffer. So when you do
Code:
cin.get();
after outputting the answer, this takes the lf/cr already present and hence your program terminates immediately instead of waiting for your input. You need to clear the input of any characters first, reset any error on input and then get the input.
Code:
int main() {
char ch;
cout << "Enter 2 numbers, c and n :";
cin >> c >> n;
cout << series(c,n) << endl;
cin.clear();
while (cin.get(ch) && (ch != '\n'));
cout << "Press ENTER to continue...";
cin.clear();
cin.get();
return 0;
}
Thanks, it is working now, but what does If/cr stand for?
I am using Dev C++ and I have been told not to use the system("PAUSE") because my antivirus was flagging my code.
Honestly, I didn't know antivirus programs got this sophisticated.
Yes, system("PAUSE") is an easy way for someone to destroy your system. You could download a bogus copy of Dev C++, and in that copy everything is OK, except that there is a "pause.exe" trojan or virus program. So when Dev-C++ automatically creates that "system("pause")" line for you, whenever you run the program, you invoke the trojan/virus.
Yet another reason to avoid "system("pause").
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 11th, 2013 at 03:25 PM.
Thanks for the info, pretty scary in my opinion...
Any C++ IDE that automatically puts "system("pause")" in the code is a candidate for this type of attack. There must be literally thousands of downloads of Dev-C++ (it's old now, so it isn't as popular as it was a few years ago), so this could wreak a lot of havoc on those unfortunate new C++ coders.
I'm a little surprised that the anti-virus people were aware of this -- maybe they got wind of it by a student who had lost a lot of data after running a Dev-C++ generated program.
There was a discussion here some years ago on the "system(pause)" being dangerous, and some (don't remember who now) forum members would advocate its usage because it was "easy" for the beginner student. Well times sure have changed...
There was a discussion here some years ago on the "system(pause)" being dangerous, and some (don't remember who now) forum members would advocate its usage because it was "easy" for the beginner student. Well times sure have changed...
However, the possibility of trojanizing the PAUSE command is an urban legend. At least as of XP SP3 and any earlier version of Windows or DOS I know of, it has always been one of the so-called built-in commands. That means it's hard-coded inside the shell (cmd.exe as of XP) and has precedence over any sort oft executable or batch file. Compromizing that command would only be possible by directly manipulating the shell executable itself. So it's not entirely impossible, but much harder than the legend tells.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Bookmarks