Results 1 to 6 of 6

Thread: c++ driving me mad!!!!!!!!

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  2. #2
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    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

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    omg...then the book sucks! i am just doin what the book says...bah
    \m/\m/

  6. #6
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    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
  •  



Click Here to Expand Forum to Full Width