Problem with atoi()?

This is a discussion on Problem with atoi()? within the C++ Programming forums, part of the General Programming Boards category; I've done a little program which reads in info from a text file, but there seems to be a problem ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Problem with atoi()?

    I've done a little program which reads in info from a text file, but there seems to be a problem when I try to output certain numbers.

    Code for the program in question:

    Code:
    #include <iostream>
    #include <conio>
    #include <vector>
    #include <fstream>
    using namespace std;
    namespace Myprog {
      namespace Globals{};
    
      class Example {
        unsigned int counter;
      public:
        Example(unsigned int val)
        {
          counter = val;
        }
        
        unsigned int get_val() { return counter; }
    
      };
      
      namespace Globals {
        vector <Example *> objects;
        ifstream in;
      }
      
      namespace Script_Processing {
        bool make_example();
      
        void read_script()
        {
          Globals::in.open("script.txt", ios::in | ios::binary); // The binary flag doesn't seem to make a difference.
          if(!Globals::in) {
            cout << "Unable to open script file.\n";
            exit(1);
          }
          string hold_read; // Holds the string currently being tested.
          for( ; ; ) {
            while(isspace(Globals::in.peek())) Globals::in.get();
            Globals::in >> hold_read.begin();
            if(!strcmp(hold_read.begin(), "MakeExample")) {
              cout << "About to call make_example()\n";
              if(Script_Processing::make_example()) return;
            }
            if(Globals::in.eof()) {
              cout << "Error processing script.\n";
              exit(1);
            }
          }
        }
    
        bool make_example()
        {
          cout << "Inside make_example()\n";
          string hold_read1;
          string hold_read2;
          for( ; ; ) {
            while(isspace(Globals::in.peek())) Globals::in.get();
            if(Globals::in.peek() == '{') {
              Globals::in.get();
              continue;
            }
            else break;
          }
          Globals::in >> hold_read1.begin();
          if(!strcmp(hold_read1.begin(), "IntVar")) {
            while(isspace(Globals::in.peek())) Globals::in.get();
            Globals::in >> hold_read2.begin();
            Globals::objects.push_back(new Example(atoi(hold_read2.begin())));
            return true;
          }
          return false;
        }
      } // End of namespace Script_Processing;
    
    
    
    
      int main()
      {
        Script_Processing::read_script();
    
        cout << "Val in object: " << Globals::objects[0]->get_val() << '\n';
    
        cout << "Not stuck in an infinite loop!\n";
        
        delete Globals::objects[0];
    
        getch();
    
        return 0;
      }
    } // End of namespace myprog;
    The script it reads from (a text file called script):

    MakeExample {
    IntVar 196010
    }

    The problem is that when the first number is either 1 or 0, the value output has a number of commas which seem to be related to the number of leading 1s or 0s (except when the number is 10, 100 etc in which case there are a lot of commas). I'm wondering if the problem is somewhere other than the atoi() call though, because this other little test program seems to work OK:

    #include <iostream>
    #include <conio>
    using namespace std;


    Code:
    int main()
    {
      char *text = "196010";
      int val = atoi(text);
      cout << val << '\n';
      
      getch();
      
      return 0;
    }
    Any suggestions?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    What's all the .begin() stuff?

    > Globals::in >> hold_read.begin();
    Globals::in >> hold_read;

    > if(!strcmp(hold_read.begin(), "MakeExample"))
    if( hold_read == "MakeExample" )

    > atoi(hold_read2.begin()
    atoi(hold_read2.c_str())

    Also, this
    if(Globals::in.eof())
    needs to be immediately after you've read something from the file, not after you've tried to process a failed read result.

    Also, all your peek-ahead loops will lock up if there is an EOF at the wrong point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    Just followed your advice and the problem seems to be fixed.

    What's all the .begin() stuff?
    I couldn't find anything about an operator>> for use with strings in my documentation on istream so I assumed that I would have to do everything through char pointers. Changing to using the string itself seems to solve the problem. Intend to look up exactly what operator==(string, char*) and stream.c_str do when I do more rigorous testing.

    Still have to test the advice regarding EOF though, I assumed I would slip up on that somehow.

    Thanks.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I couldn't find anything about an operator>> for use with strings in my documentation on istream
    You'll find the operator>> overloads in your documentation for <string>.

    >so I assumed that I would have to do everything through char pointers
    string::begin() isn't guaranteed to give you a pointer to a character. It gives you an iterator, which is allowed to be a class without an implicit conversion to char*. If you want a char* from a string, use the c_str member function.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Make sure you actually #include<string>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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