getline problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MC felon

    getline problem

    getline isn't working.....
    what's wrong with this code?

    #include <iostream>
    #include <string>
    #include <conio2.h// ignore this.. my old habits

    class base
    {
    int x,y;
    std::string str;
    public:
    base() {std::cout<<"I' m the constructor of class
    base\n"<<std::e ndl;}
    void setdata(int p,int h)
    {
    x=p;
    y=h;
    }
    void draw()
    {
    gotoxy(x,y);
    std::cout<<"$\n "<<std::end l;
    }
    void clear()
    {
    clrscr();
    }
    void tryagain()
    {
    using std::cout;
    using std::cin;
    int main();
    cout<<"would you like to try again?\n"<<std: :endl;
    getline(cin,str );
    if(str == "yes" || str == "y")
    {
    clrscr();
    main();
    }
    else { exit(0); }
    }
    };


    int main()
    {
    int p,h;
    base b;
    base* bt;
    bt = &b;
    std::cin>>p;
    std::cin>>h;
    bt -clear();
    bt -setdata(p,h);
    bt -draw();
    bt -tryagain();
    return 0;
    }


    when i finish with the drawing, it says, "would you like to try again?"
    and then exits (without waiting
    for my reply).
    HELP!

  • Ondra Holub

    #2
    Re: getline problem


    MC felon napsal:
    getline isn't working.....
    what's wrong with this code?
    >
    #include <iostream>
    #include <string>
    #include <conio2.h// ignore this.. my old habits
    >
    class base
    {
    int x,y;
    std::string str;
    public:
    base() {std::cout<<"I' m the constructor of class
    base\n"<<std::e ndl;}
    void setdata(int p,int h)
    {
    x=p;
    y=h;
    }
    void draw()
    {
    gotoxy(x,y);
    std::cout<<"$\n "<<std::end l;
    }
    void clear()
    {
    clrscr();
    }
    void tryagain()
    {
    using std::cout;
    using std::cin;
    int main();
    cout<<"would you like to try again?\n"<<std: :endl;
    // You must skip end of line from first input
    cin.ignore(std: :numeric_limits <size_t>::max() );
    getline(cin,str );
    if(str == "yes" || str == "y")
    {
    clrscr();
    main();
    }
    else { exit(0); }
    }
    };
    >
    >
    int main()
    {
    int p,h;
    base b;
    base* bt;
    bt = &b;
    std::cin>>p;
    std::cin>>h;
    bt -clear();
    bt -setdata(p,h);
    bt -draw();
    bt -tryagain();
    return 0;
    }
    >
    >
    when i finish with the drawing, it says, "would you like to try again?"
    and then exits (without waiting
    for my reply).
    HELP!
    BTW: You are calling main() from tryagain method. That means you are
    using recursion. You should better return some value from tryagain
    method and test it in main.

    Comment

    • Jack Klein

      #3
      Re: getline problem

      On 22 Dec 2006 05:39:25 -0800, "Ondra Holub" <ondra.holub@po st.cz>
      wrote in comp.lang.c++:
      >
      MC felon napsal:
      getline isn't working.....
      what's wrong with this code?
      [snip]
      BTW: You are calling main() from tryagain method. That means you are
      using recursion. You should better return some value from tryagain
      method and test it in main.
      You can't use main() recursively in C++. The standard explicitly
      prohibits calling main() from within a program.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • red floyd

        #4
        Re: getline problem

        Jack Klein wrote:
        On 22 Dec 2006 05:39:25 -0800, "Ondra Holub" <ondra.holub@po st.cz>
        wrote in comp.lang.c++:
        >
        >MC felon napsal:
        >>getline isn't working.....
        >>what's wrong with this code?
        >
        [snip]
        >
        >BTW: You are calling main() from tryagain method. That means you are
        >using recursion. You should better return some value from tryagain
        >method and test it in main.
        >
        You can't use main() recursively in C++. The standard explicitly
        prohibits calling main() from within a program.
        >
        For MC and Ondra:

        Jack is correct. See 3.6.1, specifically 3.6.1/3.

        Comment

        • Micah Cowan

          #5
          Re: getline problem


          Ondra Holub wrote:
          MC felon napsal:
          void tryagain()
          {
          using std::cout;
          using std::cin;
          int main();
          cout<<"would you like to try again?\n"<<std: :endl;
          >
          // You must skip end of line from first input
          cin.ignore(std: :numeric_limits <size_t>::max() );
          That should go after the int extractors, ideally, not before each
          getline(). Also, you would want to specify the delimiter, as the
          default delimiter is EOF.

          std::numeric_li mits<size_t>::m ax() is not the right value: you want
          std::numeric_li mits<std::strea msize>::max(). These are /never/ the same
          type (though POSIX's ssize_t would be a good fit), as size_t is
          unsigned whereas streamsize is required to be signed.
          getline(cin,str );

          Comment

          Working...