Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

converting string to int

Posted on 2004-10-16
3
214 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 400 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 100 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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