Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 12th, 2006, 5:23 AM   #1
sayu
Newbie
 
Join Date: Nov 2006
Posts: 10
Rep Power: 0 sayu is on a distinguished road
convert to int

hello
can anybody say the code required to convert a string to integer datatype

that is get a number as a string type and convert it integer

ex
char num[10];
cout<<"Enter a number: ";
cin>>num;

// convert num to int datatype so that arithmetic calcs is possible
sayu is offline   Reply With Quote
Old Nov 12th, 2006, 7:04 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
In this case you can set your num variable to an int, cause you are using streams. Otherwise you should google for the atoi(...) function.

/Klarre
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Nov 12th, 2006, 7:04 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Firstly, read the sticky post, always at the top of the C++ forum, entitled "How to post a question". It will give you useful tips on how to ask questions in a way that increases your chance of getting a useful response. One part of that is learning how to use [code[bf][/bf]] tags to present code in a more readable form.

If all you want to do is read an int from a standard stream, do this;
#include <iostream>

int main()
{
    int num;
    std::cout << "Enter a number : ";
    std::cin >> num;
}
This works because the standard stream types support input and output of basic types.

If you want to read it as a string first (which is valid, as it means you can do extra error checking) one approach is this;
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char buffer[10];
    std::cout << "Enter a number : ";
    std::cin.getline(buffer, 10);           // note that this does not overflow buffer
    
    std::string temp(buffer);              //  store copy of buffer in a string
    std::istringstream mystream(temp);  // create a stream to read from temp
    int value;
    mystream >> value;
}
grumpy is offline   Reply With Quote
Old Nov 12th, 2006, 11:18 AM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
i think there is a function which will allow you to do this ...i am not quite sure but i think it is called atoi() and needs the inclusion of the stdlib header file...like i said not quite sure...?
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 12th, 2006, 3:03 PM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
If you are to use C functions atoi is not the best function for str->num conversion. You should use: strtol because it allows better error checking.
Game_Ender is offline   Reply With Quote
Old Nov 12th, 2006, 10:26 PM   #6
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
ok this is no help but DID YOU EVEN THINK OF SEARCHING THE FORUMS?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Nov 12th, 2006, 10:45 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 934
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Why doesn't someone compile a list of answers to all the basic C++ questions like this one that get asked millions of times and post it as a sticky in this forum?
titaniumdecoy is offline   Reply With Quote
Old Nov 13th, 2006, 1:35 AM   #8
Mocker
Hobbyist Programmer
 
Mocker's Avatar
 
Join Date: Oct 2005
Location: Indiana
Posts: 224
Rep Power: 0 Mocker is an unknown quantity at this point
Send a message via AIM to Mocker
Quote:
Originally Posted by titaniumdecoy View Post
Why doesn't someone compile a list of answers to all the basic C++ questions like this one that get asked millions of times and post it as a sticky in this forum?
Thanks for volunteering
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi
freelance scripts - http://ryanguthrie.com/index.html
Mocker is offline   Reply With Quote
Old Nov 13th, 2006, 3:12 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by titaniumdecoy View Post
Why doesn't someone compile a list of answers to all the basic C++ questions like this one that get asked millions of times and post it as a sticky in this forum?
If you wish to volunteer, go ahead.

In my experience, the majority of such questions come from newbies who have not bothered to read forum guidelines or any existing FAQs. So, after preparing a FAQ, you would probably just be changing the focus of your frustration towards "why don't they read the FAQ?".
grumpy is offline   Reply With Quote
Old Nov 13th, 2006, 4:16 AM   #10
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
or you could make a chat bot that would parse every thing and come up with the best answers.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert char[14] to char * myName C++ 1 Mar 5th, 2006 9:08 PM
Convert to double myName C# 12 Dec 14th, 2005 8:45 AM
How to convert from any base number system to any base in TrueBasic? linzi53 Other Programming Languages 4 Oct 30th, 2005 8:45 AM
Convert int to string Programmingb52 C# 1 Apr 23rd, 2005 6:16 PM
Could anyone please convert this into assembly ? oxyi Assembly 9 Mar 13th, 2005 6:37 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC