|
-
Dec 24th, 2002, 05:05 PM
#1
Thread Starter
yay gay
c++ driving me mad!!!!!!!!
i am trying to use the get() statement:
PHP Code:
#include "stdafx.h"
#include "iostream.h"
#include "stdio.h"
void main()
{
char str[80];
cout << "enter a string: ";
gets(str);
cout << "Here is ur string :" << str;
}
what happens here is that when i run the program it first wants me to put text and then shows up the 2 cout's......*** is goin on?
im tired of ms vc++ givin me g@y errors...
\m/  \m/
-
Dec 24th, 2002, 05:35 PM
#2
Member
Try this:
Code:
#include <iostream>
int main()
{
using namespace std;
char myString[50];
cout<<"Enter a word\n";
gets(myString);
cout<<"Here it is\n"<<myString<<"\n";
return 0;
}
Last edited by RabidChimp; Dec 24th, 2002 at 05:40 PM.
Death is always smiling down on us, the only thing we can do is smile back
-
Dec 24th, 2002, 06:22 PM
#3
Monday Morning Lunatic
Make sure to flush the output stream after inserting:
Code:
cout << "Enter a word\n" << flush;
...which can also be:
Code:
cout << "Enter a word" << endl;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 25th, 2002, 06:40 AM
#4
Nothing to do with the error, but mixing C and C++ I/O functions doesn't look good.
Instead of gets use getline
cin.getline(myString, 50);
which also ensures that there is no buffer overflow, the curse of C console I/O functions.
Or even better ::getline(cin, someString) with someString being an instance of the class std::string from <string>. But beware of a bug in MSVC++6 when you do this. There's a fix for that bug in MSDN.
Even if you want to keep the C functions use fgets and explicitly pass stdin. Unlike gets fgets allows you to pass a maximum character count.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 25th, 2002, 02:29 PM
#5
Thread Starter
yay gay
omg...then the book sucks! i am just doin what the book says...bah
\m/  \m/
-
Dec 25th, 2002, 02:54 PM
#6
Fanatic Member
yes beginner C++ tends to be subtle on I/O stuffs. I suggest you use C++ style input/output (cin/cout with >>/<<), they are nicer to work with. but the book never show you that-- they expect you to work with C strings too -- C++ strings are MUCH nicer
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|