I created a program to convert a number into its binary format using a string(r), now I want to convert it into integer data type, I found atoi() function(import from cstdlib) on google for conversion from string to integer but its not working.
Here is my code- it shows error click here to see it
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num,n;string r;
cout<<"Enter the number : ";
cin>>num;
while(num!=0){r = (num%2==0?"0":"1")+r;num/=2;}
cout<<"\nBinary value is "<<r<<endl;
n = atoi(r);
cout<<n;
return 0;
}
atoi() takes char arrays(ex. char xd[210]). If you wanna use strings, use stoi() instead.
atoi() expects a const char * (an array of char), where r is a std::string object. You can convert a string to const char * with it's c_str() method.
atoi(r.c_str())
Related
I am trying to pull a specific char from a string and convert it to an int. I have tried the following code, but I am unclear why it doesn't work nor can I find a way to do the conversion.
int value = 0;
std::string s = "#/5";
value = std::atoi(s[2]); // want value == 5
You can create std::string from one char and use std::stoi to convert to integer.
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int value = 0;
string s = "#/5";
value = stoi(string(1, s[2])); //conversion
cout << value;
}
You can write:
std::string s = "#/5";
std::string substring = s.substr(2, 1);
int value = std::stoi(substring);
Using the substr method of std::string to pull out the substring that you want to parse as an integer, and then using stoi (which takes a std::string) instead of atoi (which takes a const char *).
You should read the manual page for atoi() more carefully. The actual prototype is:
int atoi(const char *string)
You are attempting to pass a single character rather than a pointer to a character array. In other words, by using s[2] you are dereferencing the pointer. You could, instead, use:
value = std::atoi(s+2);
or alternatively:
value = std::atoi(&s[2]);
This code doesn't dereference the pointer.
The argument to std::atoi must be char*, but s[2] is char. You need to use its address. And to get a valid C string from a std::string, you need to use the c_str() method.
value = std::atoi(&(s.c_str()[2]));
You should have gotten an error saying that the argument wasn't of the correct type.
I am trying to compare to integer lengths in terms of their digit lengths, and padding out the smallest one with 0's so they are both the same size, ie:
6 and 1500
becomes
0006 and 1500
I cannot get std::stoi to work, to then check the length of each number.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int schoolMethod(int a, int b, int base){
if(std::stoi(a)<std::stoi(b)){
//do stuff
return 0;
}
}
karatsuba.cpp: In function ‘int schoolMethod(int, int, int)’:
karatsuba.cpp:50: error: ‘stoi’ is not a member of ‘std’
stoi takes Const String as parameter, but here you are passing intto it.
Use std::to_string method to convert int into string and then compare their lengths.
If you want to convert your ints to strings to compare length, the following is a guide on how to basically do this.
std::to_string() will convert your ints to strings so you can then compare them.
string.length() will then return the length of the string.
#include <string>
int schoolMethod(int a, int b, int base){
std::string stringa = std::to_string(a); //convert to string
std::string stringb = std::to_string(b); //convert to string
if(stringa.length()<stringb.length()){ //compare string lengths
//do stuff
return 0;
}
}
This question already has answers here:
How to convert a number to string and vice versa in C++
(5 answers)
Closed 9 years ago.
#include <iostream>
using namespace std;
int main() {
string a = "1234"; //How this string convert in integer number
system("pause");
return EXIT_SUCCESS;
}
string a = "1234";
How this convert in integer
You can use std::stoi() to convert a std::string to an int.
#include <iostream>
#include <string>
int main() {
std::string a = "1234"; //How this string convert in integer number
int b = std::stoi(a);
system("pause");
return EXIT_SUCCESS;
}
If you have C++11 and onwards, use
int n = std::stoi(a);
(Pre C++11, you could use std::strtol;)
You could use boosts lexical cast
#include <boost/lexical_cast.hpp>
std::string str_num = "12345";
int value = 0;
try
{
value = boost::lexical_cast<int>(str_num);
}
catch(boost::bad_lexical_cast &)
{
// error with conversion - calling code will deal with
}
This way you can easily modify the code to deal with float or double if your string contains those types of numeric value also
You have to use std::stoi:
#include <iostream>
#include <string>
std::string s = "123";
int number= std::stoi(s);
The C++ Standard has a special function
int stoi(const string& str, size_t *idx = 0, int base = 10);
Probably you can try this
string a = "28787" ;
int myNumber;
istringstream ( a) >> myNumber;
See or you can search for stoi function and see how it can be used. Probably It can work but never try because I dont have the compiler of c++
My compiler is Code::Blocks. I am trying to eliminate vocals from a character sequence.
#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
int i=0;
int main (){
cout<<"sir=";cin.getline(sir,256);
for (i=strlen(sir);i>0;i--){
if (strstr(sir[i],'aeiou')==0){
strcpy(sir+i,sir+i+1);
break;}}
cout<<"sir="<<sir<<"\n";
return 0;
}
I receive the following error:
error: call of overloaded 'strstr(char&, int)' is ambiguous
note: candidates are:
note: char* strstr(char*, cost char*) near match
But I think the problem is on strstr command...
'aeiou' is not a string literal in c/c++ use "aeiou".
In c/c++ string literal are represented inside " "(double quotes)
Read more here
So, apparently, the idea is to remove vowels. As others have said, use "aeiou" and not 'aeiou'. But you also need to use the right function to check whether you have a vowel. That's strchr(const char* s, int c), not strstr. strchr looks for an occurrence of c in the string that s points to, and returns a pointer to that occurrence, or, if it's not found, a pointer to the terminating nil character. So the test in the original code should be:
if (*strchr("aeiou", sir[i] != '\0')
Personally, I'd write this a bit more succinctly:
if (*strchr("aeiou", sir[i]))
As I wrote in the first comment, the expression
strstr(sir[i],'aeiou')
is wrong for two reasons: ' is for single characters, " is for strings, but the main reason is, that strstr finds the occurance of the whole thing, not of the characters separately.
Try this:
#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
char sir2[256];
int i=0;
int main (){
cout<<"sir=";cin.getline(sir,256);
char* reader = sir;
char* writer = sir2;
while(*reader) {
if(*reader!='a' && *reader!='e' && *reader!='i' && *reader!='o' && *reader!='u') {
*writer = *reader;
++writer;
}
++reader;
}
*writer = '\0';
cout<<"sir="<<sir2<<"\n";
return 0;
}
ststr is defined by two function prototypes
const char* strstr( const char* str, const char* target );
char* strstr( char* str, const char* target );
your call is calling as
strstr(sir[i],'aeiou')
the first arg is a char type, not a char * type, so the compiler does know how to map that to const char * or char *
Also check your loop index as
i=strlen(sir)
will over index the char array and
i > 0
will NOT access the last character.
here i have code for calculate hash value of unsigned char
#include <cstdlib>
#include <iostream>
#include<string.h>
using namespace std;
unsigned oat_hash(unsigned char *key,int len)
{
unsigned char *p=key;
unsigned h=0;
int i;
for(i=0;i<len;++i){
h+=p[i];
h+=(h<<10);
h^=(h>>6);
}
h+=(h<<3);
h^=(h>>11);
h+=(h<<15);
return h;
}
using namespace std;
int main(int argc, char *argv[])
{
unsigned char mystring[]="123456789abcdef";
unsigned char *key=&mystring[0];
int n=sizeof(mystring)/sizeof(mystring[0]);//length of mystring
cout<<oat_hash(key,n)<<endl;
//system("PAUSE");
//return EXIT_SUCCESS;
return 0;
}
name of this hash function is so called One-at-a-Time hash(by Bob Jenkins) i have one question is this little part of code correct?
int n=sizeof(mystring)/sizeof(mystring[0]);//length of mystring
because mysting has not built-in function length,i used this
Under the circumstances, yes -- but it's pretty fragile. For example, if you changed your definition from:
unsigned char mystring[]="123456789abcdef";
To:
unsigned char *mystring="123456789abcdef";
Your method of finding the length would produce completely incorrect results. Also note that since your string is made up of chars, the /sizeof(mystring[0]) isn't really necessary either -- sizof(char) == 1 (and the same for signed char or unsigned char).
You normally want to use strlen instead.
Yeah, your code is correct. You may want to compare against the data-type though:
int n=sizeof(mystring) / sizeof(char); //length of mystring
Note that this only works if the string isn't dynamic.
Otherwise use strlen for c-style strings.
I must say, however, C++'s std::string does have a length method, and is much easier to use in the majority of cases - especially when using them with the STL.
Also, boost can do C++ string hashes
Yes I feel that the code will work fine. But ensure that if you pass an array of a string through a method it would not give you the desired result since passing array in functions implicitly passed by a pointer. That time your code can provide a disaster. Other wise it is fine. The other way you can find the length of a string array is like:
int len = 0;
int iCount = 0;
while (mystring[iCount].empty() != true)
{
iCount++;
len++;
}
Then use len as length of the String array
Hope this will help.