473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 46432
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> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.