![]() |
| | #1 |
| Programmer Join Date: Nov 2005 Location: Canada
Posts: 33
| cmd close's too soon... 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;
}
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 | |
| | #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(); |
| rodrigorules is offline | |
| | #3 |
| 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 | |
| | #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 | |
| | #5 | |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| Quote:
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 | |
| | #6 |
| the Great 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 | |
| | #7 | |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| Quote:
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 | |
| | #8 |
| the Great 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 | |
| | #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 | |
| | #10 | |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| Quote:
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 | |
| | #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 | |
| | #12 | |
| the Great Join Date: Nov 2005 Location: Republika Srpska - Balkan
Posts: 377
| Quote:
( 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 | |
| | #13 | |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| Quote:
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 | |
| | #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 | |
| | #15 |
| the Great 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |