Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

CONVERT char in int C++

char vector[250];
int x;

cout <<  "entry type:  ";
cin >> vector; 

# example vector = {'0','1','0','1','1' };
x = vector[0];
# example x = 0 (interger)

Open in new window



Any suggestions?
0
CreuLINUX
Asked:
CreuLINUX
  • 2
  • 2
  • 2
1 Solution
 
phoffricCommented:
Not sure what the question is. What problem are you having?
0
 
Daniel JungesCommented:
try this:

x = (int)vector[0]; // here you get the ascii code

x = (int)vector[0] - 48; // here you get the number "only for numerics"
0
 
phoffricCommented:
Well, if you are trying to convert an ascii representation of a digit, then you can say:
    x = vector[0] - '0';  // Using '0' is preferable to magic numbers like 48
Converting a char to an int does not require a type cast, although it doesn't hurt to have it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CreuLINUXAuthor Commented:
drawing on the question whether I have

[code]
char vector[250];
int result;
#example vector = {'0','1','0','1','1' };
# in binary this would be: 01011
# in decimal: 11
# how do I get the "result" to receive the value 11?
result = 11;
[/code]
0
 
Daniel JungesCommented:
convert you vector to a string then:

int value = Integer.parseInt("1001", 2);**//*2*for*binary
0
 
CreuLINUXAuthor Commented:
but in c++ not c#
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now