Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Question.

C++ Question.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
23 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kevin McFarlane

    Works OK for me in VC++ 6. Kevin

    W Offline
    W Offline
    WREY
    wrote on last edited by
    #10

    Strange! because I have VC++ 6, and I tried a dozen or more different techniques before posting the question, and I couldn't get any of them produce the desired behavior. :confused: William Fortes in fide et opere!

    K P 2 Replies Last reply
    0
    • J Jeryth

      Try using strcmp(). I've had problems trying to use normal logical operators on strings and char arrays before but using the different compare functions sure saved me from headaches. The question "Do computers think?" is the same as "Can submarines swim?" DragonFire Software Jeryth

      W Offline
      W Offline
      WREY
      wrote on last edited by
      #11

      Thanks for your reply. Because I've worked with 'string' so often, and have equally studied its syntaxes and member functions in the C++ Standard library so much as well, I can say that the authors of 'string' went above and beyond their call of duty to make it as robust as possible. Not only is this reflected in the ways they have tried to make it as seamless for backward compatibility when dealing with C-Style 'char*' string, but they have sought to expand it with features that the old C-Style string did not possess. For example, the C++ Standard has overloaded the string comparison operators to accept three different kinds of comparisons when comparing std::strings. 1) a std::string with another std::string 2) a std::string with a C-Style char* cstr 3) a C-Style char* cstr with a std::string Those operators represent, ==, !=, <, >, <=, and >=. So there is no need to use the old C-Style 'strcmp' or 'strncmp' functions (which deals specifically with C-Style char* strings) when working with std::string. I believe the problem I'm experiencing, has more to do with 'getline()' than with 'strings'. I've had so many problems in the past using 'getline()', that I shun it like the plague. It's a beast of its own kind that seems to operate like a renegade. It might have good qualities, but (for me) it doesn't seem to be very lending. :sigh: William Fortes in fide et opere!

      1 Reply Last reply
      0
      • W WREY

        Strange! because I have VC++ 6, and I tried a dozen or more different techniques before posting the question, and I couldn't get any of them produce the desired behavior. :confused: William Fortes in fide et opere!

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #12

        Hmmm. I'm puzzled then. I think my VC++ has SP3 applied. perhaps that makes a difference? And I tried it on NT4. Kevin

        W 1 Reply Last reply
        0
        • K Kevin McFarlane

          Hmmm. I'm puzzled then. I think my VC++ has SP3 applied. perhaps that makes a difference? And I tried it on NT4. Kevin

          W Offline
          W Offline
          WREY
          wrote on last edited by
          #13

          Thanks for your reply. Even stranger! that you have VC++ 6, SP3 and claim it works for you, while I with VC++ 6, SP5, cannot get the kind of behavior I'm looking for. The problem (it looks more and more to be), is that 'getline() isn't receiving any data from the input queue, because a 'cout' of the string (which is the input buffer) reveals NOTHING in it. Consequently, when the test is made to see if data is absent, the test succeeds because NO data has been collected and is the reason why the invalid segment gets activated. So the problem doesn't seem to rest with 'string', but more with 'getline()'. To be honest with you, 'getline()' has been a problem child for me every time I try to use it, and this time is no exception. :sigh: William Fortes in fide et opere!

          1 Reply Last reply
          0
          • W WREY

            In the use of

            string str;
            getline(cin, str); // data gets entered here
            if(str=="???" || str=="")
            {
            // invalid string
            }
            else
            {
            // VALID string
            }

            The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

            M Offline
            M Offline
            Michael Gunlock
            wrote on last edited by
            #14

            There is a known bug in the getline() VC 6.0 version (see MS KB #240015). This KB article has a "fix" which requires editing the string header file. It may not be related to the problem your having, but underscores your suspicions of getline(). By the way, it executes as expected in VS.NET 2003 without revision. Regards Mike

            W 2 Replies Last reply
            0
            • M Michael Gunlock

              There is a known bug in the getline() VC 6.0 version (see MS KB #240015). This KB article has a "fix" which requires editing the string header file. It may not be related to the problem your having, but underscores your suspicions of getline(). By the way, it executes as expected in VS.NET 2003 without revision. Regards Mike

              W Offline
              W Offline
              WREY
              wrote on last edited by
              #15

              Thanks for your reply. I'm looking into it. BTW, are you sure about that KB number? Thanks. :) William Fortes in fide et opere!

              1 Reply Last reply
              0
              • W WREY

                Strange! because I have VC++ 6, and I tried a dozen or more different techniques before posting the question, and I couldn't get any of them produce the desired behavior. :confused: William Fortes in fide et opere!

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #16

                I dont understand why the comparison with three "?" is it a desired input?? or is it the comparison for uninitialised junk string? The World is getting smaller and so are the people.

                1 Reply Last reply
                0
                • M Michael Gunlock

                  There is a known bug in the getline() VC 6.0 version (see MS KB #240015). This KB article has a "fix" which requires editing the string header file. It may not be related to the problem your having, but underscores your suspicions of getline(). By the way, it executes as expected in VS.NET 2003 without revision. Regards Mike

                  W Offline
                  W Offline
                  WREY
                  wrote on last edited by
                  #17

                  Thanks for the tip. I did locate the KB article and the string file to which the error refered. However, even after I had made the recommended change the article offered, the problem with 'getline()' still persist. :sigh: William Fortes in fide et opere!

                  1 Reply Last reply
                  0
                  • W WREY

                    In the use of

                    string str;
                    getline(cin, str); // data gets entered here
                    if(str=="???" || str=="")
                    {
                    // invalid string
                    }
                    else
                    {
                    // VALID string
                    }

                    The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

                    S Offline
                    S Offline
                    Stye
                    wrote on last edited by
                    #18

                    Move the cin, e.g. char line[100]; cout << " Type a line terminated by 't'" << endl; cin.getline( line, 100, 't' ); cout << line;

                    W 1 Reply Last reply
                    0
                    • S Stye

                      Move the cin, e.g. char line[100]; cout << " Type a line terminated by 't'" << endl; cin.getline( line, 100, 't' ); cout << line;

                      W Offline
                      W Offline
                      WREY
                      wrote on last edited by
                      #19

                      Thanks for replying. Your suggestion has merit to it (perhaps) for a number of other situations, but for what I'm doing, unless I can engender a character (other than any printable character), using a special terminating character (as this form of 'getline()' requires) would not work for me. For example, if I were to use "t" as my terminating character, there could very well be instances where I might need "t" as a single character to be by itself, which means when the 'getline()' function sees a single "t" standing by itself, it would interpret that single "t" as the terminating character and transfer the collected data from the input queue to the buffer, when in truth and fact, in that particular instance I did not mean for "t" to be a terminating character (it may have been meant to be data). This is especially true when dealing with scientific equations and formulas. :sigh: William Fortes in fide et opere!

                      S 1 Reply Last reply
                      0
                      • W WREY

                        Thanks for replying. Your suggestion has merit to it (perhaps) for a number of other situations, but for what I'm doing, unless I can engender a character (other than any printable character), using a special terminating character (as this form of 'getline()' requires) would not work for me. For example, if I were to use "t" as my terminating character, there could very well be instances where I might need "t" as a single character to be by itself, which means when the 'getline()' function sees a single "t" standing by itself, it would interpret that single "t" as the terminating character and transfer the collected data from the input queue to the buffer, when in truth and fact, in that particular instance I did not mean for "t" to be a terminating character (it may have been meant to be data). This is especially true when dealing with scientific equations and formulas. :sigh: William Fortes in fide et opere!

                        S Offline
                        S Offline
                        Stye
                        wrote on last edited by
                        #20

                        Then simply use: cin.getline( line, 100) for the example given. The DEFAULT will be '\n' (carriage return) added as the final character when the return (enter) key is pressed.

                        W 1 Reply Last reply
                        0
                        • S Stye

                          Then simply use: cin.getline( line, 100) for the example given. The DEFAULT will be '\n' (carriage return) added as the final character when the return (enter) key is pressed.

                          W Offline
                          W Offline
                          WREY
                          wrote on last edited by
                          #21

                          In case you haven't been reading the other posts, 'getline()' has a bug in it, and is unfit to use (at least for VC++ 6). Even the fix that MS has issued, DOES NOT WORK!! Take my word for it, I am very acquainted with the various iostream member functions (including 'getline()') and the only reason why I posted the question on this forum, was in hope that somebody may have found a workaround for the problems this I/O function has been plagued with. I understand VC++ 7 does not have this problem, and that is good news for those who might want to use it. :suss: William Fortes in fide et opere!

                          1 Reply Last reply
                          0
                          • W WREY

                            In the use of

                            string str;
                            getline(cin, str); // data gets entered here
                            if(str=="???" || str=="")
                            {
                            // invalid string
                            }
                            else
                            {
                            // VALID string
                            }

                            The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

                            S Offline
                            S Offline
                            Stye
                            wrote on last edited by
                            #22

                            I have 6.00 and works fine for me, try this out: #include "stdafx.h" #include "iostream.h" #include "string.h" int main(int argc, char* argv[]) { char line[100]; cout << "Type a line" << endl; cin.getline( line, 100); if(strcmp(line, "xxx") == 0) cout << "You entered xxx"; else cout << "You did not enter xxx"; return 0; }

                            1 Reply Last reply
                            0
                            • W WREY

                              In the use of

                              string str;
                              getline(cin, str); // data gets entered here
                              if(str=="???" || str=="")
                              {
                              // invalid string
                              }
                              else
                              {
                              // VALID string
                              }

                              The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

                              S Offline
                              S Offline
                              Stye
                              wrote on last edited by
                              #23

                              Doing it the way you have you have to press the enter key twice after getline to get a response (the bug you were referring to). See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q240015 for the fix ( the fix does work if you make the changes in the right file. The file referred to is in two different locations. You might as well change the code in both: MicrosoftVisualStudio\VC98\Include\String and MicrosoftVisualStudio\VC98\CRT\SRC\String I did and the enter key only has to be pressed once now and the code below which is like your way of doing it works fine. #include "stdafx.h" #include "string" #include "iostream" using namespace std ; int main(int argc, char* argv[]) { string s1; cout << "Enter a sentence:"; getline(cin,s1); if(s1 == "???" || s1 == "") cout << "First case"; else cout << "Second case"; return 0; }

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups