![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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;
}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;
} |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
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:
|
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
ok this is no help but DID YOU EVEN THINK OF SEARCHING THE FORUMS?
__________________
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
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?
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
|
Quote:
![]()
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi freelance scripts - http://ryanguthrie.com/index.html |
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
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?". |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |