Solved

converting string to int

Posted on 2004-10-16
3
212 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to correct a C6001issue? 5 217
linker section missing in visual studio 1 84
Visual Studio 2015 compiler settings for 32bit 1 105
L prefix in C++ 8 109
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

774 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