C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-26-2005, 01:54 PM   #1
Programmer
 
Join Date: Nov 2005
Location: Canada
Posts: 33
cmd close's too soon...

Hello! I need some help on how to stop the consol from closing. I'll put a <---- Where it cuts off.

Code:
#include <iostream>

using namespace std;

int main()
{
int a, b, c;
cout << "Hello!\nPlease Enter Any Whole Number:";
cin >> a;
cout << "Alright!  You entered ";
cout << a;
cout << "\nAlright! Now Enter Another Whole Number:";
cin >> b;
<----- (Cuts off here)
cout << "Alright!  You entered ";
cout << b;
cout << "\nNow where going to add ";
cout << a;
cout << " and ";
cout << b;
cout << "!\n";
c = a + b;
cout << c;
return 0;
}
I have no idea on how to make it wait to the end to close. Any help please? (I didn't comment the code since it's so basic.)
__________________
(Expert Visual Basic Programmer)
(Newbie C/C++ Programmer)
dimirpaw is offline   Reply With Quote
Old 11-26-2005, 02:02 PM   #2
Registered User
 
Join Date: Aug 2005
Posts: 87
add a, im still new at this to...i had this problem as well
Code:
cin.get();
so it waits for enter at the end
rodrigorules is offline   Reply With Quote
Old 11-26-2005, 02:04 PM   #3
ZuK
Registered User
 
