| Pages: [1] :: one page |
| Author |
Thread Statistics | Show CCP posts - 0 post(s) |

Fordia
|
Posted - 2008.07.16 15:34:00 -
[1]
Ok so i thought id have a go at abit of basic C++ and now im stuck. What i want it to do is ask u to put a number in, say X, and then say wat number u have put in and that is half of X*2. What happens is that asks for my number, i put in a number and then press return, it then jus closes . Here's my code, any help wud be much appreciated:
#include <iostream> using namespace std;
int main () { int i; cout<<"Please enter an integer value:"; cin>>i;
cout<<"You have entered"<<i; cout<<"which is half of"<<i*2<<".\n";
return 0; }
|

defiler
Mad Hermit
|
Posted - 2008.07.16 15:45:00 -
[2]
Geez, haven't touched that stuff in ages...
But, I do seem recall something about performing a flush, to ensure that whatever's in the buffer gets printed to screen before the program terminates. So, change it tocout<<"which is half of"<<i*2<<".\n"<<flush; and try again.
Probably wrong though, my c++fu is weak.
Mad Hermit corporation Minding our own business since 2004 |

Arvald
Caldari Ninja's N Pirate's
|
Posted - 2008.07.16 15:46:00 -
[3]
great now i cant remember how to do math, i hope your happy
Originally by: Siddy
APERANTLY GEED PEE VEE PEE PLAYAS!!111
im in your forums, derailing your threads |

Irish Whiskey
Caldari Corp 1 Allstars The Requiem
|
Posted - 2008.07.16 15:48:00 -
[4]
umm
isnt half of X*2...
X?
writing senseless code like that you must be applying for a job at microsoft
|

Nautikus
principle of motion
|
Posted - 2008.07.16 15:50:00 -
[5]
Edited by: Nautikus on 16/07/2008 15:50:59 Try this line right above return 0; system("PAUSE");
A lot of compilers will complete the code and then shut right down. It completes the code, throws the results up and then closes because it thinks it's done. That line will force the program to pause itself at the end so you can view your handywork. I hope it works for :)
|

Lord MuffloN
Caldari Aggressive Tendencies Veritas Immortalis
|
Posted - 2008.07.16 15:53:00 -
[6]
Edited by: Lord MuffloN on 16/07/2008 15:57:03 Your solution is cin.get (and cin.ignore() )
Try std::cin.ignore(); before return0;
Reason it closes down is what previous poster said
Originally by: Nautikus Edited by: Nautikus on 16/07/2008 15:50:59 Try this line right above return 0; system("PAUSE");
A lot of compilers will complete the code and then shut right down. It completes the code, throws the results up and then closes because it thinks it's done. That line will force the program to pause itself at the end so you can view your handywork. I hope it works for :)
Originally by: Jago Kain If they ever decide to award a Nobel Prize for Emo, Lord MuffloN is a sure fire winner of the first on
|

Isiskhan
Gnostic Misanthropy
|
Posted - 2008.07.16 15:55:00 -
[7]
That should work fine, and I actually just compiled it myself and had no problems:
foobar.cpp:
#include <iostream> using namespace std;
int main (int argc, char *argv[]) { int i; cout << "Please enter an integer value: "; cin >> i; cout << "You have entered " << i << " which is half of " << i*2 << endl; return 0; }
Misanthropy:test pathos$ ./foobar Please enter an integer value: 42 You have entered 42 which is half of 84
Perhaps you are launching the program on Windows by double clicking on it and the console closes as soon as the program is finished? Try opening the console and then executing the program from the command line instead.
|

Cal5
Federal Defence Union
|
Posted - 2008.07.16 16:07:00 -
[8]
Edited by: Cal5 on 16/07/2008 16:07:39 Not a great expert here, but I think the previous poster has it spot on. Alternatively you could put the line "while (1)" which would stop the program closing I beleive. My preffered method would be to declare another int, say 'k'. Have the line "cin >> k;". That would also stop programing closing.
Oh and also another thought. What compiler are you using? You could just step through the code line for line and see what happens.
Hope this helps. Good Luck!
|

Yaar Podshipnik
Gallente Paxton Industries Paxton Federation
|
Posted - 2008.07.16 16:26:00 -
[9]
Having while(1) would also consume a lot of cpu time ;).
Forida, when asking for help always report your compiler version/IDE you are using.
Also, never ever name variables with single letters. In non-trivial code it is extremely painful to grep for them, and ii instead of i is just one more keystroke.
|

Fordia
|
Posted - 2008.07.16 16:36:00 -
[10]
Thanks for the replies guys :D
system ("PAUSE"); worked great thanks.
Btw im using Visual C++ 2008 Express Edition, if that helps.
|

defiler
Mad Hermit
|
Posted - 2008.07.16 16:43:00 -
[11]
Edited by: defiler on 16/07/2008 16:44:43 Um, ok... My answer was based on the assumption that the OP ran his program from a console window, now that I know that is not the case the problem is much clearer. 
I humbly suggest you do that from now on (or use whatever console window VS has if you can run it that way), better than using forever loops or pausing execution imo. That way you can see it as God (Stroustrup) intended 
edit: Spell checking is not a substitute for proofreading.
Mad Hermit corporation Minding our own business since 2004 |

The TX
Gallente Earth Inc.
|
Posted - 2008.07.16 16:56:00 -
[12]
My advice with C++, run away and become a hairdresser instead.
-------------------- [Signature]
[/Signature]
|

Xen Gin
Universal Mining Inc Forged Dominion
|
Posted - 2008.07.17 01:22:00 -
[13]
Originally by: Yaar Podshipnik Having while(1) would also consume a lot of cpu time ;).
Forida, when asking for help always report your compiler version/IDE you are using.
Also, never ever name variables with single letters. In non-trivial code it is extremely painful to grep for them, and ii instead of i is just one more keystroke.
is there an alternative to using while(1) to keep code looping?
|

Isiskhan
Gnostic Misanthropy
|
Posted - 2008.07.17 01:40:00 -
[14]
Originally by: Xen Gin is there an alternative to using while(1) to keep code looping?
You can use a for or a do/while loop but that's just syntactic sugar: you'll still be hogging the CPU.
The way to prevent his, while still mantaining some sort of infinite loop running, is throwing a sleep() call in there (C standard function) to suspend execution for whatever number of seconds, and then check for some condition to decide if you exit the loop or re-enter and hibernate for another while (you can also break a sleep() asynchronously with a signal).
But in the OPs particular case, instead of adding these sort of artifacts, the cleanest and easiest way is to simply open first the console and launch the program from its command line. That way, the console will remain open once the program exists and you'll be able to see the output.
|
| |
|
| Pages: [1] :: one page |
| First page | Previous page | Next page | Last page |