pointer testing #2


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
#include<iostream>
#include<cstring>
using namespace 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?

Last edited on
1
2
3
4
5
6
7
8
        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. 


read these too:
http://www.cplusplus.com/forum/articles/6046/
http://www.cplusplus.com/reference/string/
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/ntcs/

edit: added some more suggestions
Last edited on
Topic archived. No new replies allowed.