iostream cin problem - c++

This is a discussion on iostream cin problem - c++ ; Hi: Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in a console program. I use cin.getline() and sometimes just cin Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP ...

+ Reply to Thread
Results 1 to 10 of 10

iostream cin problem

  1. Default iostream cin problem

    Hi:

    Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
    a console program. I use cin.getline() and sometimes just cin

    Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
    IIRC on VS enterprise. Really pushing the envelope here.

    This program works fine if I run it from VC++, but even after doing a
    release build, if I run the exe, it acts like I didn't enter a thing
    when my prompt "enter number" or "enter anything" comes up and I enter a
    valid number (or "anything")

    It's strange. At first, the function clock() returned zero. Now that
    works. I also had array corruption (array initialized in a constructor)
    happening and the fix was to declare a dummy variable in the class
    constructor. Ok enough horror. Here's a snippet - the problem snip du
    jour (sp?), if you will:

    Any idea why the cin object isn't doing it's job? Please? I did try
    running it in 95 compatibility mode. It's a relatively fresh XP install,
    too. And I'm carefull about what SW I install.

    Thanks in advance
    Mike

  2. Default Re: iostream cin problem

    On 2007-08-19 01:01, Active8 wrote:
    > Hi:
    >
    > Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
    > a console program. I use cin.getline() and sometimes just cin
    >
    > Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
    > IIRC on VS enterprise. Really pushing the envelope here.
    >
    > This program works fine if I run it from VC++, but even after doing a
    > release build, if I run the exe, it acts like I didn't enter a thing
    > when my prompt "enter number" or "enter anything" comes up and I enter a
    > valid number (or "anything")
    >
    > It's strange. At first, the function clock() returned zero. Now that
    > works. I also had array corruption (array initialized in a constructor)
    > happening and the fix was to declare a dummy variable in the class
    > constructor. Ok enough horror. Here's a snippet - the problem snip du
    > jour (sp?), if you will:
    >
    > Any idea why the cin object isn't doing it's job? Please? I did try
    > running it in 95 compatibility mode. It's a relatively fresh XP install,
    > too. And I'm carefull about what SW I install.


    First off, you forgot to post the snippet, and, while it probably has
    nothing to do with your problem, you really should install SP2 for XP.

    --
    Erik Wikström

  3. Default Re: iostream cin problem

    On Aug 18, 7:13 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
    > On 2007-08-19 01:01, Active8 wrote:
    >
    >
    >
    > > Hi:

    >
    > > Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
    > > a console program. I use cin.getline() and sometimes just cin

    >
    > > Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
    > > IIRC on VS enterprise. Really pushing the envelope here.

    >
    > > This program works fine if I run it from VC++, but even after doing a
    > > release build, if I run the exe, it acts like I didn't enter a thing
    > > when my prompt "enter number" or "enter anything" comes up and I enter a
    > > valid number (or "anything")

    >
    > > It's strange. At first, the function clock() returned zero. Now that
    > > works. I also had array corruption (array initialized in a constructor)
    > > happening and the fix was to declare a dummy variable in the class
    > > constructor. Ok enough horror. Here's a snippet - the problem snip du
    > > jour (sp?), if you will:

    >
    > > Any idea why the cin object isn't doing it's job? Please? I did try
    > > running it in 95 compatibility mode. It's a relatively fresh XP install,
    > > too. And I'm carefull about what SW I install.

    >
    > First off, you forgot to post the snippet, and, while it probably has
    > nothing to do with your problem, you really should install SP2 for XP.
    >
    > --
    > Erik Wikström



    Sorry <blush> and sorry for posting via google, but mozilla is sucking
    ram. I thoght I saw the prob, but it should f up either wa I run the
    program.

    cout << "Hexagram number now enter: ";
    cin >> hexnum;

    Yeah. It's an I Ching program.

    I don't know if something is in the buffer like a white space or what.
    I *seem to have better luck with cin.getline, but as I said, this crap
    app does run out of VC++

    Thanks for the speedy response.

    If you can, could you tell me the best way to do SP2 without upgrading
    all that stuff I might not need. Seems like the online upgrade
    "wizard" want's to give me everything under the sun. I especially want
    to keep messenger services, MSN Messenger, and Alexa far away. I also
    don't want any new services set to run at startup.

    Thanks again.
    Mike



  4. Default Re: iostream cin problem

    "Active8" <mike.dcc> wrote in message
    news:1187485424.115835.18610@k79g2000hse.googlegroups.com...
    On Aug 18, 7:13 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
    > On 2007-08-19 01:01, Active8 wrote:
    >
    >
    >
    > > Hi:

    >
    > > Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
    > > a console program. I use cin.getline() and sometimes just cin

    >
    > > Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
    > > IIRC on VS enterprise. Really pushing the envelope here.

    >
    > > This program works fine if I run it from VC++, but even after doing a
    > > release build, if I run the exe, it acts like I didn't enter a thing
    > > when my prompt "enter number" or "enter anything" comes up and I enter a
    > > valid number (or "anything")

    >
    > > It's strange. At first, the function clock() returned zero. Now that
    > > works. I also had array corruption (array initialized in a constructor)
    > > happening and the fix was to declare a dummy variable in the class
    > > constructor. Ok enough horror. Here's a snippet - the problem snip du
    > > jour (sp?), if you will:

    >
    > > Any idea why the cin object isn't doing it's job? Please? I did try
    > > running it in 95 compatibility mode. It's a relatively fresh XP install,
    > > too. And I'm carefull about what SW I install.

    >
    > First off, you forgot to post the snippet, and, while it probably has
    > nothing to do with your problem, you really should install SP2 for XP.
    >
    > --
    > Erik Wikström



    Sorry <blush> and sorry for posting via google, but mozilla is sucking
    ram. I thoght I saw the prob, but it should f up either wa I run the
    program.

    cout << "Hexagram number now enter: ";
    cin >> hexnum;

    Yeah. It's an I Ching program.

    I don't know if something is in the buffer like a white space or what.
    I *seem to have better luck with cin.getline, but as I said, this crap
    app does run out of VC++

    Thanks for the speedy response.

    If you can, could you tell me the best way to do SP2 without upgrading
    all that stuff I might not need. Seems like the online upgrade
    "wizard" want's to give me everything under the sun. I especially want
    to keep messenger services, MSN Messenger, and Alexa far away. I also
    don't want any new services set to run at startup.

    =====

    Well, if you go to windowsupdate.microsoft.com after authenticating your
    windows XP license, it will search your machine and build a list of things
    it wants to instal (critical updates). You can then, if you desire, go
    through that list and uncheck anything you don't want to install. However, I
    would really suggest you get all the critical updates. They close a number
    of security holes in XP that can leave your computer vulnerable.



  5. Default Re: iostream cin problem


    Active8 <mike.dcc> wrote in message...

    > cout << "Hexagram number now enter: ";
    > cin >> hexnum;


    'cout' is not defined. 'cin' is not defined.
    'operator<<' is not defined.
    'hexnum' is not defined.
    In other words, you have not posted enough code to determine your problem.

    I'll give you an example of what the people in this NG want to see:

    #include <iostream>

    int main(){
    int hexnum(0);
    std::cout << "Hexagram number now enter: ";
    std::cin >> hexnum;
    std::cout << "\nthe number entered: "<<hexnum<<std::endl;
    // - may need if more input. (ignores next char in buffer) -
    // std::cin.ignore(); // .ignore(2); skips 2 chars, etc.
    // std::cin >> hexnum;
    return 0;
    } // main()
    [ note: when posting, please use spaces, not tabs, when indenting. ]

    FAQ http://www.parashift.com/c++-faq-lite

    --
    Bob R
    POVrookie



  6. Default Re: iostream cin problem

    On Aug 19, 12:14 am, "BobR" <removeBadB...@worldnet.att.net> wrote:
    > Active8 <mike....> wrote in message...
    > > cout << "Hexagram number now enter: ";
    > > cin >> hexnum;

    >
    > 'cout' is not defined. 'cin' is not defined.
    > 'operator<<' is not defined.
    > 'hexnum' is not defined.
    > In other words, you have not posted enough code to determine your problem.
    >
    > I'll give you an example of what the people in this NG want to see:
    >
    > #include <iostream>
    >
    > int main(){
    > int hexnum(0);
    > std::cout << "Hexagram number now enter: ";
    > std::cin >> hexnum;
    > std::cout << "\nthe number entered: "<<hexnum<<std::endl;
    > // - may need if more input. (ignores next char in buffer) -
    > // std::cin.ignore(); // .ignore(2); skips 2 chars, etc.
    > // std::cin >> hexnum;
    > return 0;
    > } // main()
    > [ note: when posting, please use spaces, not tabs, when indenting. ]
    >
    > FAQ http://www.parashift.com/c++-faq-lite
    >
    > --
    > Bob R
    > POVrookie



    The above example you provided is indicative of the relevant code in
    my prog except I might need an ignore() (ok that didn't help.) If
    something in the code after the cin is doing it, I wouldn't know where
    and to post the whole prog is a lot of tabs to change. cin *does work
    elsewhere. Can't run debug 'cause it only fouls up when I'm not in vc+
    + IDE. Remember the really odd thing is that it only screws up when I
    run the prog "normally" i.e. not clicking "run" button in VC++? The
    program flow is the same either way except the flow stops in the case
    of the former 8)

    Is it possible that VC++ is preventing a condition (in the stream
    buffer?) from occuring?

    When I enter a valid number and hit return, the cursor just goes to a
    new line, nothing else happens, I can't enter anything after that ,
    and have to ctrl-c kill the prog?

    I'm including iostream via fstream.h and

    oh what joy, not. I tried std::cin >> hexnum;

    error C2653: 'std' : is not a class or namespace name

    Been so long since I saw that I don't remember the fix.

    Using cin as opposed to std::cin couldn't have adverse effects, could
    it?

    Thanks for the link. Great FAQ

    Hope we're not chasing a non c++ related rabbit.

    Thanks
    Mike


  7. Default Re: iostream cin problem


    Active8 <mike.dcc> wrote in message...
    >
    > When I enter a valid number and hit return, the cursor just goes to a
    > new line, nothing else happens, I can't enter anything after that ,
    > and have to ctrl-c kill the prog?
    >
    > I'm including iostream via fstream.h and


    That's a problem. Always specifically include the headers you need. Do NOT
    depend on one header includeing another.
    'fstream.h' is a 'C' header. For 'C++', do it this way:

    #include <iostream>
    #include <fstream> // NO '.h'

    If you use the 'C' headers, use the ones provided for 'C++'.

    // #include <time.h>
    #include <ctime>
    // #include <stdlib.h>
    #include <cstdlib>
    .... etc.. Check your implementation, or docs, for others.

    >
    > oh what joy, not. I tried std::cin >> hexnum;
    >
    > error C2653: 'std' : is not a class or namespace name
    > Been so long since I saw that I don't remember the fix.


    #include <fstream> // NO '.h' for C++

    >
    > Using cin as opposed to std::cin couldn't have adverse effects, could
    > it?


    It should not compile. Turn up the warning level of your compiler.

    Try those 'fixes', and post back here.

    --
    Bob R
    POVrookie



  8. Default Re: iostream cin problem

    On Aug 19, 3:28 pm, "BobR" <removeBadB...@worldnet.att.net> wrote:
    > Active8 <mike....> wrote in message...
    >
    > > When I enter a valid number and hit return, the cursor just goes to a
    > > new line, nothing else happens, I can't enter anything after that ,
    > > and have to ctrl-c kill the prog?

    >

    <snip>
    >


    > > Using cin as opposed to std::cin couldn't have adverse effects, could
    > > it?

    >
    > It should not compile. Turn up the warning level of your compiler.


    warnings do not a failed compile make IMO, but I get you.

    cin (no wonder it's used alone in the first c++ book I read. The
    author must have learned c first and isn't half the coder you are?
    wish I had my c/c++ ref. handy)

    cin did compile and still does after the header changes unless I've
    another include that's now free of the inclusion guards. I'll check.

    But std::cin does work now.
    >
    > Try those 'fixes', and post back here.


    Lots of warnings about stuff not in the brainbench certification

    char mybits;
    char operator ^ (char rhs) {return myBits ^ rhs;} // was warning prior
    to the include file changes. The xor forces an implicit cast of myBits
    to int, IIR[or assume]C ? Wonder why I no longer need that protection
    from myself. Is the c++ include checking to make sure there's no
    possibility of data loss?

    I like this one from <XMEMORY>:

    .... INCLUDE\xmemory(39) : warning C4100: '_P' : unreferenced formal
    parameter

    microsoft's own code creates warnings from an include of an include of
    who knows how many included cludes (or is it "kludge"?) nah... it must
    be me.

    still digging but still the same prob. I'm at warn level 4, BTW was at
    3. I'll conceed it's good to see the warnings up to a point maybe
    turning off specific ones one is aware of and can live with.

    I'll keep at it checking for the naughty includes. Thanks for
    elucidating me on something I rarely touch. Just trying to get the
    classes finished and have a quick tool that I can let friends try. As
    soon as I get the warnings cleaned up, if the prob still exists, I'll
    just use getline() and validate the input since getline() has been
    nicer to me. I wasn't going to guard against all the stoopid user
    tricks - let 'em crash it if they want to waste time entering
    meaningless values. It'll be a windows app when it grows up. OTHOH
    fixing it right saves me the effort of warning them against easter egg
    hunting. They're adults but we like to play, too.

    Tnx
    Mike


  9. Default Re: iostream cin problem


    Active8 <mike.dcc> wrote in message...
    > <snip>
    >
    > I'll keep at it checking for the naughty includes. Thanks for
    > elucidating me on something I rarely touch. Just trying to get the
    > classes finished and have a quick tool that I can let friends try. As
    > soon as I get the warnings cleaned up, if the prob still exists, I'll
    > just use getline() and validate the input since getline() has been
    > nicer to me.


    If 'getline' works (show me/us which one you are using, and how), then
    std::cin should too. You most likely have something left in the buffer. Try
    this after the first input:

    // .....
    std::cin>>MyVar;
    if( std::cin.peek() == '\n' ){
    std::cin.ignore(1); // clear a 'return' left in buffer.
    std::cout<<"removed a return."<<std::endl;
    }
    if( not std::cin ){
    std::cout<<"exiting."<<std::endl;
    // std::terminate(); // pick one
    // exit(1);
    }
    std::cin>>MyVar2;
    // check again
    // .....

    An 'std::getline' removes the '\n', std::cin' leaves it in buffer.

    Don't be afraid to put plenty of 'couts' in your code to test things, they
    are easily removed once everything is working.
    If you have lots of input, you could put the 'check' in a function:
    [ just for a loose idea. ]

    bool CheckCin( std::istream &in ){
    if( not in ){
    return false;
    }
    if( in.peek() == '\n' ){
    in.ignore(1);
    std::cout<<"removed a return."<<std::endl;
    }
    return true;
    } // CheckCin(istream&)

    // main()
    std::cin>>MyVar;
    if( not CheckCin( std::cin ) ){ exit(1); }
    std::cin>>MyVar2;
    if( not CheckCin( std::cin ) ){ exit(1); }


    > I wasn't going to guard against all the stoopid user
    > tricks - let 'em crash it if they want to waste time entering
    > meaningless values. It'll be a windows app when it grows up. OTHOH
    > fixing it right saves me the effort of warning them against easter egg
    > hunting. They're adults but we like to play, too.
    > Tnx
    > Mike


    Well, when you get to 'windows', std::cin, std::cout, etc. won't work! <G>

    --
    Bob R
    POVrookie



  10. Default Re: iostream cin problem

    Active8 wrote:
    :: On Aug 19, 3:28 pm, "BobR" <removeBadB...@worldnet.att.net> wrote:
    ::: Active8 <mike....> wrote in message...
    :::
    :::: When I enter a valid number and hit return, the cursor just goes
    :::: to a new line, nothing else happens, I can't enter anything
    :::: after that , and have to ctrl-c kill the prog?
    :::
    :: <snip>
    :::
    ::
    :::: Using cin as opposed to std::cin couldn't have adverse effects,
    :::: could it?
    :::
    ::: It should not compile. Turn up the warning level of your compiler.
    ::
    :: warnings do not a failed compile make IMO, but I get you.
    ::
    :: cin (no wonder it's used alone in the first c++ book I read. The
    :: author must have learned c first and isn't half the coder you are?
    :: wish I had my c/c++ ref. handy)
    ::
    :: cin did compile and still does after the header changes unless I've
    :: another include that's now free of the inclusion guards. I'll
    :: check.
    ::
    :: But std::cin does work now.
    :::
    ::: Try those 'fixes', and post back here.
    ::
    :: Lots of warnings about stuff not in the brainbench certification
    ::
    :: char mybits;
    :: char operator ^ (char rhs) {return myBits ^ rhs;} // was warning

    You cannot define operators for built-in types like char. They are
    predefined.


    :: prior to the include file changes. The xor forces an implicit cast
    :: of myBits to int, IIR[or assume]C ? Wonder why I no longer need
    :: that protection from myself. Is the c++ include checking to make
    :: sure there's no possibility of data loss?
    ::
    :: I like this one from <XMEMORY>:
    ::
    :: ... INCLUDE\xmemory(39) : warning C4100: '_P' : unreferenced formal
    :: parameter
    ::
    :: microsoft's own code creates warnings from an include of an
    :: include of who knows how many included cludes (or is it "kludge"?)
    :: nah... it must be me.

    Yes, it's you - for using an ancient compiler. :-)

    The warnings in standard headers were fixed in VS2002, that's over 5
    years ago.

    After you security update your OS, you might consider getting a recent
    C++ compiler. Visual C++ Express, for example, has an exceptional
    price/performance ratio.


    Bo Persson



+ Reply to Thread

Similar Threads

  1. iostream thorwing constructors
    By Application Development in forum c++
    Replies: 0
    Last Post: 09-13-2007, 08:55 AM
  2. Question on iostream
    By Application Development in forum c++
    Replies: 4
    Last Post: 06-08-2007, 02:44 PM
  3. unable to open iostream.h
    By Application Development in forum c++
    Replies: 3
    Last Post: 12-26-2006, 01:47 PM
  4. <iostream> query
    By Application Development in forum c++
    Replies: 5
    Last Post: 10-07-2006, 12:46 AM
  5. iostream it's a joke right?
    By Application Development in forum c++
    Replies: 55
    Last Post: 10-01-2006, 05:58 PM