Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Okay So i have this function
long valid(const char* str)
{
int temp = atoi(str)
return long;
}
`str = 9789070002046` //13 digit long
the function returns some random 10 digit number.I have also tried STOI stringstream
#shivam-gupta hi buddy,
You maybe have been quite new to C/C++ programming is that correct? Some people at the forum require you to make explain what your problem is in detail also with the right C++ syntax.
I know what you mean, and it had been a main problem with me in the beginning when I tried to learn C/C++. The function that you might be looking at is atol() not atoi(). From my point of view it is better to convert the string to double, then cast it to a long/integer variable.
The code I used to complete your snippet is this
#include <iostream>
long valid(const char* str);
int main()
{
long test;
const char *str = "9789070002046"; //13 digit long
test = valid(str);
std::cout << test << " sizeof test is " << sizeof(test) << std::endl;
return 0;
}
long valid(const char* str)
{
long temp = (long)atol(str);
return temp;
}
Enjoy coding C++! And remember, life is not too short to learn C++!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I parse a char array such as:
"2019-11-01T17:00:10.000Z"
I want 17:00:10.000 and nothing else. How do I parse with respect to the char array indices?
The simplest and safest way to deal with strings is to use the standard std::string. The std::string has many class member functions that can help with this. One of them is substr that can be used to create a new std::string from a part of a string.
Example:
#include <iostream>
#include <stdexcept>
#include <string>
std::string get_time_str(const std::string& timestamp) {
if(timestamp.size() != 24)
throw std::runtime_error("bad timestamp: "+ timestamp);
return timestamp.substr(11, 12); // start at pos 11 and 12 chars forward
}
int main() {
const char* ts = "2019-11-01T17:00:10.000Z";
std::cout << get_time_str(ts) << '\n';
}
Output:
17:00:10.000
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to ask,
how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?
#include <iostream>
#include <string>
using namespace std::literals::string_literals;
std::string operator*(std::size_t n, const std::string& s)
{
std::string ret;
while (n--)
ret += s;
return ret;
}
int main()
{
std::cout << 5 * std::string("Hallo") << std::endl;
std::cout << 5 * "Test"s << std::endl;
}
What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?
By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.
So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have information stored in an array of char and I would like to pass them to a single double value.
As an example, I want to pass it from,
data[0]=3;
data[1]=.;
data[2]=1;
data[3]=4;
data[4]=1;
data[5]=5;
data[6]=1;
to
double data = 3.14151;
How can I do it?
Thanks!
You can use functions std::stringstream from sstream or strtod from cstdlib or stod (If you are using C++11) as per your need.
Quoting example from cplusplus.com
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
I am assuming that your array actually contains characters and you just forgot the apostrophs. I used an easier, less error-prone way to initialize data, with the added benefit that my version actually does a zero-termination, which yours didn't (as Axel correctly pointed out).
char const * data = "3.1415";
The solution would be:
#include <cstdlib>
// ...
double number = std::strtod( data, NULL );
// ...
Check the strtod() documentation for behaviour in case of error, and how to use the second parameter to check if your conversion actually went as far as you expected.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm not entirely sure what is going on here. I'm guessing because my input is a string and I'm cycling through it one character at a time it is always returning as type char.
I pretty sure a string is actually char*. The only way I can think to fix this is to include and check what type of character it is, but I'd like to avoid doing that. Is there an alternative method using typeid.name() to figure out what the char is?
I'm using gcc compiler
voidQueue outQueue;
string temp = "32ad1f-31f()d";
int i = 0;
while(temp[i] != '\0')
{
outQueue.enqueue(temp[i]);
i++;
}
template<typename T>
void voidQueue::enqueue(T data)
{
T *dataAdded = new T;
*dataAdded = data;
string type(typeid(data).name());
cout<< type;
myQueue::enqueue((void *)dataAdded,type);
}
i want it recognize that char('9') is actually an int
You can use std::isdigit for this:
#include <cctype>
bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);
In your example, T is char and gcc returns "c" for typeid(char).name(), as demonstrated by the following program:
#include <iostream>
#include <typeinfo>
int main() {
std::cout << typeid(char).name() << std::endl;
std::cout << typeid(short).name() << std::endl;
std::cout << typeid(int).name() << std::endl;
std::cout << typeid(long).name() << std::endl;
}
On my compiler, this prints out
c
s
i
l
Given that the name() strings are implementation-defined, this is compliant behaviour.