|
-
Jan 24th, 2003, 11:28 PM
#1
Thread Starter
Frenzied Member
String to Int How?
is this the proper way?
5. How to convert a string to a number?
- Use the atoi function to convert a string to an integer.
Example:
---------------------------------------------------------------------------------------------------------------------------
#include <iostream> //preprocessor statement
#include <stdlib.h> //contains itoa & atoi
using namespace std; //choosing the namespace that we are going to use
int main() //this is where the program starts executing
{
char buffer[20];
int i = 3445;
_itoa( i, buffer, 10 );
cout<<buffer<<endl;
return 0;
}
Is that the way its the same as he has for the Int to String. I found this in the main tutorial link
I Tried using the below
VB Code:
char buffer[20];
char buffer2[20];
::GetWindowText (edit,buffer,255);
::GetWindowText (edit,buffer2,255);
int i = buffer;
int i2 = buffer2;
int i3 = i + i2;
_itoa( i, buffer, 10 );
_itoa( i2, buffer2, 10 );
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 09:48 AM
#2
Code:
char buffer2[20];
::GetWindowText (edit,buffer,255);
::GetWindowText (edit,buffer2,255);
int i = buffer;
int i2 = buffer2;
int i3 = i + i2;
_itoa( i, buffer, 10 );
_itoa( i2, buffer2, 10 );
Ok, think about it. First, you allocate a buffer for 19 characters. Then you retrieve the text of an edit box, where you tell windows that you have space for 255 characters. First error.
Then you assign the buffers to integers. This won't compile. And in the thing you quoted it says "Use the atoi function to convert a string to an integer." Second error.
Finally you convert those integers (that have a random value) back to strings and store them in the buffers. I guess that's not what you wanted to do either.
Ok, so you basically used the wrong function. Here's how to get an integer from an edit box:
Code:
char buffer[256]; // You gotta keep your promises.
::GetWindowText(edit, buffer, 255);
int i = atoi(buffer);
Or simpler (if you have the id of the edit box):
Code:
int i = ::GetDlgItemInt(IDC_EDIT);
_itoa is a Microsoft-specific function. The standard way to convert an integer to a string in C is sprintf:
Code:
sprintf(buffer, "%i", i);
and in C++ stringstream
Code:
std::ostringstream oss;
oss << i;
std::string s = oss.str();
(from the header <sstream>).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 25th, 2003, 02:48 PM
#3
Thread Starter
Frenzied Member
ok so far so good now i tried
VB Code:
std:stringstream oss;
oss << i3;
std::string s = oss.str();
but no good i get like 3 errors
DO i have to include any h files?
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 02:53 PM
#4
Thread Starter
Frenzied Member
Ok now i got it lol im so stupid thanks alot you do help so much and srry bout before
VB Code:
char buffer[256]; // You gotta keep your promises.
char buffer2[256]; // You gotta keep your promises.
char buffer3[256]; // You gotta keep your promises.
::GetWindowText(edit, buffer, 255);
::GetWindowText(edit2, buffer2, 255);
int i = atoi(buffer);
int i2 = atoi(buffer2);
int i3 = i + i2;
sprintf(buffer3, "%i", i3);
::SetWindowText(edit3,buffer3);
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 26th, 2003, 06:48 AM
#5
Fanatic Member
(from the header <sstream> ).
DO i have to include any h files?
I rest my case...
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 26th, 2003, 07:06 AM
#6
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|