How can i write this?

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main(){
char a=12345;
cout<<a<<endl;
system("PAUSE");
return 0;}


How can i write 12345 number in console?
Use int. char is for characters, as the name already says.
I know this:
1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main(){
int a=12345;
cout<<a<<endl;
system("PAUSE");
return 0;}


But i don't want it.I want this:
1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){char a[]="12345";
cout<<(int)a<<endl;
system("PAUSE");
return 0;}

This result is not 12345.I want doesn't change result.
closed account (zb0S216C)
Helegurbann wrote:
char a=12345;

I don't think 12345 is a valid ASCII value. When a char object is assigned an numerical value, the character that has the same numerical on the ASCII table is used. This is the same for an int. When an int object is assigned a character, the numerical value of the assigned character is used.

Wazzak
You can skip the C-style cast (the (int)) on line 4. << does work with char*s, though I'd suggest that you use strings instead. :)

1
2
3
4
5
6
7
#include <iostream>
#include <string>
int main(){
std::string a="12345";
std::cout<<a<<std::endl;
std::cin.sync(), std::cin.ignore();
return 0;}
12345


-Albatross
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
char a[5]("12345");

cout<<a<<endl;

cin.get(); cin.get();
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
int a = 1234;

cout<<a<<endl;

cin.get(); cin.get();
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
int a;

cout<<Enter number: ";
cin>>a; 

cin.get(); cin.get();
return 0;
} 

Last edited on
Janlan.I want to convert char to int and convert while doesn't change result.
My char value:12345
My char to int convert value:12345
ok???
It is impossible to have a char value of '12345', unless you mean a vector of chars.
Hmm.How can make
1
2
int Value1 = 0;
string Value2 = Convert.toString(Value1); 

in C#?
or:
1
2
string a="1";
int b=Convert.toInt32(a);
It can make with TextField or another data input-output tools.
Last edited on
In C++, this is done with lexical_cast:

1
2
int Value1 = 0;
string Value2 = lexical_cast<string>(Value1);

and

1
2
string a="1";
int b=lexical_cast<int>(a);


See also:
http://www.boost.org/doc/libs/1_46_1/libs/conversion/lexical_cast.htm
char data type contains only 1bytes(Enlish Charaters are all 1 bytes.). So, you should use array list or pointer or String class.
Convert char to int with atoi reference...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>

using namespace std;

int main ()
{

	char c[6]("12345");

	int n;

	n = atoi(c);

	cout<<n;






cin.get(); cin.get();
return 0;
}
If you want to make int sum of char variables, convert char to string, and then string to int...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{

	char c[6]("12345");
	char cc[6]("12345");

	int sum;
	string n;
	string nn;
	
	n = string(c);
	nn = string(cc);

	int number1, number2;

	stringstream s, ss;

	s<<n;
	s>>number1;

	ss<<nn;
	ss>>number2;

	sum = number1 + number2;

	cout<<sum<<endl;


cin.get(); cin.get();
return 0;
}


...think this is enough for explanation ... :))
I found easier method.In here:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
string a="12345";
for(int i=0;i<a.length();i++)
cout<<((int)a[i])-48;
system("PAUSE");
return 0;}

This is not impossible and this is not hard.
Thank u Janlan.
Last edited on
Topic archived. No new replies allowed.