-
February 10th, 2013, 04:13 PM
#1
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;
-
February 10th, 2013, 04:31 PM
#2
Re: Why can't I see the output of my C++ code?
Maybe because you don't have any statements that output anything.
-
February 10th, 2013, 04:36 PM
#3
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
-
February 10th, 2013, 05:30 PM
#4
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;
}
-
February 10th, 2013, 05:58 PM
#5
Re: Why can't I see the output of my C++ code?
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?
-
February 10th, 2013, 11:32 PM
#6
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.
-
February 11th, 2013, 04:19 PM
#7
Re: Why can't I see the output of my C++ code?
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
Last edited by Paul McKenzie; February 11th, 2013 at 04:25 PM.
-
February 11th, 2013, 04:25 PM
#8
Re: Why can't I see the output of my C++ code?
Thanks for the info, pretty scary in my opinion...
-
February 11th, 2013, 05:39 PM
#9
Re: Why can't I see the output of my C++ code?
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
-
February 12th, 2013, 12:15 AM
#10
Re: Why can't I see the output of my C++ code?
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.
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|