472,794 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

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 46200
Luuk
1,047 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,065 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,065 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

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

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
5
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats...
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
1
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
3
by: qwert7465 | last post by:
I wrote this program and took care of most of the errors and when i execute it, it works but something is wrong. It is a store and i put some products in this store and after you made your selection,...
2
by: dcowboyz31 | last post by:
/* I didnt copy my functions on here so it wouldnt be as long if those could be the problem ill copy those as well */ #include <stdio.h> #include <math.h> #include <stdlib.h> ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.