gets("rope") ... help pls

This is a discussion on gets("rope") ... help pls within the C++ Programming forums, part of the General Programming Boards category; Been working on this all day... I want to give the user 2 options. He or she can either enter ...

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    gets("rope") ... help pls

    Been working on this all day...

    I want to give the user 2 options. He or she can either enter a number between 2 - 100, or type "exit." This has to happen on the same command line because I want the user to have the option of exiting at any time. However, If I scan in an integer, I can not compare the strings, and if I scan in a string I cannot properly compare the integers.

    I have tried itoa(), but I converts the string to that long, frustrating negative number, so I can't compare that way.
    Last edited by Invincible; 02-12-2002 at 01:16 AM.
    "The mind, like a parachute, only functions when open."

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    scanf("%s",string);
    if(strcmp("exit", string) == 0)
    {
       exit(1);
    }
    else
    {
        int sides = atoi(string);
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Thumbs up

    Thanks,

    That did the trick.

    Here's the end product if you're interested in seeing what you helped with.
    Attached Files Attached Files
    "The mind, like a parachute, only functions when open."

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    When writing C++ code, use cin.getdata instead of gets. And use cout instead of printf. Or perhaps there r some advantages in using C function?

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    I'm confused.

    I though printf belonged to the C++ family and cout was old school.

    Can anyone clarify this.

    Some say that the printf function is faster, and from what I've seen it's more versatile and easier to format.

    Maybe cout is just too confusing for me and it's easier to use a function.

    "The mind, like a parachute, only functions when open."

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    I found it's much easier to use C++ specific functions, classes,etc.
    Especially that it is nor secure to mix C with C++, for example:

    #include <iostream.h>
    int main()
    {
    // char *myString;
    // myString=new char[20];
    // cout <<"Name:";
    // cin.getline(myString,20);
    // cout <<"\nYour name is"<<myString;
    // return 0;
    //}

    works perfect, but try running this:

    #include <iostream.h>
    #include <stdio.h>
    int main()
    {
    char *myString;
    myString=new char[20];
    cout<<"Name:";
    gets(myString);//This runs first
    cout <<"\nYour name is"<<myString;
    return 0;
    }

    stdio.h and iostream.h are not complatible, or maybe I am wrong?

    Nima

  7. #7
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Strange ... the second bit of code runs fine if you use a namespace.

    Too bad I don't really have a clue about what a namespace is. Except that it allows the use ambiguous class names.

    #include <iostream>
    //#include <cstdio>

    using namespace std;

    int main()
    {
    char myString;

    myString=new char[20];

    cout<<"Name:";
    //gets(myString);//This runs first
    cout <<"\nYour name is"<<myString;
    return 0;
    }
    "The mind, like a parachute, only functions when open."

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I though printf belonged to the C++ family and cout was old school.

    Other way round. printf() belongs to the C I/O family, it is, however, still supported by C++, (along with all the other functions). cout(), (et al), are C++ specific.

    >>> from what I've seen it's more versatile and easier to format.

    There are many that would agree, however, the ability to overload the insertion and extraction operators in C++ stream I/O is much more flexible when trying to save and read objects.

    >>> (don't) mix C with C++,

    Good advice generally. C and C++ I/O systems buffer differently, (in some cases not at all), and in different places. Using both on the same channel can lead to hard to find bugs.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Since you brought it up...

    How does one go about opening more than one channel?

    For instance,

    if I cin >> string;

    then I

    cin >> newVariable

    then newVariable = string

    but let's say I want to 2 seperate inputs from the user before I

    cout<< string

    cout<< newVariable

    I know it's a newbie question, but it continues to be a probelm
    Last edited by Invincible; 02-12-2002 at 03:01 AM.
    "The mind, like a parachute, only functions when open."

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Code:
    FILE *stream1,*stream2;
    stream1 = fopen( "data1", "r" )
    stream2 = fopen( "data2", "r" )
    ... for example.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    now you're really confusing me

    isn't fopen a stdio function?

    and stdio is a member of C, because that's also the header for the printf() function right?
    "The mind, like a parachute, only functions when open."

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    >>C and C++ I/O systems buffer differently, (in some cases not at all)

    True. As mentiond in my previous post:

    char *myString;
    myString=new char[20];
    cout<<"Name:";
    gets(myString);//This runs first
    cout <<"\nYour name is"<<myString;

    wont work properly while, this one works fine:

    int i;
    printf("Age:");
    cin>>i;
    printf("%d",i);


    but,why?

    Nima

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    invincible:

    >>> isn't fopen a stdio function?

    Yes.

    >>>
    and stdio is a member of C, because that's also the header for the printf() function right?
    <<<

    Kind of right. <stdio.h> was a C header, however, for backward compatibility, C type headers are acceptable in C++

    nima_ranjbar:

    >>> but,why?

    Because...

    >>> C and C++ I/O systems buffer differently

    ... !!!

    >>>
    char *myString;
    myString=new char[20];
    cout<<"Name:";
    gets(myString);//This runs first
    cout <<"\nYour name is"<<myString;
    <<<

    Your cout has gone to the filebuf object of your stream, it has not been "flushed". Your gets() is coming from the stream, (NOT the same buffering as the cout). Add a "flush" to your cout.

    It is poor practice to mix C and C++ I/O. Don't do it, you don't get that kind of problem. As I said, there are some quite subtle effects which can be very difficult to debug.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    >>It is poor practice to mix C and C++ I/O. Don't do it, you don't >>get that kind of problem

    Again, that is completely true. I am not trying to combine C with C++, am just experiencing different combinations.
    Is this only with IO or there are other C functions which intrupt C++ functionality?

    Nima

  15. #15
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    well that's just great

    I didn't use any C++ I\O streams in my random.cpp, so I guess it's just a plain old C program. I can barely tell the 2 apart anyway. I'm not even entirely sure what a buffer is, and all I know about streams is that they carry water. I guess I've got a lot to learn.

    But I'm tired, an now I must brave the nine hells of Java.

    Why can't I go to a real school where I get a choice...


    aurevoir
    "The mind, like a parachute, only functions when open."

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 07:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 02:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 09:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM

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