x++ not incrementing
i am converting each character from string into ASCII and i need size of array "x" to store them in array why didnt it increments "x" after cout?
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
|
#include <iostream>
#include <string>
using namespace std;
void convertToASCII(string letter)
{
int x=0;
for (int count = 0; count < letter.size(); count++)
{
x++; // worked
char x = letter.at(count);
cout << int(x) << endl;
// x++; not worked why?
}
cout << x;
}
int main()
{
string plainText;
cout << "Enter text to convert to ASCII: ";
cin >> plainText;
convertToASCII(plainText);
return 0;
}
|
You have two variables named 'x'. One is an int, declared on line 8, the other is a char declared on line 13.
1 2 3 4 5 6 7 8 9 10 11
|
int x=0; // <- "int_x"
for (int count = 0; count < letter.size(); count++)
{
x++; // <- increments int_x
char x = letter.at(count); // <- "char_x"
cout << int(x) << endl; // <- prints char_x as an integer
// x++; <- increments char_x... NOT int_x
}
cout << x; // <- prints int_x
|
Topic archived. No new replies allowed.