Join Date: Aug 2005
Posts: 1,303
Your program works fine.
If you mean with "program cuts off" that the window closes before you can see the output. Call it from the commandline or search the forum.
Kurt
ZuK is offline   Reply With Quote
Old 11-26-2005, 02:31 PM   #4
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
Common problem - the second half of your program is entirely output. Once all the text has been written to the screen, the computer considers the program finished, so the console window in which it was running is finished. The simple solution is just to request some user input at the end, like rodrigorules suggested. (Running the program in a console like ZuK said would work - but it's a bit of an inconvenient solution most of the time). You can find several other solutions in the FAQ, and there are pros and cons of each - just pick one that works for you.
sean is offline   Reply With Quote
Old 11-26-2005, 02:41 PM   #5
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Quote:
Originally Posted by rodrigorules
add a, im still new at this to...i had this problem as well
Code:
cin.get();
so it waits for enter at the end
Well, hasn't cleared the stream, so he would need two cin.get(); or a cin.ignore() and cin.get().

dimirpaw, its in the FAQ.
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101
Dae is offline   Reply With Quote
Old 11-26-2005, 02:44 PM   #6
the Great
 
ElastoManiac's Avatar
 
Join Date: Nov 2005
Location: Republika Srpska - Balkan
Posts: 377
or you could just add:
Code:
while(2 == 3)
{
}
__________________
lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu
ElastoManiac is offline   Reply With Quote
Old 11-26-2005, 02:58 PM   #7
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Quote:
Originally Posted by ElastoManiac
or you could just add:
Code:
while(2 == 3)
{
}
LOL, that made my day. I've seen that one before.I can't tell if you were joking or not.

It doesn't work though because 2 doesn't equal 3. It turns into while(0) which never gets executed.
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101
Dae is offline   Reply With Quote
Old 11-26-2005, 02:59 PM   #8
the Great
 
ElastoManiac's Avatar
 
Join Date: Nov 2005
Location: Republika Srpska - Balkan
Posts: 377
i was joking...
__________________
lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu
ElastoManiac is offline   Reply With Quote
Old 11-26-2005, 03:17 PM   #9
Registered User
 
Join Date: Nov 2005
Posts: 627
all depends if you put while after the code it would generate the code at least once. but still yet you still have to have cin.get();
Raigne is offline   Reply With Quote
Old 11-26-2005, 03:26 PM   #10
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Quote:
Originally Posted by Raigne
all depends if you put while after the code it would generate the code at least once. but still yet you still have to have cin.get();
Well his code wasn't a do...while.

That was funny ElastoManiac, don't get many jokes around here. There isn't even a laugh smilie.... ....
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101
Dae is offline   Reply With Quote
Old 11-26-2005, 03:27 PM   #11
Programmer
 
Join Date: Nov 2005
Location: Canada
Posts: 33
Yep I used cin.get(); more then once it wont work because when you input a number it takes it as your pressing enter to quit, and yes I'm reading the FAQ's, however I have other things to do so I cannot read it all now... However I'll try what Dae said, thanks.

EDIT: Thanks Dae's works now!

Code:
#include <iostream>

using namespace std;

int main()
{
int a, b, c;
cout << "Hello!\nPlease Enter Any Whole Number:";
cin >> a;
cout << "Alright!  You entered ";
cout << a;
cout << "\nAlright! Now Enter Another Whole Number:";
cin >> b;
cout << "Alright!  You entered ";
cout << b;
cout << "\nNow where going to add ";
cout << a;
cout << " and ";
cout << b;
cout << "!\n";
c = a + b;
cout << c;
cin.get();
cin.get();
return 0;
}
__________________
(Expert Visual Basic Programmer)
(Newbie C/C++ Programmer)

Last edited by dimirpaw; 11-26-2005 at 03:30 PM.
dimirpaw is offline   Reply With Quote
Old 11-26-2005, 03:35 PM   #12
the Great
 
ElastoManiac's Avatar
 
Join Date: Nov 2005
Location: Republika Srpska - Balkan
Posts: 377
Quote:
Originally Posted by Dae
There isn't even a laugh smilie.... ....
( this isn't a laugh, more like a happy expresion )
Yeah, i miss smilies too, Why doesn't someone add some?
Missing expresions:
- angry
- good
- ........ed off
- crying
- ...
__________________
lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu
ElastoManiac is offline   Reply With Quote
Old 11-26-2005, 03:48 PM   #13
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Quote:
Originally Posted by dimirpaw
Yep I used cin.get(); more then once it wont work because when you input a number it takes it as your pressing enter to quit, and yes I'm reading the FAQ's, however I have other things to do so I cannot read it all now... However I'll try what Dae said, thanks.
Actually, the entire code is being processed and printed. When the program reaches the end of main() it quits. You just aren't able to see the parts after you enter the second number being printed because it prints them, and reaches the end of main() where it quits. Its hard to see whats printed before it reaches the end and knows it needs to quit. I'm able to see the things printed before the app closes, for a split second. The reason it doesn't quit and asks you for numbers is because cin halts the position in the code and waits for input, so it hasn't reached the end of main(). The same goes for cin.get(), it halts the program and it waits for one character to remove from the stream.

When you enter something in cin you push enter, the part entered is extracted and stored in the variable, but the 'enter' command is still in the stream.

So when you call cin.get() later in the program (and you've called cin), you still have that enter command in the stream, so it simply extracts that and continues with the program, so you have not paused at all - just removed that one command. So you call cin.get() again to wait for another character because theres nothing in the stream (unless you didn't extract all the rest of the characters from the stream, which isn't done often). So it pauses, and waits for the enter command. I think its '\n' in the stream, but I'm not sure.. I haven't read up on much of C++ in a few months. If I'm wrong somewhere you can be sure someone will mention it or elaborate on it.

Or cin.ignore() first will clear the entire stream, and then you can call cin.get() which will wait for a character to extract from the stream since cin.ignore() just cleared it.

That was longer that it should have been >.<, read http://www.cplusplus.com/ref/iostream/istream/get.html for more info - cplusplus.com is a good reference for the details of the library.
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101

Last edited by Dae; 11-26-2005 at 03:54 PM.
Dae is offline   Reply With Quote
Old 11-26-2005, 03:54 PM   #14
Programmer
 
Join Date: Nov 2005
Location: Canada
Posts: 33
Alright, I didn't read all of what you said since I gota go in a few minutes. I got a lot of C++ Books about 20 or so, on Game Programming, Database's, ect... I have this really big one called Visual C++ 101 Tips, not bad. (Most of the stuff in there can be related to any other compiler.)

Well I hope I can learn C++, personally I don't care to Learn C then C++, I'm already in Expert in Visual Basic with well over 6 years of experince. Not bad it wouldn't really matter for learning C++. Only the concepts about int, ect... I can carry on.
__________________
(Expert Visual Basic Programmer)
(Newbie C/C++ Programmer)
dimirpaw is offline   Reply With Quote
Old 11-26-2005, 03:55 PM   #15
the Great
 
ElastoManiac's Avatar
 
Join Date: Nov 2005
Location: Republika Srpska - Balkan
Posts: 377
This is much simplier, you just press enter at the end of program - and thats it.
Code:
#include <conio.h>

// at the end
cout<<"Press a key to continue...";
getch();
__________________
lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu
ElastoManiac is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C window code - How easy is it to intergrate cmd code spadez C Programming 23 04-20-2009 07:35 AM
OOP Question DB Access Wrapper Classes digioz C# Programming 2 09-07-2008 04:30 PM
Error running program in cmd - cygwin.dll missing - JFonseka C Programming 5 08-27-2007 12:12 PM
Program closes when startup form closes dcboy C# Programming 1 07-01-2006 02:40 AM
Little help with CMD robid1 C Programming 2 02-23-2003 04:09 AM


All times are GMT -6. The time now is 02:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22