How can i write this?

Jun 30, 2011 at 11:09pm
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?
Jun 30, 2011 at 11:10pm
Use int. char is for characters, as the name already says.
Jun 30, 2011 at 11:47pm
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.
Jun 30, 2011 at 11:54pm
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
Jun 30, 2011 at 11:59pm
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 Jul 1, 2011 at 12:00am
Jul 1, 2011 at 8:58am
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 Jul 1, 2011 at 9:00am
Jul 1, 2011 at 9:19am
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???
Jul 1, 2011 at 9:50am
It is impossible to have a char value of '12345', unless you mean a vector of chars.
Jul 1, 2011 at 10:09pm
Hmm.How can make
1
2
int Value1 = 0;
string Value2 = Convert.toString(Value1); 

in C#?
Jul 1, 2011 at 10:10pm
or:
1
2
string a="1";
int b=Convert.toInt32(a);
Jul 1, 2011 at 10:12pm
It can make with TextField or another data input-output tools.
Last edited on Jul 1, 2011 at 10:12pm
Jul 1, 2011 at 10:16pm
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
Jul 2, 2011 at 3:02am
char data type contains only 1bytes(Enlish Charaters are all 1 bytes.). So, you should use array list or pointer or String class.
Jul 2, 2011 at 10:51am
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;
}
Jul 2, 2011 at 11:35am
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 ... :))
Jul 2, 2011 at 8:34pm
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.
Jul 2, 2011 at 8:39pm
Thank u Janlan.
Last edited on Jul 2, 2011 at 8:43pm
Topic archived. No new replies allowed.