Problems with cin.getline

Hello! I'm writing a Mad Libs project, and I'm almost done! I have just a few minor issues that I'm working out. One of the biggest issues is that my cin.getline function isn't working as I expect it to, and I can't figure out why.

If I understand correctly, cin.getline will take the entire line that is entered by the user and store that as a value in an array like so:

1
2
3
4
5
6
7
8
main()
{
char name[256];
cout << "What is your name?;
cin.getline(name, 256);
cout << name << endl;
return 0;
} 


This code should print the name you entered correct? Or do I have it wrong?

In any case, here's the code I need some help with:

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
54
55
56
57
58
/**********************************************************************
* This function processes the words in the file to find which ones are
* the tokens.
***********************************************************************/
void readWords(char words[][32], char answers[])
{
   for (int i = 0; words[i]; i++) //start looping through the array
   {
      fin >> words[i];
      k++;
      if (words[i][0] == '<')//if statement for "tags" within the text file
      {
         words[i][0] = '\t';
         for (int j = 0; words[i][j]; j++)
         {
            if (words[i][j] == '>')
               words[i][j] = ':';
            if (words[i][j] == '_')
               words[i][j] = ' ';
         }
         switch (words[i][1])//Switch statement for special cases in the '<'
         {
            case '{':
               cout << "";
               break;
            case '}':
               cout << "";
               break;
            case '[':
               cout << "";
               break;
            case ']':
               cout << "";
               break;
            case '#':
               cout << "";
               break;
            default:
               words[i][1] = toupper(words[i][1]);
         }
         if (words[i][1] == '{' || words[i][1] == '}' ||
            words[i][1] == '[' || words[i][1] == ']' ||
            words[i][1] == '#')
         {
            continue;
         }
         cout << words[i] << " "; //outputting the questions for Mad Lib
         cin.getline(answers, 256); //receiving input for each tag
         cout << answers[0] << endl; //displaying each tag (for testing)

      }
      if (fin.eof())
      {
         return;
      }
   }
   fin.close();
}


The variables initialized are
1
2
char words[256][32];
char answers[256];


However, my problem is that when I try to display the values stored in answers, I can only see the first letter of the input, not the whole word at slot 0 in the array. Why is this happening?

Thank you for your time and help. I appreciate it.
Last edited on
change.

cout << name[0] << endl;

to this

cout << name << endl;

and fix your typos.
closed account (1vD3vCM9)
Also try to avoid 'using namespace std'.
If you do, you will have to put std:: before cout and cin
There are articles on why to avoid 'using namespace std'
Bdanielz,
I believe you might have me mistaken. That first bit of code was just an example, not my actual code. The one below is the code. Maybe you thought that was my problem? Anyway, I changed it like you stated, and it changed from displaying the first letter to now displaying the address. It wasn't the solution I was looking for. Also, I'm confused by what you mean by "fix your typos." Did I write something wrong in my question? If you could point it out, I'd gladly fix it. Thanks for your help!

oren drobitsky,
If I remember right, you're supposed to put using namespace std because you DON'T have to put std:: before every cout and cin. That's what I've been doing right now, and that's what I've been told in my programming classes. In any case, even though there could be a conflict in namespaces, this is for a project at school, and they require us to use the namespace std just because that's how they want the programs to run. Otherwise I would probably change it. Thanks for the advice!
So by typos: I mean the code is

fin >> somevar;

should it be ?

cin >> somevar

About the other issue. Let me ask you what is the difference between the ways I am printing the variable name, to common out (cout) ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
 
    char name[256];
    cout << "What is your name?" << endl;
    
    cin.getline(name, 256);
    cout << name << endl;
    cout << *name << endl;
    cout << &name << endl;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.