Why can't I see the output of my C++ code?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Why can't I see the output of my C++ code?

  1. #1
    Join Date
    Feb 2013
    Posts
    36

    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;

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: Why can't I see the output of my C++ code?

    Maybe because you don't have any statements that output anything.

  3. #3
    Join Date
    Feb 2013
    Posts
    36

    Re: Why can't I see the output of my C++ code?

    right, unfortunately, even when I type:

    Code:
     cout<<series(c,n);
    instead of just

    Code:
    series(c,n);
    I still cannot see the output

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: Why can't I see the output of my C++ code?

    Code:
    cin>> c >> n;
    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;        
    }

  5. #5
    Join Date
    Feb 2013
    Posts
    36

    Re: Why can't I see the output of my C++ code?

    Quote Originally Posted by 2kaud View Post
    Code:
    cin>> c >> n;
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Why can't I see the output of my C++ code?

    Quote Originally Posted by math8 View Post
    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.

  8. #8
    Join Date
    Feb 2013
    Posts
    36

    Re: Why can't I see the output of my C++ code?

    Thanks for the info, pretty scary in my opinion...

  9. #9
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Why can't I see the output of my C++ code?

    Quote Originally Posted by math8 View Post
    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

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Why can't I see the output of my C++ code?

    Quote Originally Posted by Paul McKenzie View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center