Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 30th, 2006, 3:51 PM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Did you get any error messages? If not, open a command window and run your program from there, see what happens. Be clear and thorough in your posts.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 31st, 2006, 2:37 PM   #12
Zephirus
Newbie
 
Zephirus's Avatar
 
Join Date: May 2006
Posts: 12
Rep Power: 0 Zephirus is on a distinguished road
#include <iostream>

using namespace std;

int main()

{
cout << "HEY, you, I'm alive! Oh, and Hello World!" << endl;
cin.get();

return 0;
}


I changed the \n I know its good C++ practice to use it but I can type endl a lot faster than \n so thats what I walways use. Sometimes you have to put 2 "cin.get();" in there. I know I have to on most of mine to test.

Change the return 1 to return 0 though. With this function, you dont need to return anything at all.


I dont see why this should fail.


OH I just thought of something. in my C++ IDE i have to have to have another include something like staffix.h or something.. Not sure what it is for since im just starting also.
Zephirus is offline   Reply With Quote
Old May 31st, 2006, 3:01 PM   #13
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>Try clearing the stream by using 'cin.sync();' before 'cin.get();'.
Um, no. cin.sync() isn't guaranteed to do anything meaningful, much less "clear" the stream. The only portable way to read and discard leftover characters is to read them either with a loop, or with cin.ignore():
// Loop
char ch;
while ( cin.get ( ch ) && ch != '\n' )
  ;

// Ignore
#include <ios>
#include <limits>
cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old May 31st, 2006, 3:26 PM   #14
g2k556
Newbie
 
Join Date: May 2006
Posts: 7
Rep Power: 0 g2k556 is on a distinguished road
Okay, I tried what you said DaWai because I didn't get any error messages. I went to the command prompt, said start c:\learingc1.exe. I hit enter, then it went to asking for another command. It must have executed the file, but it appears that there is nothing happening. Is there anyway to cause it to pause.

and narue, i don't quite understand what you mean, you have to remember, I'm only in my first lesson of C++.
g2k556 is offline   Reply With Quote
Old May 31st, 2006, 3:51 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Originally Posted by my basic istream sync doc, no guarantees as to validity
Synchronizes the input device associated with the stream with the stream's buffer.
int sync( );

Return Value
If rdbuf is a null pointer, the function returns -1. Otherwise, it calls rdbuf ->pubsync. If that returns -1, the function calls setstate(badbit) and returns -1. Otherwise, the function returns zero.
Note that the extraction function will return if it finds an item eligible to be returned or one that causes an error. "I have to call it twice (or ten times, or whatever)" is not a valid solution to offer. There are reasons for these things. Voodoo is okay, but hard on one's chickens.

If you ran your program, as it is shown, in a commandd prompt window, and saw no output, then you probably have something that is not as shown. As far as endl is concerned, it outputs a newline and flushes the output buffer. There are conditions where the flush is a good idea, but they aren't often seen in these little "Goodbye, cruel world" programs. Still, doesn't mean you haven't something else running that's farbling with things.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 1st, 2006, 12:07 PM   #16
JamesCEdmonds
Newbie
 
Join Date: May 2006
Posts: 19
Rep Power: 0 JamesCEdmonds is on a distinguished road
// My first program in C++.
#include <iostream>
using namespace std;
int main ()
{
cout<<"Any text!";
return 0;
}



I have no problems like this.I also ran it the way you have posted with no problem.Try hitting the compile and run button. (f9)

Last edited by JamesCEdmonds; Jun 1st, 2006 at 12:21 PM.
JamesCEdmonds is offline   Reply With Quote
Old Jun 1st, 2006, 1:10 PM   #17
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Narue
>Try clearing the stream by using 'cin.sync();' before 'cin.get();'.
Um, no. cin.sync() isn't guaranteed to do anything meaningful, much less "clear" the stream. The only portable way to read and discard leftover characters is to read them either with a loop, or with cin.ignore():
Quote:
Originally Posted by DaWei
Note that the extraction function will return if it finds an item eligible to be returned or one that causes an error. "I have to call it twice (or ten times, or whatever)" is not a valid solution to offer. There are reasons for these things. Voodoo is okay, but hard on one's chickens.
I see. This harks back to one of my first threads on this forum when I was just starting to learn C++. I was first told to "try flushing it" (the stream). Then told to use cin.sync(), and it "worked". Guess I made the assumption that cin.sync() was clearing the stream and never thought any more of it.

Just for reference:
http://www.programmingforums.org/for...ead.php?t=6640

Won't be making that mistake again.
Cache is offline   Reply With Quote
Old Jun 1st, 2006, 2:06 PM   #18
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>Then told to use cin.sync(), and it "worked".
This is one of the grey areas. The C++ standard in no way implies that cin.sync() will do what most people expect when they want to "flush" the stream. Unfortunately, many compilers implement it so that it does, and even Stroustrup explicitly (and incorrectly) states that it flushes the stream. The best solution is to avoid it entirely in favor of other solutions that are guaranteed to work as expected.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Jun 4th, 2006, 5:16 PM   #19
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The C++ FAQ recommends std::cin.clear followed by std::cin.ignore.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC