469,300 Members | 2,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

what's wrong with this program?

I am getting the error:
[Error] 'atoi' was not declared in this scope
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.       #include <string.h>
  3.  
  4.        using namespace std;
  5.        int main()
  6.        {
  7.            string str = "131.90";
  8.         int n;
  9.            n = atoi(str);
  10.            cout << n;
  11.            return 0;
  12.        }
Jan 12 '14 #1

✓ answered by Nepomuk

Add
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
to your imports. That's where atoi is defined.
Alternatively, if this is supposed to be C++, use the following imports:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>

13 43611
Luuk
1,043 Expert 1GB
Did it return an error?, or did it return unexpected results?
Jan 12 '14 #2
[Error] 'atoi' was not declared in this scope
Jan 13 '14 #3
Nepomuk
3,112 Expert 2GB
Add
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
to your imports. That's where atoi is defined.
Alternatively, if this is supposed to be C++, use the following imports:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
Jan 13 '14 #4
after added <cstdlib>

[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'
Jan 13 '14 #5
Nepomuk
3,112 Expert 2GB
And rightfully so - string is a type which has a char array but isn't one itself. Try using the string::c_str() function:
Expand|Select|Wrap|Line Numbers
  1. n = atoi(str.c_str());
Jan 13 '14 #6
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.     #include <string.h>
  3.     #include <stdlib.h>
  4.  
  5.      using namespace std;
  6.      int main()
  7.      {
  8.        char str[100] = "131.90";
  9.        int n;
  10.        n = atoi(str);
  11.        cout << n;
  12.        return 0;
  13.      }
Jan 13 '14 #7
Nepomuk
3,112 Expert 2GB
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.
Jan 13 '14 #8
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.
Jan 13 '14 #9
thank you pundliknm
it is working.
i'm on initial stage to learn C++ language.
any other tip.
Jan 13 '14 #10
Zee Malik,
I am also not expert in C++. If I know something on it, definitely I will share with you.
Jan 13 '14 #11
Banfa
9,064 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.   string str = "131.90";
  10.   string leftover;
  11.   istringstream iss;
  12.   int result;
  13.  
  14.   iss.str(str);
  15.  
  16.   iss >> result;
  17.   iss >> leftover;
  18.  
  19.   cout << "Converted: " << result << " Leftover: " << leftover << endl;
  20. }
  21.  
Output: Converted: 131 Leftover: .90

By simply changing the type of result to double I can convert the whole string.
Jan 13 '14 #12
I can convert whole string in one line.

Expand|Select|Wrap|Line Numbers
  1.     #include <iostream>
  2.     #include <string.h>
  3.  
  4.     using namespace std;
  5.  
  6.      int main()
  7.      {
  8.        string str= "131.90";
  9.        int n;
  10.        n=stoi(str);
  11.        cout<<n;
  12.      }
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 ............
Jan 14 '14 #13
Banfa
9,064 Expert Mod 8TB
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
Jan 14 '14 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Chris Geerdink | last post: by
5 posts views Thread by Alexandre Martins | last post: by
3 posts views Thread by mahsa | last post: by
5 posts views Thread by islayer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.