| View previous topic :: View next topic |
| Author |
Message |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 8:05 pm Post subject: Program exiting automatically |
|
|
What command would I use to make a program not automatically exit?
I think I have a clue something like
but I tried that, but it didn't work. _________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Oct 24, 2008 8:12 pm Post subject: |
|
|
| run it from the command line. |
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Fri Oct 24, 2008 8:18 pm Post subject: |
|
|
| Code: | while (exit == false)
{
//Do something
if input = "exit"
exit = true;
}
|
Pseudo code for a loop.
A better way is to get input using a switch. |
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 8:28 pm Post subject: |
|
|
Okay, I don't really know what I'm doing wrong, but can you please tell me?
| Code: | // my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
while {exit == false}
{
cout << "You can now exit. ";
if input = "exit"
exit = true
} |
http://pastebin.com/d2afed972
These are the 4 errors I'm getting:
d:\documents and settings\owner\my documents\visual studio 2008\projects\hello world!\hello world!\hello world.cpp(14) : error C2059: syntax error : 'while'
d:\documents and settings\owner\my documents\visual studio 2008\projects\hello world!\hello world!\hello world.cpp(14) : error C2143: syntax error : missing ';' before '{'
d:\documents and settings\owner\my documents\visual studio 2008\projects\hello world!\hello world!\hello world.cpp(14) : error C2447: '{' : missing function header (old-style formal list?)
d:\documents and settings\owner\my documents\visual studio 2008\projects\hello world!\hello world!\hello world.cpp(15) : error C2447: '{' : missing function header (old-style formal list?) _________________
Last edited by xV on Fri Oct 24, 2008 8:30 pm; edited 2 times in total |
|
| Back to top |
|
 |
dEEznutz Grandmaster Cheater
Reputation: 0
Joined: 29 Nov 2007 Posts: 514
|
Posted: Fri Oct 24, 2008 8:29 pm Post subject: |
|
|
cin.get()
? _________________
Learning C++ |
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 8:31 pm Post subject: |
|
|
| dEEznutz wrote: | cin.get()
? |
Sorry, I just started learning. Can you tell me what that does and how I'll be able to use it? I'm just making a very simple program that just says a line of text. If it helps, my project is Win32, someone told me I should use that. _________________
|
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Fri Oct 24, 2008 8:34 pm Post subject: |
|
|
Your error there is that the while is never reached.
Place it in the main method, and then it'll actually be reached.
As the other guy said
| Code: | cin.get();
cin.sync(); |
will stop the program, but you can't do anything after that. The switch method is a good way to do things. |
|
| Back to top |
|
 |
clanner Master Cheater
Reputation: 0
Joined: 26 Jul 2006 Posts: 290
|
Posted: Fri Oct 24, 2008 8:35 pm Post subject: |
|
|
Well, one of the really cheap ways is to add
at the end of your code. |
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Fri Oct 24, 2008 8:37 pm Post subject: |
|
|
| clanner wrote: | Well, one of the really cheap ways is to add
at the end of your code. |
Thats also a pretty bad way... |
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 8:39 pm Post subject: |
|
|
Clanner, I tried to put your code into this simple code and I get 2 errors.
http://pastebin.com/d6d09cc99
Errors:
| Quote: | | MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup |
| Quote: | | D:\Documents and Settings\owner\My Documents\Visual Studio 2008\Projects\My First Project!\Debug\My First Project!.exe : fatal error LNK1120: 1 unresolved externals |
_________________
|
|
| Back to top |
|
 |
dEEznutz Grandmaster Cheater
Reputation: 0
Joined: 29 Nov 2007 Posts: 514
|
Posted: Fri Oct 24, 2008 8:40 pm Post subject: |
|
|
| Frostbyt3 wrote: | | clanner wrote: | Well, one of the really cheap ways is to add
at the end of your code. |
Thats also a pretty bad way... |
Seriously, do not use that.
Try putting it before return 0; _________________
Learning C++ |
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 8:47 pm Post subject: |
|
|
I went back to what Frostbyt3 said and used this code:
| Code: | // my second program in C++
#include <iostream>
using namespace std;
int main ()
{
while (exit == false)
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
if input = "exit"
exit = true;
}
} |
And returned with only one error, not a really big error I think.
| Quote: | | D:\documents and settings\owner\my documents\visual studio 2008\projects\my first project!\my first project!\hello.cpp(14) : error C2061: syntax error : identifier 'input' |
Do you know what's wrong? _________________
|
|
| Back to top |
|
 |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Fri Oct 24, 2008 8:47 pm Post subject: |
|
|
you stuffed up lots of this
| Code: | while (exit == false)
{
cout << "You can now exit. ";
if input = "exit"
exit = true;
} |
wrong brackets and no ; its fixed
also, go and look at some basic C++ tutorials because they are far easier than asking us everything _________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Oct 24, 2008 8:49 pm Post subject: |
|
|
cin.sync
cin.ignore / get.
| Code: | #include <iostream>
int main (){
std::cout << "Hello World!";
std::cout << "I'm a C++ program";
std::cin.sync();
std::cin.ignore();
return 0;
} |
Last edited by hcavolsdsadgadsg on Fri Oct 24, 2008 8:51 pm; edited 1 time in total |
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Fri Oct 24, 2008 9:15 pm Post subject: |
|
|
| slovach wrote: | cin.sync
cin.ignore / get.
| Code: | #include <iostream>
int main (){
std::cout << "Hello World!";
std::cout << "I'm a C++ program";
std::cin.sync();
std::cin.ignore();
return 0;
} |
|
:O What's all that std stuff?
Could I get rid of all that std:: if I used: | Code: | | using namespace std; |
_________________
|
|
| Back to top |
|
 |
|