0

I was wondering if there is any way to parse integers from a string (like in java)? Anyone know of a built in function in c++?

9
Contributors
11
Replies
14
Views
10 Years
Discussion Span
Last Post by rubberman
-1

atoi is a C function that works in C++.

e.g.:

#include <iostream>
#include <cstdio>
#include <cstring>

int main() {
    char s[80];
    int i;
    cin>>s;
    i = atoi(s);
    cout>>i;
    return 0;
}

My C++ is a little rusty, but that's my example. Using the String class might be a little dirrerent.

-1

hey fucktards, I wanted to know if I could parse a single integer from a string that contains multiple substrings** and integers -.-

1

hey ####tards, I wanted to know if I could parse a single integer from a string that contains multiple substrings** and integers -.-

Hey clown. Glad to hear you no longer want to know.

0

gobartatti - As we all can see, you are new to our community. One way of getting you noticed in a good way, and also, to be helped, is to obey the rules (which you agreed upon your registration) and not to embarrass yourself by your posts.

I see you have a question, but this thread in which you posted is 6 years old (threads older than 3 months shouldn't be replied, only if adding important/new information to the topic). I sugest you to start a new topic in wich to exaplain your needs/problems, also proving (by showing some code) that you'd tried to solve that issues.
Good luck.

0

Instead of atoi, use strtol() (string to long) or strtoll() (string to long long). They let you provide a pointer that will be set to the first unconvertable character. It will deal with +- leaders, and all the rest of that cruft. Perfectly fine in C++. If you are reading from a stream in C++, then you are probably better off using the syntax strm >> intvar such as cin >> anInt.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.