473,836 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's wrong with this program?

6 New Member
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
13 46682
pundliknm
4 New Member
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 Recognized Expert Moderator Expert
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
pundliknm
4 New Member
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 Recognized Expert Moderator Expert
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
2400
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."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
5
1988
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 wrong ?? tks
3
2045
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');" %>'>Need Help?</asp:HyperLink> -- mahsa
1
2500
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. Whats wrong? Any clues?
7
2239
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 me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
5
2324
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 the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
1
1448
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 obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all...
3
1916
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, it gives you the receipt. unfortunatly it does not keep track of how many items you selected so it is always zero. any help would be appreciated on this problem. here is the code: // Final Project #include <iostream.h> #include...
2
1612
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> #include <string.h> #define N 20 #define M 5 #define A 10
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9661
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10831
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10536
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10582
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7778
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5815
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.