what's wrong with this program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zee Malik
    New Member
    • Jan 2014
    • 6

    what's wrong with this program?

    I am getting the error:
    [Error] 'atoi' was not declared in this scope
    Code:
    #include <iostream>
    	  #include <string.h>
    
    	   using namespace std;
    	   int main()
    	   {
    	       string str = "131.90";
            int n;
    	       n = atoi(str);
    	       cout << n;
    	       return 0;
    	   }
    Last edited by Frinavale; Jan 13 '14, 04:15 PM. Reason: Added the error that the code is throwing.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Did it return an error?, or did it return unexpected results?

    Comment

    • Zee Malik
      New Member
      • Jan 2014
      • 6

      #3
      [Error] 'atoi' was not declared in this scope

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Add
        Code:
        #include <stdlib.h>
        to your imports. That's where atoi is defined.
        Alternatively, if this is supposed to be C++, use the following imports:
        Code:
        #include <iostream>
        #include <cstring>
        #include <cstdlib>

        Comment

        • Zee Malik
          New Member
          • Jan 2014
          • 6

          #5
          after added <cstdlib>

          [Error] cannot convert 'std::string {aka std::basic_stri ng<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            And rightfully so - string is a type which has a char array but isn't one itself. Try using the string::c_str() function:
            Code:
            n = atoi(str.c_str());

            Comment

            • pundliknm
              New Member
              • Jan 2014
              • 4

              #7
              See this below code. It's not a big deal. Again confusion?

              Code:
              #include <iostream>
                  #include <string.h>
                  #include <stdlib.h>
                  
                   using namespace std;
                   int main()
                   {
                     char str[100] = "131.90";
                     int n;
                     n = atoi(str);
                     cout << n;
                     return 0;
                   }
              Last edited by Frinavale; Jan 13 '14, 04:15 PM. Reason: Added code tags.

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                That should work too, yes. But using the string class is a good idea, it's much easier to use in many cases than a char array or char pointer.

                Comment

                • pundliknm
                  New Member
                  • Jan 2014
                  • 4

                  #9
                  c_str() returns a const char*.

                  A common use of c_str() is precisely to convert a C++ std::string to a const char* C string.

                  Not only using the string class is a good idea but also char*.

                  Both works fine.

                  Comment

                  • Zee Malik
                    New Member
                    • Jan 2014
                    • 6

                    #10
                    thank you pundliknm
                    it is working.
                    i'm on initial stage to learn C++ language.
                    any other tip.

                    Comment

                    • pundliknm
                      New Member
                      • Jan 2014
                      • 4

                      #11
                      Zee Malik,
                      I am also not expert in C++. If I know something on it, definitely I will share with you.

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        You should only really call a function like .c_str if you need to call a legacy C function. In this case atoi is a legacy C function but C++ has plenty of better options that you could use (actually the legacy C standard library has better options to use other than atoi). In a good C++ design you should be avoiding dropping down to C constructs like arrays if at all possible favouring vector<> or array<> instead.

                        This can be implemented entirely using the C++ library using a stringstream which has the advantage that you cen tell how much of the string has been converted.

                        Code:
                        #include<iostream>
                        #include<string>
                        #include<sstream>
                        
                        using namespace std;
                        
                        int main()
                        {
                          string str = "131.90";
                          string leftover;
                          istringstream iss;
                          int result;
                          
                          iss.str(str);
                          
                          iss >> result;
                          iss >> leftover;
                          
                          cout << "Converted: " << result << " Leftover: " << leftover << endl;
                        }
                        Output: Converted: 131 Leftover: .90

                        By simply changing the type of result to double I can convert the whole string.

                        Comment

                        • pundliknm
                          New Member
                          • Jan 2014
                          • 4

                          #13
                          I can convert whole string in one line.

                          Code:
                              #include <iostream>
                              #include <string.h>
                              
                              using namespace std;
                          		 
                               int main()
                               {
                                 string str= "131.90";
                                 int n;
                                 n=stoi(str);
                                 cout<<n;
                               }
                          stoi() converts string to integer.
                          Its better & easy to use compare to atoi() & c_str() & iss.str().

                          Don't forget to run like this : g++ -std=c++0x ............

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            You are correct, I overlooked this because it is part of C++11 (and extensions on some compilers) and I am not altogether familiar with C++11 yet as my project is still using C++98

                            Comment

                            Working...