| View previous topic :: View next topic |
| Author |
Message |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Wed Sep 17, 2008 7:56 pm Post subject: My C++ Question Thread |
|
|
Well, I started to learn C++ today.
Obviously i'm starting with consoles for many reasons, and than moving on to Win32 API.
I will post all my questions here, so I dont end up making like 50 threads...
1st Question: Why does my program open than close? - SOLVED
My source:
| Code: | //my first program in C++, Sep. 17, 2008
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
} |
2nd Question: When I use 'int' instead of 'double' my answer is different. -SOLVED
| Code: | // Defined Constants: Calculating Circumference. Sep 18, 2008.
#include <iostream>
using namespace std;
// Define our constants:
#define PI 3.14159
#define NEWLINE '\n'
int main()
{
double r=5.0; //radius
double circle;
circle = 2*PI *r;
cout << circle;
cout << NEWLINE;
cin.sync();
cin.ignore();
return 0;
} |
_________________
Last edited by AndrewMan on Thu Sep 18, 2008 6:24 pm; edited 6 times in total |
|
| Back to top |
|
 |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
Posted: Wed Sep 17, 2008 8:01 pm Post subject: |
|
|
That's what a console application does.
Start -> Run...
Type in cmd
Navigate to your directory using "cd <dir>", for example:
cd "C:\Documents\C++ Projects\Hello World\bin"
Type in the name of the executable and press enter. The output should appear
If you want to get round this, cin a single char at the end of your main method. The application will halt at the end, waiting for user input. When the user presses any key, the application will close.
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Sep 17, 2008 8:04 pm Post subject: |
|
|
It's because you are instantly returning (in this case closing your program) right after outputting your text. To pause your program put these two lines of code before return 0.
| Code: |
cin.sync();
cin.ignore();
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Wed Sep 17, 2008 9:07 pm Post subject: |
|
|
Thanks guys, more question to come tomorrow as I start working with user input.
_________________
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Wed Sep 17, 2008 9:50 pm Post subject: |
|
|
| Use getchar(); or system("pause");
|
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Wed Sep 17, 2008 9:59 pm Post subject: |
|
|
| _void_ wrote: | | Use getchar(); or system("pause"); | No, bad.
_________________
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Wed Sep 17, 2008 10:24 pm Post subject: |
|
|
| _void_ wrote: | | Use getchar(); or system("pause"); |
rofl, probably some of the worst advice i've seen in a long time on CEF.
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Sep 18, 2008 2:32 am Post subject: |
|
|
| Overload wrote: | | _void_ wrote: | | Use getchar(); or system("pause"); |
rofl, probably some of the worst advice i've seen in a long time on CEF. |
It's void, what'd you expect? ^_^.
Heres some stuff so you dont even need to ask for it:
| Code: | void _inline pause(void) {
std::cin.sync(); std::cin.ignore();
} |
| Code: | void _inline mincls(void) {
COORD start = {0, 0};
std::cout.fill(' ');
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), start );
} |
| Code: | void fullcls(void) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0}; DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
} |
Function names pretty much tell you everything.
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Thu Sep 18, 2008 6:04 pm Post subject: |
|
|
Thanks sooo much for the help guys, you all so damn helpful.
Edit: New question added.
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Thu Sep 18, 2008 6:09 pm Post subject: |
|
|
int does not support decimals. you can use it, but it wouldn't be as accurate as float or double.
_________________
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Thu Sep 18, 2008 6:15 pm Post subject: |
|
|
| HalfPrime wrote: | | int does not support decimals. you can use it, but it wouldn't be as accurate as float or double. |
Ahh I see, no wonder why my answer had no decimals.
Thanks HalfPrime
_________________
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Thu Sep 18, 2008 6:52 pm Post subject: |
|
|
| It get's the job done and that's all I want.
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Thu Sep 18, 2008 10:08 pm Post subject: |
|
|
| _void_ wrote: | | It get's the job done and that's all I want. |
Gets it done very horribly.
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Fri Sep 26, 2008 11:43 am Post subject: |
|
|
add this question and answer PLEASE .......how do u set only specific parts of the wording coloured like this?
cout << "Hello World" << endl;
but i only want "World" in red and keep everything else the same
i know its been ansered many times before but i cant find it and its a good addition to this thread
Thanks,
Travis13
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Sep 26, 2008 2:25 pm Post subject: |
|
|
Take a look at:
GetStdHandle
SetConsoleTextAttribute
|
|
| Back to top |
|
 |
|