Char to array

Note: It does partly work
The char "string" contains an array of letter (does work), for example a road name - it is able to contain it, but the chars "name" and "road" contain only one letter/number each (partly work) - but I require an array of chars, any way to help?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream.h>
#include <windows.h>
#include <conio.h>
char* getstring();

int main()
{
    char string[50];
    system("color E");
    cout << "Enter your name: ";
    strcpy(string,getstring());
    char name = *string;
    system("cls");
    cout << "\n";
    cout << "Enter housing: ";
    strcpy(string,getstring());
    char road = *string;
    system("cls");
    cout << "\nYour name is: " << name << endl;
    cout << "Your housing: " << road << endl;
    system("pause");
    return 0;
}

char* getstring()
{
      char ch, str[50];
      int count = 0;
    do
    {
        ch=getch();
        if(ch == '\b')
        {
              if(count > 0)
              {
                       count--;
                       putch('\b');
                       putch(' ');
                       putch('\b');
              }
        }
    else
    {
        putch(ch);
        str[count]=ch;
        count++;
     }
    }while(ch != '\r');
    str[count-1]='\0';
    return str;
}
Last edited on
Your function getstring is attempting to return a pointer to a local variable. That is an error, because the local variable str no longer exists after the function has ended. (The memory where it resided is re-used for other purposes).

Instead, pass a pointer to a string defined in the calling function.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

void getstring(char *);

int main()
{
    char name[50];
    char road[50];
    
    system("color E");
    cout << "Enter your name: ";
    getstring(name);
    //system("cls");
    cout << "\n";
    cout << "Enter housing: ";
    getstring(road);
    //system("cls");
    cout << "\nYour name is: " << name << endl;
    cout << "Your housing: " << road << endl;
    system("pause");
    return 0;
}

void getstring(char * str)
{
    char ch;
    int count = 0;
    do
    {
        ch=getch();
        if (ch == '\b')
        {
            if (count > 0)
            {
                count--;
                putch('\b');
                putch(' ');
                putch('\b');
            }
        }
        else
        {
            putch(ch);
            str[count]=ch;
            count++;
        }
    } while(ch != '\r');
    str[count-1]='\0';
}
I still get the exact same problem, I use "gacsam" for both the "name" and "road", yet in return I get only "g", unlike the supposed "gacsam".
What @Chevril said.

Also, when you do char name = *string; You are a defining a char and initialising it with the first letter of string, i.e. string[0].

I imagine what you wanted to do was char *name = string; which is also a bad idea, because you still overwrite string on subsequent calls to strcpy, and you'd just end up printing out the same value (road) for road and name.
I still get the exact same problem
because you didn't use the code which I posted.
1
2
3
4
    char name[50];
    char road[50];
    getstring(name);
    getstring(road);
I apologise, my program was going weird and not noticing changes made o.O

Thank you very much :)
No problem. My fault too for not explaining it properly the first time.
Topic archived. No new replies allowed.