Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting string to int

Posted on 2004-10-16
3
Medium Priority
?
218 Views
Last Modified: 2010-04-24
hi,
i'm using visual studio. i consider myself a novice when it comes to c++

i have a string that i'd like to convert to an int:

//not real code...
int functionReturningInt(){
    char number[8];
    //read user input from std in
    cin.getline (number, sizeof(number));
    //do something to number so it becomes an int
    return number;
}

i can just do cin >> number (or << whatever, it's late), but i'd like to have some kind of user validation.
any ideas?
btw, take the code sample as pseudocode :)
thanks,
daniel416
0
Comment
Question by:daniel416
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1600 total points
ID: 12331354
int functionReturningInt(){
    char number[8];
    //read user input from std in
    cin.getline (number, sizeof(number));

    int n = atoi(number);

    return n;
}
0
 
LVL 16

Assisted Solution

by:nonubik
nonubik earned 400 total points
ID: 12332974
You can also use sscanf(..) function:

int functionReturningInt(){
    char number[8];
    //read user input from std in
    cin.getline (number, sizeof(number));
    int n;
    if(sscanf(number, "%d", &n) != 1)
    {
       //could not convert string to number
       cout << "Error converting";
       return 0;
    }
    return n;
}
0
 
LVL 3

Author Comment

by:daniel416
ID: 12336313
Thanks!
I used the atoi solution, but the sscanf looks interesting, too (maybe i'll come in useful one day)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question