how to save two digit variables in an array
Feb 11, 2014 at 8:47pm UTC
hi I am trying to redefine an array, but the result can only display one digit variable. I want the program to display 97 98 99 *+, but the program is only displaying 789*+. Any help will be appreciated.
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
int main()
{
string test="abc*+" ;
for (int i=0; i<test.size;i++)
{
if (test[i]=='a' )
{
test[i]='97' ;
}
else if (test[i]=='b' )
{
test[i]='98' ;
}
else if (test[i]=='c' )
{
test[i]='99' ;
}
else if (test[i]=='*' )
{
test[i]='*' ;
}
else if (test[i]=='+' )
{
test[i]='+' ;
}
}
cout<<test<<endl;
}
Feb 11, 2014 at 9:04pm UTC
In c++, single quotes are used to denote a character while double quotes denote strings. Only one character can be placed within a single quote
You cannot place more than one character in an index of a string. What you can do in this case is to build a new string:
1 2 3 4
...
if (test[i] == 'a' )
new_string += " 97" ;
...
Topic archived. No new replies allowed.