Thread: program for real roots of quadratic equation

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mozza314 View Post
    What problem is windows specific? I don't see how finding the roots of a quadratic equation is windows specific.
    S/he probably believes - incorrectly - that the problem of a host system (i.e. operating system or windowing environment) closing the window displaying program output is Microsoft windows specific.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Quote Originally Posted by grumpy View Post
    S/he probably believes - incorrectly - that the problem of a host system (i.e. operating system or windowing environment) closing the window displaying program output is Microsoft windows specific.
    Really? I've never seen that happen on a Linux system. Does OSX have this behavior?

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi again

    As you guys suggested that I replace the last line system("PAUSE"); with cin.get();, I did that. But it didn't work. The console window closed down before I could see anything.

    I'm using these header files (I think they are called this?)
    #include <iostream>
    # include <cmath>
    #include <stdlib.h>


    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Clairvoyant1332 View Post
    Really? I've never seen that happen on a Linux system. Does OSX have this behavior?
    It's not actually a feature of the operating system at all.

    The behaviour is an artefact of how some IDEs manage output from a program, how they link lifetime of a program being run to a separate window that displays output, etc.

    It is often associated with windows because commonly used IDE's under windows do things that way by default when creating/executing/debugging console mode applications.

    I've never used OSX, but have seen the phenomenon when running console mode programs for debugging within IDEs under linux (and its windowing environments) and (going back a bit) Solaris.

    Quote Originally Posted by jackson6612 View Post
    Hi again
    As you guys suggested that I replace the last line system("PAUSE"); with cin.get();, I did that. But it didn't work. The console window closed down before I could see anything.
    Look at my post again. I did not suggest a one-line replacement.

    If there is any pending output, cin.get() will return immediately. Since you are using "cin >> ... " operations, there is probably a carriage-return in the input pending when you call cin.get(). You need to find a way of discarding that input (eg a call to cin.ignore(), maybe followed by a call to cin.clear()) if you want your program to wait for input.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by grumpy View Post
    Look at my post again. I did not suggest a one-line replacement.

    If there is any pending output, cin.get() will return immediately. Since you are using "cin >> ... " operations, there is probably a carriage-return in the input pending when you call cin.get(). You need to find a way of discarding that input (eg a call to cin.ignore(), maybe followed by a call to cin.clear()) if you want your program to wait for input.
    Hi grumpy

    Thank you for the reply. For some time I would stick with system("PAUSE"), even though as Mozza says it's non-standard.

    I wanted your opinion on this. In the code below I have used blank lines to make the code more easily readable and understandable. e.g. Once I have input the value of "a", then blank line follows, then comes the part of "b". Is this right practice? I don't think compiler has any problem with it.

    Regards
    Jackson


    Code:
    #include <iostream>
    # include <cmath>
    #include <stdlib.h>
    
    using namespace std;
    int main ()
    
    {
        float a, b, c, R1, R2; //variables and constants used
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a;
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
        float D = b*b - 4*a*c;
        
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        }
        
        else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE");
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jackson6612 View Post
    I wanted your opinion on this. In the code below I have used blank lines to make the code more easily readable and understandable. e.g. Once I have input the value of "a", then blank line follows, then comes the part of "b". Is this right practice? I don't think compiler has any problem with it.
    The compiler certainly has no problem with it.

    There is no universal right or wrong approach in such things, but I tend to advocate keeping code as simple as possible, and using layout (additional lines, indentation) to ensure people can understand the code.

    Code that is simple and well laid out is easier to understand, therefore it is easier to get right. Such things also make it easier for people - other than the original programmer - to examine the code, decide if it is fit for their purpose. If it is necessary to make changes, then code that is simple and well laid out is easier to understand, and therefore easier to change.

    Your code layout does meet the aims of being uncomplicated, and understandable. Your approach is not the only way, but it is one way, and it is sensible for the code you have shown.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by grumpy View Post
    The compiler certainly has no problem with it.

    There is no universal right or wrong approach in such things, but I tend to advocate keeping code as simple as possible, and using layout (additional lines, indentation) to ensure people can understand the code.

    Code that is simple and well laid out is easier to understand, therefore it is easier to get right. Such things also make it easier for people - other than the original programmer - to examine the code, decide if it is fit for their purpose. If it is necessary to make changes, then code that is simple and well laid out is easier to understand, and therefore easier to change.

    Your code layout does meet the aims of being uncomplicated, and understandable. Your approach is not the only way, but it is one way, and it is sensible for the code you have shown.
    Thank you for your informed opinion.

    Could you please help with text formatting? I want to show the following lines in some color and bold.

    Code:
    cout << "R1 is="  ( -b + sqrt(D) ) / ( 2*a );
    cout<< "R2 = ( -b - sqrt(D) ) / ( 2*a );
    I found this link:
    colors in console for beginners - C++ Forum

    The second post on that link is very simple (by "Mythios") But the problem is it changes the background color of the console and text. e.g. If I had included the line
    Code:
    system("Color 1A");
    after the line where I float the variables at the top, then the background color would be blue and text color would green. That's good. But I want the application only on specific lines. Could you help me on this?

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Outputting text with different attributes (eg colour, highlighting) to a console is outside the scope of standard C++.

    The general logic, however, is to set attributes you want, output the text you wish to have those attributes. Once you are done with one lot, select a new set of attributes and output text that needs those attributes. Keep doing that until all your text is output with the desired attributes. At the end of the program, preferably, reset the attributes to the original settings (those in force when the program started) - this helps keeps your users sane if they run your program from an existing console window.

    For example;
    Code:
       CONSOLE_SCREEN_BUFFER_INFO save_buffer;
       GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &save_buffer);
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "Text you want with attribute 0x5B\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x1A);
       std::cout << "Text you want with attribute 0x1A\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "More text you want with attribute 0x5B\n";
    
       SetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), save_buffer.wAttributes);
       std::cout << "Text with the original (starting) attributes\n";
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #24
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    Code:
    #include <iostream>
    # include <cmath>
    #include <stdlib.h>
    
    using namespace std;
    int main ()
    
    {
        float a, b, c, R1, R2; //variables and constants used
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a;
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
        float D = b*b - 4*a*c;
        
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        }
        
        else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE");
    }
    That is soooooooooooo much better :-)

    Like Grumpy said, there is no universal way to add lines and such, but whatever way you do it, it makes your code much easier to read.

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by grumpy View Post
    Outputting text with different attributes (eg colour, highlighting) to a console is outside the scope of standard C++.

    The general logic, however, is to set attributes you want, output the text you wish to have those attributes. Once you are done with one lot, select a new set of attributes and output text that needs those attributes. Keep doing that until all your text is output with the desired attributes. At the end of the program, preferably, reset the attributes to the original settings (those in force when the program started) - this helps keeps your users sane if they run your program from an existing console window.

    For example;
    Code:
       CONSOLE_SCREEN_BUFFER_INFO save_buffer;
       GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &save_buffer);
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "Text you want with attribute 0x5B\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x1A);
       std::cout << "Text you want with attribute 0x1A\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "More text you want with attribute 0x5B\n";
    
       SetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), save_buffer.wAttributes);
       std::cout << "Text with the original (starting) attributes\n";
    Hi Grumpy

    Thanks a lot. I include #include <windows.h> header file. But to make it work I had to remove the bold lines. Otherwise this error was encountered:
    `SetConsoleScreenBufferInfo' undeclared (first use this function)

    Working code is:

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    
    {
    
       CONSOLE_SCREEN_BUFFER_INFO save_buffer;
       GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &save_buffer);
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "Text you want with attribute 0x5B\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x1A);
       std::cout << "Text you want with attribute 0x1A\n";
    
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x5B);
       std::cout << "More text you want with attribute 0x5B\n";
    
       system("PAUSE");
       
    }
    By the way I tried to use some lines of your code to change the output in some program. It didn't work. Here is the code.

    Code:
    #include <iostream>
    # include <cmath>
    #include <cstdlib>
    #incude <windows.h>
    
    using namespace std;
    
    int main ()
    
    {
        CONSOLE_SCREEN_BUFFER_INFO save_buffer;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &save_buffer);
        
        float a, b, c, R1, R2; //variables and constants used
        
        system("Color 1A");
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a;
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
        float D = b*b - 4*a*c;
        if (D >= 0)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x1A);
    		cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        }
        
        else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE");
    }
    I genuinely appreciate your effort to help and am thankful for this, and especially your time. Now please help me with this.


    Quote Originally Posted by Mozza314 View Post
    That is soooooooooooo much better :-)

    Like Grumpy said, there is no universal way to add lines and such, but whatever way you do it, it makes your code much easier to read.
    Hi Mozza

    That's really encouraging. You have really helped me and without your and others like Grumpy it would have very difficult. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Replies: 4
    Last Post: 08-18-2009, 03:32 PM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM