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.
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.
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.
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.
I found this link:Code:cout << "R1 is=" ( -b + sqrt(D) ) / ( 2*a ); cout<< "R2 = ( -b - sqrt(D) ) / ( 2*a );
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 lineafter 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?Code:system("Color 1A");
Regards
Jackson
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.
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:
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 <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"); }
I genuinely appreciate your effort to help and am thankful for this, and especially your time. Now please help me with this.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"); }
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.