Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting string to int

Posted on 2004-10-16
3
Medium Priority
?
217 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
[X]
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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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