hi all members
i would like to know how to convert int to char? can any tell me?
this is what i did is it rite? -
#include <iostream.h>
-
-
int main()
-
{
-
int num;
-
char conv;
-
-
cout<<"Enter the number range between 0-9"<<endl;
-
cin>>num;
-
switch(num)
-
{
-
case 1:
-
{
-
conv = 01;
-
num = conv;
-
break;
-
}
-
case 2:
-
case 3:
-
case 4:
-
case 5:
-
{
-
cout<<"number";
-
break;
-
}
-
default:
-
{
-
cout<<"number";
-
break;
-
}
-
}
-
cout<<num;
-
return 0;
-
}
basically im trying to print 0 infront of the number if it is a single digit.
example if i input 1 it will print 01 on the console window. is this the rite way of doing it? or is there another way of doin it?
thanks for replying and helping me!!cheers~~
regrads andrew
9 5523
Well, you can test to see if the number is lower than 10 (and, thus, has one digit). If it is, then print a 0 out, then the number. If not, just print the number.
Basically, you can use sprintf() to convert integer, float, handle...into a string.
Well, you can test to see if the number is lower than 10 (and, thus, has one digit). If it is, then print a 0 out, then the number. If not, just print the number.
yea i understand wat u mean ganon by using cout to print the 0 infront but besides using cout i would like to rename it have the number of 01 which char can print out but int can't.
Basically, you can use sprintf() to convert integer, float, handle...into a string.
hmmm wondering how to use sprintf()? i'm kinda new to c++!!got any website to see or can any1 tell me how?
hmmm wondering how to use sprintf()? i'm kinda new to c++!!got any website to see or can any1 tell me how?
sprintf() can be used in C to convert numeric data to character arrays. However, in C++ you can use stringstream to convert numeric data to strings, e.g.
http://www.codeproject.com/vcpp/stl/ostringstream.asp
http://www.zeuscmd.com/tutorials/cplusplus/43-StringNumberConversions.php
Funny you should ask how to use sprintf
Like this: -
-
int something;
-
int from = 9;
-
char tobe;
-
something = sprintf(tobe,"%d",&from);
-
-
You clearly tried, so let me explain.
sprintf return the amount of characters converted, the from is the number to be converted.
And the tobe is the char which gets the value of from as a char
Funny you should ask how to use sprintf
Like this: -
-
int something;
-
int from = 9;
-
char tobe;
-
something = sprintf(tobe,"%d",&from);
-
-
You clearly tried, so let me explain.
sprintf return the amount of characters converted, the from is the number to be converted.
And the tobe is the char which gets the value of from as a char
tobe should be a char* (pointer to sufficent chars to take the converted data) and you don't need the & in front of from (which will print a pointer value), e.g. -
int from = 9;
-
char tobe[10];
-
something = sprintf(tobe,"%d",from);
-
tobe will then contain '9' plus '\0' terminator
tobe should be a char* (pointer to sufficent chars to take the converted data) and you don't need the & in front of from (which will print a pointer value), e.g. -
int from = 9;
-
char tobe[10];
-
something = sprintf(tobe,"%d",from);
-
tobe will then contain '9' plus '\0' terminator
Ok, I forgot the *, thanks for the correction
just wanted to throw my 2 cents in *throws*
another way, the c++ way. -
#include <string>
-
#include <sstream>
-
-
inline std::string intToStr(unsigned int x) {
-
std::ostringstream o;
-
if (!(o << x))
-
throw RunTimeError("Converting integer to string failed!");
-
return o.str();
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brad Moore |
last post by:
Hey all,
I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate...
|
by: MilanB |
last post by:
Hello
How to convert char to int?
Thanks
|
by: Alper Akcayoz |
last post by:
Hello Esteemed Developpers
I would like to thank you in advance for your sincere responses
I am a fresh Visual C++ .NET Developer. Can you kindly guide me for How to Convert char* to System::String
|
by: Elioth |
last post by:
Hi...
I need to know how to convert Char Array to Byte Array and vice-versa in VB
2K5
Thanks for all help.
Elioth
|
by: Joah Senegal |
last post by:
Hello I need to convert a chat to a string... but I don't know how to do
this. i;ve searched the internet but I've only find some code to convert
char * to string or char to string....
but I...
|
by: simon |
last post by:
I am a fresh Visual C++ .NET Developer. Can you kindly guide me for
How to Convert char to System::String
I am using windows forms and trying to set a text value referencing
the method...
|
by: Rasheed |
last post by:
Hi,
i have a char pointer buffer which will hold the bitmap buffer, So i
need to convert char *buffer as a bitmap.
becuase when i draw on the Dialog using char *buffer. Nothing will be
dispalyed....
|
by: helios |
last post by:
Hi all,
I'm resolved problem. and I want anybody need me that convert char to array bits
char ConvertChar2ArrayBit(char ch)
{
char Bits;
....
return Bits;
}
for...
|
by: mamul |
last post by:
Hi please some one can help me. how to convert char * to string?
i have take char *argv from command line and want to pass to a function as string object(string str)
i want to first convert argv...
|
by: DaveRook |
last post by:
Hi
I've had a few error messages with this I don't understand why. It works fine in a Windows Form, but now moving it to a website, it's not working. I don't have the option to count the lines as...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |