Why can't I see the output of my C++ code?
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;
Re: Why can't I see the output of my C++ code?
Maybe because you don't have any statements that output anything.
Re: Why can't I see the output of my C++ code?
right, unfortunately, even when I type:
instead of just
I still cannot see the output
Re: Why can't I see the output of my C++ code?
will read the two numbers but not the terminating lf/cr. This is still in the buffer. So when you do
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;
}
Re: Why can't I see the output of my C++ code?
Quote:
Originally Posted by
2kaud
will read the two numbers but not the terminating lf/cr. This is still in the buffer. So when you do
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?
Re: Why can't I see the output of my C++ code?
Not "If" but "lf", i.e., line feed and carriage return, in other words, the new line sequence.
Re: Why can't I see the output of my C++ code?
Quote:
Originally Posted by
math8
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
Re: Why can't I see the output of my C++ code?
Thanks for the info, pretty scary in my opinion...
Re: Why can't I see the output of my C++ code?
Quote:
Originally Posted by
math8
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...
Regards,
Paul McKenzie
Re: Why can't I see the output of my C++ code?
Quote:
Originally Posted by
Paul McKenzie
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...
Could it be you mean this one: http://forums.codeguru.com/showthrea...t-pause-quot-)? Probably the most comprehensive discussion of this subject I've ever seen.
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.