Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Mac Stringstream returns wrong output

Mac Stringstream returns wrong output

Scheduled Pinned Locked Moved Unsolved C++ Gurus
c++qtcreator 4.9.1stringstreammac os
32 Posts 8 Posters 7.6k Views
  • 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.
  • M mranger90
    31 May 2019, 13:22

    @Bugi said in Mac Stringstream returns wrong output:

    I would suggest changing this:

    for (size_t i{0}; i < 3; i++) {
    

    to the more traditional

    for (size_t  i = 0; i < 3; i++) {
    

    It's possible that your compiler is having an issue with that kind of initialization

    B Offline
    B Offline
    Bugi
    wrote on 31 May 2019, 13:33 last edited by
    #10

    @mranger90
    I tried it.
    but that didn't make any difference.
    But as you can see in the pictures of debug from
    the above answer. it iterate well through
    (I will add the debugging image in the question.)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mranger90
      wrote on 31 May 2019, 13:50 last edited by
      #11

      Your images don't show the contents of "poly" so it's hard to tell if it's being set correctly.
      try breaking it into pieces like:

          std::vector<std::pair<double, int> > poly;
          std::stringstream stringstream(p);
      
          for (size_t i{0}; i < 3; i++) {
              std::pair<double, int> tpair;
              stringstream >> tpair.first;
              stringstream.ignore(2);
              stringstream >>  tpair.second;
              poly.push_back(tpair);
          }
          return poly;
      

      And looking at "tpair" to see if stringstream is setting it correctly.

      1 Reply Last reply
      1
      • F Offline
        F Offline
        fcarney
        wrote on 31 May 2019, 13:55 last edited by
        #12

        What C++ standard is the compiler set to? Can you force it to say -std=c11? Be curious to know what version of the standard it is set to for compiling now. Is the clang setup gcc based or something else? You said it worked in mingw, which is basically gcc. Is it possible to get a gcc compiler for MacOS?

        Another option to consider. Completely uninstall your Qt installation and reinstall. I had issues with a Qt install installation where header files were corrupted and all sorts of weird problems. Some things would work, and others would not. I did a drive check in Windows and found corrupted sectors. This caused the corruption of the files. This was in Windows 7 however.

        C++ is a perfectly valid school of magic.

        B 1 Reply Last reply 31 May 2019, 21:10
        0
        • F Offline
          F Offline
          fcarney
          wrote on 31 May 2019, 14:14 last edited by
          #13

          The code as presented in the original post works in gcc like @Bugi said about it working in mingw. I tried the following to see if it made any difference:

          QMAKE_CXXFLAGS += -std=c++98
          QMAKE_CXXFLAGS += -std=c++11
          QMAKE_CXXFLAGS += -std=c++14
          QMAKE_CXXFLAGS += -std=c++17
          

          I tested each separately, but they did not change the output at all. I get this on output:

          22x^3+2x^2-1x^0
          

          System I tested on:
          Ubuntu 18.04 Linux
          gcc/g++ 7.4.0
          64 bit compile

          Hope this helps you narrow it down.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1
          • F Offline
            F Offline
            fcarney
            wrote on 31 May 2019, 14:30 last edited by
            #14

            I just installed clang 7 on Linux and compiled the program like this:

            clang++7 main.cpp
            

            This produced "a.out".
            I ran this and it produced:

            22x^3+2x^2-1x^0
            

            I don't know if clang is different on Linux than on MacOS though. I don't know what version you have either. If you can tell me the version I can see if that works.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1
            • B Bugi
              31 May 2019, 12:41

              @Konstantin-Tokarev
              I Was using debug build. which always resultet in the output: 0x^00x^00x^0
              But if i switch to relaease i get a random output evry time eks:

              • 0x^-19887461000x^-19887461000x^-1988746100
              • 0x^2068517450x^2068517450x^206851745
              • 0x^8690926600x^8690926600x^869092660
              • ....

              I now how to insert brake point and running the debug mode.
              but don't know how to single stepping. or what to look for in the debug window...

              Image of debugging can be fund in dropbox folder...
              link debugging Images

              K Offline
              K Offline
              Konstantin Tokarev
              wrote on 31 May 2019, 15:34 last edited by
              #15

              But if i switch to relaease i get a random output evry time eks:

              • 0x^-19887461000x^-19887461000x^-1988746100
              • 0x^2068517450x^2068517450x^206851745
              • 0x^8690926600x^8690926600x^869092660

              This means that you are printing uninitialized variables somehow.

              but don't know how to single stepping. or what to look for in the debug window...

              https://doc.qt.io/qtcreator/creator-debug-mode.html

              You need to look at value of poly[i].second before and after stringstream >> poly[i].second; line. Also look up value of p at this moment, and look at internal data fields of stringstream. If still unclear, put a breakpoint on that line, and press F11 to get as deep into implementation of stringstream as needed

              1 Reply Last reply
              2
              • F Offline
                F Offline
                fcarney
                wrote on 31 May 2019, 19:07 last edited by fcarney
                #16

                I am running Linux so it only took a couple of minutes to install clangs 3.9, 4.0, 5.0, 6.0, and 7.0. I then compiled and ran your code on all of those versions and it produced what it was supposed to produce (22x^3+2x^2-1x^0). So unless you have a really old version of clang my guess is something else is wrong. I have no clue what could be wrong, but my first inclination is to reinstall Qt completely. In my opinion there is nothing wrong with the code itself.

                Edit: Then again, 6 other people are having the same problem. So no clue on what could be the issue. Sorry.

                Edit 2: @Bugi what does your pro file look like?

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                1
                • M mranger90
                  31 May 2019, 13:22

                  @Bugi said in Mac Stringstream returns wrong output:

                  I would suggest changing this:

                  for (size_t i{0}; i < 3; i++) {
                  

                  to the more traditional

                  for (size_t  i = 0; i < 3; i++) {
                  

                  It's possible that your compiler is having an issue with that kind of initialization

                  B Offline
                  B Offline
                  Bugi
                  wrote on 31 May 2019, 19:37 last edited by
                  #17

                  @mranger90
                  I don't know if this is what you ment by intialization it in a diffrent way...
                  (by creating a double and a int to pass the values into instead.) But it made no difference.

                  std::vector<std::pair<double, int> > readPolynomial(std::string& p)
                  {
                      std::vector<std::pair<double, int> > poly;
                      std::stringstream ss(p);
                  
                      double coeff;
                      int power;
                  
                      for (size_t i = 0 ;i < 3; i++) {
                          ss >> coeff;
                          ss.ignore(2);
                          ss >> power;
                  
                          poly.push_back(std::make_pair(coeff,power));
                      }
                      return poly;
                  }
                  

                  Images of this code in debug mode se folder debug_2
                  if you meant something else with your suggestion can you elaborate a little.

                  The image of the first debug run with the origanal code is also the folder (link above) as debug_1 updatet with visible content of "poly"

                  And you're code sugesting with "std::pair<double, int > tpair" is also in the folder (link above) as debug_3

                  K 1 Reply Last reply 31 May 2019, 21:05
                  0
                  • M Offline
                    M Offline
                    mranger90
                    wrote on 31 May 2019, 19:55 last edited by
                    #18

                    Ok, this is a head scratcher. As indicated by @fcarney I've run your code, as is on a couple of systems (ubuntu 18.04 with gcc 7.4) and Windows 10 with msvc 2017. And they both produce the expected output.
                    So the issue seems to be compiler or environment related. And the fact that debug/release builds produce different output seems to indicated that somewhere, something is not being initialized, but in your simple code that would be easy to spot.
                    The only other issue that pops out on visual inspection is using the variable named "stringstream" which I suppose could cause confusion with the type "stringstream", but one of your tests changes the name to "ss" so that is not the issue.
                    Try parsing the first parameter as an int instead of a double, or even as a float instead of double to see if that makes a difference.

                    1 Reply Last reply
                    0
                    • B Bugi
                      31 May 2019, 19:37

                      @mranger90
                      I don't know if this is what you ment by intialization it in a diffrent way...
                      (by creating a double and a int to pass the values into instead.) But it made no difference.

                      std::vector<std::pair<double, int> > readPolynomial(std::string& p)
                      {
                          std::vector<std::pair<double, int> > poly;
                          std::stringstream ss(p);
                      
                          double coeff;
                          int power;
                      
                          for (size_t i = 0 ;i < 3; i++) {
                              ss >> coeff;
                              ss.ignore(2);
                              ss >> power;
                      
                              poly.push_back(std::make_pair(coeff,power));
                          }
                          return poly;
                      }
                      

                      Images of this code in debug mode se folder debug_2
                      if you meant something else with your suggestion can you elaborate a little.

                      The image of the first debug run with the origanal code is also the folder (link above) as debug_1 updatet with visible content of "poly"

                      And you're code sugesting with "std::pair<double, int > tpair" is also in the folder (link above) as debug_3

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 31 May 2019, 21:05 last edited by
                      #19

                      Be sure the stream's not corrupted:

                      std::stringstream ss(p);
                      ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
                      
                      try  {
                         for (size_t i = 0 ;i < 3; i++) {
                              ss >> coeff;
                              ss.ignore(2);
                              ss >> power;
                      
                              poly.push_back(std::make_pair(coeff,power));
                          }
                      }
                      catch (const std::exception & e)
                      {
                          std::cerr << e.what() << std::endl;
                          throw;
                      }
                      

                      You'd need the appropriate headers too:

                      #include <exception>
                      #include <iostream>
                      

                      Read and abide by the Qt Code of Conduct

                      B 1 Reply Last reply 31 May 2019, 21:34
                      1
                      • F fcarney
                        31 May 2019, 13:55

                        What C++ standard is the compiler set to? Can you force it to say -std=c11? Be curious to know what version of the standard it is set to for compiling now. Is the clang setup gcc based or something else? You said it worked in mingw, which is basically gcc. Is it possible to get a gcc compiler for MacOS?

                        Another option to consider. Completely uninstall your Qt installation and reinstall. I had issues with a Qt install installation where header files were corrupted and all sorts of weird problems. Some things would work, and others would not. I did a drive check in Windows and found corrupted sectors. This caused the corruption of the files. This was in Windows 7 however.

                        B Offline
                        B Offline
                        Bugi
                        wrote on 31 May 2019, 21:10 last edited by
                        #20

                        @fcarney

                        I don't think there are many opportunities to change the compiler on macOS. Since the installation settings come with XCode. Actually tried "this" before I started this thread. and had to format my entire pc and reinstall everything.

                        I treed to type " $ gcc --version " to answer you question. and don't know whether this is useful...

                        MY-MacBook-Air:~ bugi$ gcc --version
                        Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
                        Apple LLVM version 10.0.1 (clang-1001.0.46.4)
                        Target: x86_64-apple-darwin18.6.0
                        Thread model: posix
                        InstalledDir: Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
                        

                        I fund this .pro file in the project folder

                        TEMPLATE = app
                        CONFIG += console c++11
                        CONFIG -= app_bundle
                        CONFIG -= qt
                        
                        SOURCES += \
                                main.cpp
                        
                        HEADERS +=
                        
                        

                        I tried reinstalling the latest Qt Open Source version:
                        Under the instalion setup there is a step where you are installing components: where i have 2 options at this step.
                        1. is to install as. Qt 5.12.3 -> macOS :
                        This installation resultet in nothing working at all!
                        couldn't even compile with the auto-detected kit. Or correct it to something that worked.
                        So I uninstalled it again (Both time with the MaintenanceTool which removed all files from the system).
                        I then tried again this time with the other option under installing component.
                        2. which is to install as. Qt 5.12.2 -> macOS : I was then able to compile my projects again but it made no difference. It is still "0x^00x^00x^0" as output.

                        I alså tried 3.th, 4.th, and a 5.th time.
                        3.th time with both option 1. and 2. simultaneously. this resultere in that nothing would compile. again with the auto-kit or any setting (qmake return 2)
                        The 4.th and 5.th time both time by uninstalling both Qt and XCode. And agin it was only possible to make the qt installation with component: Qt 5.12.2 -> macOS. able to compile any of my projects. still with the wrong output. "0x^00x^00x^0"

                        1 Reply Last reply
                        0
                        • K kshegunov
                          31 May 2019, 21:05

                          Be sure the stream's not corrupted:

                          std::stringstream ss(p);
                          ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
                          
                          try  {
                             for (size_t i = 0 ;i < 3; i++) {
                                  ss >> coeff;
                                  ss.ignore(2);
                                  ss >> power;
                          
                                  poly.push_back(std::make_pair(coeff,power));
                              }
                          }
                          catch (const std::exception & e)
                          {
                              std::cerr << e.what() << std::endl;
                              throw;
                          }
                          

                          You'd need the appropriate headers too:

                          #include <exception>
                          #include <iostream>
                          
                          B Offline
                          B Offline
                          Bugi
                          wrote on 31 May 2019, 21:34 last edited by
                          #21

                          @kshegunov

                          Okay maybe we found something here ...
                          but since I still have very little code experience I don't know if it is something or not... Or for that matter what it means
                          Output:

                          ios_base::clear: unspecified iostream_category error
                          libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                          Press <RETURN> to close this window...
                          

                          It went directly to line 49: (throw) after line 39: (ss >> coeff;)
                          Se folder and Debug_4 for code and images.

                          K J 2 Replies Last reply 1 Jun 2019, 11:23
                          0
                          • F Offline
                            F Offline
                            fcarney
                            wrote on 31 May 2019, 21:34 last edited by
                            #22

                            @Bugi said in Mac Stringstream returns wrong output:

                            I don't think there are many opportunities to change the compiler on macOS.

                            I think Python is the same way on MacOS.

                            Can you add an independent compiler? One that you can run from the command line? Not one that changes the entire system compiler. My Linux system has a standard gcc compiler installed, but I can install clang and it won't mess up everything else. That would at least give you a way to do your school work.

                            I still think the VM option with Linux on it may be your best option. I know you don't want to, but it will give you a lot more options. It will also give you experience in Linux if you don't already have that.

                            C++ is a perfectly valid school of magic.

                            B 1 Reply Last reply 31 May 2019, 21:49
                            0
                            • F fcarney
                              31 May 2019, 21:34

                              @Bugi said in Mac Stringstream returns wrong output:

                              I don't think there are many opportunities to change the compiler on macOS.

                              I think Python is the same way on MacOS.

                              Can you add an independent compiler? One that you can run from the command line? Not one that changes the entire system compiler. My Linux system has a standard gcc compiler installed, but I can install clang and it won't mess up everything else. That would at least give you a way to do your school work.

                              I still think the VM option with Linux on it may be your best option. I know you don't want to, but it will give you a lot more options. It will also give you experience in Linux if you don't already have that.

                              B Offline
                              B Offline
                              Bugi
                              wrote on 31 May 2019, 21:49 last edited by
                              #23

                              @fcarney
                              Agree and have begun to take baby steps with linux (ubunto).
                              I was dedcated windows user some years ago (before windows 8) and have worked on windows machines afterwards continued.
                              regarding my school work i have found a windows machine for the moment.
                              BUT. I am still detement to make it work on mac os. I'm really tired of hearing people say you can't be an engineer with a mac.

                              and I'm really happy with my mac :(
                              And although I have a windows machine it doesn't help the others in my class with the same problem (mac user)

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fcarney
                                wrote on 31 May 2019, 21:57 last edited by
                                #24

                                @Bugi said in Mac Stringstream returns wrong output:

                                I'm really tired of hearing people say you can't be an engineer with a mac.

                                The guy I share an office with is an electrical engineer (30+ years), he is a good programmer, uses a Mac, but runs most of his analysis software and programming tools on a VM running Windows 10. I run Linux and use a VM to run Windows 10 for doing Windows development in Qt. Most of my time is in Linux though. So, there is nothing wrong with a Mac. However, no matter what you run, you will probably need a VM to run something else. Or your boss will make a decision that forces you to run a VM. Its just how it goes.

                                C++ is a perfectly valid school of magic.

                                1 Reply Last reply
                                0
                                • B Bugi
                                  31 May 2019, 21:34

                                  @kshegunov

                                  Okay maybe we found something here ...
                                  but since I still have very little code experience I don't know if it is something or not... Or for that matter what it means
                                  Output:

                                  ios_base::clear: unspecified iostream_category error
                                  libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                                  Press <RETURN> to close this window...
                                  

                                  It went directly to line 49: (throw) after line 39: (ss >> coeff;)
                                  Se folder and Debug_4 for code and images.

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 1 Jun 2019, 11:23 last edited by
                                  #25

                                  @Bugi said in Mac Stringstream returns wrong output:

                                  Okay maybe we found something here ...
                                  but since I still have very little code experience I don't know if it is something or not... Or for that matter what it means

                                  It means one of two things:

                                  1. You're reading past the end of stream.
                                  2. You're reading formatted input (the >> operators), which is not formatted according to the expectation; i.e. there's something wrong while reading from the stream.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  3
                                  • Kent-DorfmanK Offline
                                    Kent-DorfmanK Offline
                                    Kent-Dorfman
                                    wrote on 2 Jun 2019, 00:42 last edited by
                                    #26

                                    just for grins...please replace std:stringtream with std::istringstream and retest. Since you are only using the stream for input, you should use the istream specialization intended for that purpose.

                                    1 Reply Last reply
                                    0
                                    • B Bugi
                                      31 May 2019, 21:34

                                      @kshegunov

                                      Okay maybe we found something here ...
                                      but since I still have very little code experience I don't know if it is something or not... Or for that matter what it means
                                      Output:

                                      ios_base::clear: unspecified iostream_category error
                                      libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                                      Press <RETURN> to close this window...
                                      

                                      It went directly to line 49: (throw) after line 39: (ss >> coeff;)
                                      Se folder and Debug_4 for code and images.

                                      J Offline
                                      J Offline
                                      JKSH
                                      Moderators
                                      wrote on 2 Jun 2019, 01:56 last edited by JKSH 6 Feb 2019, 08:42
                                      #27

                                      @Bugi said in Mac Stringstream returns wrong output:

                                      ios_base::clear: unspecified iostream_category error
                                      libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                                      

                                      Someone found a bug in clang's stringstream exception detection before (http://clang-developers.42468.n3.nabble.com/libc-ios-base-and-exceptions-td4038960.html ); it's quite possible that your compiler has a buggy implementation of stringstream. This would explain why everyone on this forum was unable to reproduce your issue, yet all 6 macOS users in your class have the same issue.

                                      Unless you have a way to try a different compiler on your macOS, you'll have a hard time proving or disproving that the bug is in your compiler.

                                      Does this exercise require you to use stringstream? If not, try a different way of parsing the string.

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      J K 2 Replies Last reply 2 Jun 2019, 02:08
                                      3
                                      • J JKSH
                                        2 Jun 2019, 01:56

                                        @Bugi said in Mac Stringstream returns wrong output:

                                        ios_base::clear: unspecified iostream_category error
                                        libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                                        

                                        Someone found a bug in clang's stringstream exception detection before (http://clang-developers.42468.n3.nabble.com/libc-ios-base-and-exceptions-td4038960.html ); it's quite possible that your compiler has a buggy implementation of stringstream. This would explain why everyone on this forum was unable to reproduce your issue, yet all 6 macOS users in your class have the same issue.

                                        Unless you have a way to try a different compiler on your macOS, you'll have a hard time proving or disproving that the bug is in your compiler.

                                        Does this exercise require you to use stringstream? If not, try a different way of parsing the string.

                                        J Offline
                                        J Offline
                                        JKSH
                                        Moderators
                                        wrote on 2 Jun 2019, 02:08 last edited by JKSH 6 Mar 2019, 03:17
                                        #28

                                        Anyway, in situations like this, it is useful to create minimal test cases to investigate the issue.

                                        int main() {
                                            std::string pstr = "22x^3+2x^2-1x^0";
                                            std::stringstream ss(pstr);
                                        
                                            double coeff = -42.0;
                                            ss >> coeff;
                                            std::cout << coeff << std::endl;
                                        }
                                        

                                        What does this print for you? If it's the wrong value, what happens if you replace pstr with "22"?

                                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                        1 Reply Last reply
                                        3
                                        • J JKSH
                                          2 Jun 2019, 01:56

                                          @Bugi said in Mac Stringstream returns wrong output:

                                          ios_base::clear: unspecified iostream_category error
                                          libc++abi.dylib: terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error
                                          

                                          Someone found a bug in clang's stringstream exception detection before (http://clang-developers.42468.n3.nabble.com/libc-ios-base-and-exceptions-td4038960.html ); it's quite possible that your compiler has a buggy implementation of stringstream. This would explain why everyone on this forum was unable to reproduce your issue, yet all 6 macOS users in your class have the same issue.

                                          Unless you have a way to try a different compiler on your macOS, you'll have a hard time proving or disproving that the bug is in your compiler.

                                          Does this exercise require you to use stringstream? If not, try a different way of parsing the string.

                                          K Offline
                                          K Offline
                                          Konstantin Tokarev
                                          wrote on 2 Jun 2019, 19:35 last edited by
                                          #29

                                          @JKSH said in Mac Stringstream returns wrong output:

                                          Unless you have a way to try a different compiler on your macOS, you'll have a hard time proving or disproving that the bug is in your compiler.

                                          However, it would be wise to update Xcode to the latest available version before investigating further

                                          1 Reply Last reply
                                          2

                                          19/32

                                          31 May 2019, 21:05

                                          • Login

                                          • Login or register to search.
                                          19 out of 32
                                          • First post
                                            19/32
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved