# include <iostream>
usingnamespace std;
int main ()
{
int c;
do
{
int a,b,c ;
cout <<" enter ur first no. " << endl;
cin >> a;
cout <<" enter ur second no. " << endl ;
cin >> b ;
cout <<" ur ans is " << (a+b) <<endl;
}
while (0==0);
scanf ("%d%",&c);
return 0;
}
take this simple program in consideration..how can i give colors to fonts and background ?
Why did you create a new post? You already posted the same problem. In your previous post I showed you how to do what you're asking about continuing the program. I'm browni3141. Check my first reply under your old post.
What you have above is pretty close. All you need to do is declare a variable to use for user input. Then get input from the user, such as 'y' or 'n'. Finally fix your while statement to say [code]while ((input == 'N') || (input == 'n'))[/cod]
|| is a relational operator that means "or".
EDIT: You can change the colors by right clicking the top of the window, selecting properties and going to colors.
what i mean by giving colors is how to give them while programming..means when i code this program this will appear in black background with white text..so how can i give different color to my program
#include <iostream.h>
#include <conio.h>
// Needed for the SetConsoleTextAttribute () Function
#include <Windows.h>
int main ()
{
usingnamespace std;
// Set the default text
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 7);
// Get input
int iInput;
cout << "Enter a number and I will give you a color: ";
cin >> iInput;
// Clear the screen
system ("cls");
// Set the color
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), iInput);
// Print text
cout << "Color Number: " << iInput << endl << endl;
cout << "Code To Get This Color:" << endl;
cout << "SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), " << iInput << ");";
getch ();
// Clear the screen and re-run the program
system ("cls");
return main ();
}
@im abcd: don't get to hung up about the looks of your console programs. Learning console tricks is too specific and won't help you when you make the move to GUI.
lol I doubt I piece of code is "evil", and return main () makes since to me, when you call a function in a return statement it is going to call the function when it returns, it works for me....
If you get into high enough numbers then it starts to color around the text, and the background, try some things out. Also filipe is right, don't get into colors with console applications, waste of time. In my opinion console applications are only to be used to learn basics of C/C++, then once you get into Win32 programming you can do a lot of things with actual windows, and not just things in the MSDOS window.
Have you read anything besides the title? It's bad practice.
and return main () makes since to me, when you call a function in a return statement it is going to call the function when it returns, it works for me....
That program, as it is, will run forever and recursively call main() until the stack overflows (if it compiles and runs at all, that is; calling main() is probably not allowed by the standard). When you have a statement such as
return foo();
function foo() will be executed and, when it returns, its return value will be used by the statement.
Actually, return main(); is forbidden by C++: You are not allowed to take the address of main() or call it as a function()
either directly or indirectly (via pointer).
When I use it it returned back to the main function ....
The return statement is not analogous to a goto. Your program is not sending control back to the first line of main(); it's actually running a new instance of main() every time it executes that statement. The program will never terminate on its own, and if you keep going for long enough, the stack will overflow. Are you familiar with recursion? Basically, your main() function never ever returns, it just keep recursively calling itself.
It works for me on Dev-C++, Code::Blocks, and VC++ 2010....
That doesn't make it any more correct. And don't use Dev-C++, it hasn't been updated in 5 years.