Simple question?

Don't know why this code doesn't work:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()

{
char val = '0';
char val2 = '0';

string code;

code = val+val2+"";

cout << code << endl << endl;

return 0;

}

I want the screen print to be 00 but I'm either getting only symbols or nothing. I've also tried with "" + val + val2; or 'val'+'val2'+"";. I'm using Code::Blocks for my IDE.

What am I missing?
hmm you may want to instead do this

1
2
3
4
5
string code = "";
code += val;
code += val2;
//you could put the third thing here
//but it's....not ...... I have no idea what you're trying to do with it. 


Not positive if that's the best way but I guarantee it'll work ;)


edit: I think you may actually be adding the ascii values of val and val2 anyone confirm this?
Last edited on
Try:

1
2
code.push_back(val);
code.push_back(val2);
Thanks ultifinitus, that works exactly. This is just a piece of a larger code that tests a default constructor - I thought I should know how to do this, but just couldn't get it to work. Thanks again.
No problem, don't forget the awesome firedraco though =)
Topic archived. No new replies allowed.