#include<iostream>
#include<cstring>
usingnamespace std;
int main(void)
{
char temp;
char *name = &temp;
cout<<"input something";
cin>>temp;
// char *name = temp;
short len = strlen (name) + 1, x = -1; //determine the size of "name"
short size = len;
char backward [5]; //create temp char array (not char*)
do backward [x++] = name [--len]; //copy letter by leter from back to ahead
while (len != -1); // do it until end of name
}
the first code sucessfully run but it display nothing, and what if i want the user to input the name instead of me hard coding it what's the method?
char temp; // this is a single character variable, it can only store 'a', 'A' etc.
//alternate: char temp[25] etc.
char *name = &temp; // name pointed to temp
//now char* name = temp;
....
cin>>temp; //only reads the first character into temp!
//use cin.getline(temp, 25);
//strlen should give the correct result
what if i want the user to input the name instead of me hard coding it what's the method?
1 2 3
//include <string> and <iostream>
std::string userName;
getline(std::cin, userName); //in case teh name contain spaces etc.