How to capture a full sentence?

Pages: 12
How do I get my program to recognize multiple words (including the space)?

I have tried cin >> userinput;

But when tested it shows only the first word.

Any help is appreciated.
1
2
char str[80];    // create buffer (1 extra for null char)
cin.get(str, 79);    // read up to 79 chars and place in str 


You could also dynamically allocate the array to minimise memory usage. I don't know how efficient or inefficient or useful this is, maybe someone else could comment?

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

int main()
{
    char *str;
    char buf[80];
    cin.get(buf, 79);
    str = new char[strlen(buf) + 1];
    strcpy(str, buf);
    delete &buf;
    
    return 0;
}
Line 12 is a memory corruption.

1
2
3
4
5
6
7
8
9
#include <string>
#include <iostream>

int main() {
    std::string str;
    getline( std::cin, str );
    std::cout << "You entered: '" << str << "'" << std::endl;
    return 0;
}

I tried that jsmith its not letting me input anything
Chewbob, you're trying to delete a memory address? I recommend deleting the pointer or using a hammer.

Hunter, jsmiths code should work. Try adding a cin.ignore() after the getline().
Alright, I tested it again and its not getting anything. I did the same test

cout << "i typed " << defintionPtwo << " ";

and it outputs

i typed
Last edited on
To get it to recognize full sentence, use cin.get()

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
  string blah;
  cout<<"Enter a sentence. Blah\n";
  cin.get(cin, blah);
  cin.ignore();
}


Simple as that; don't need to use any dynamic memory or allocating stuff.
Hey warrior,

i get an error saying cin.get is invalid
Oh right right, I forgot it's not cin.get for input, it's getline

1
2
3
4
5
6
7
8
9
10
11

#include <iostream>
using namespace std;

int main()
{
  string blah;
  cout<<"Enter a sentence. Blah!!\n";
  getline(cin, blah);
  cin.ignore();
}

Okay, that let it compile but I tested this

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

int main()
{
  string blah;
  cout<<"Enter a sentence. Blah!!\n";
  getline(cin, blah);
  cout<<"youtry" << blah << " ";
  cin.ignore();
  
}


and its not showing what i type
You just have to add something to make the program stop and not exit out after it shows the input. I use cin.get(); which waits for the user to press enter.

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

int main()
{
  string blah;
  cout<<"Enter a sentence. Blah!!\n";
  getline(cin, blah);
  cout<<"youtry" << blah << " ";
  cin.get();
  cin.ignore();
 }
erm... correct me if i'm wrong, but

Why don't you just do this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main() {
    string var1;
    cout<<"Please type something into the console."<<endl;
    cin>>var1;
    cin.ignore();
    return 0;
}
?
That's the way I learned to allow user input, and it's the simplest, too(for me).
Okay, that works, but I put it into my if statement and its not getting it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string defintionPtwo;
        ClearScreen();
        cout << "Form of government in which citizens rule and make laws directly rather than    through representatives is... ";
        getline(std::cin, defintionPtwo);
        
            if (defintionPtwo == "Direct Democracy" || defintionPtwo == "direct democracy")
            {
            cout << "you typed" << defintionPtwo << " ";
                 int seconds = 1;
                      #ifdef WIN32
                 Sleep(seconds*3000);
                      #else
                 sleep(seconds);
                      #endif
            }
            else (defintionPtwo != "Direct Democracy" || defintionPtwo != "direct democracy");
            {
            cout << "I just typed " << defintionPtwo << " ";
            cin.get();
            cin.ignore();
            }


What its supposed to do is say a definition and then the user gives the word (in this case its direct democracy) and then say if its wrong or right.

To answer your question cdh473 cin>> variable; only gets one word. In this case im trying to get two words separated by a whitespace (space0.
Last edited on
Hold on, I'll make one and see if it works then compare it to yours.
Mine got my input.

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

int main()
{
  string definitionPtwo;

  cout<<"Form of government in which citizens rule and make laws directly rather than    through representatives is... ";
  getline(cin, definitionPtwo);

  if (definitionPtwo == "Direct Democracy" || definitionPtwo == "direct democracy")
  {
    cout<<"\nYou just typed: ";
    cout<< definitionPtwo;
  }
  else if (definitionPtwo != "Direct Democracy" || definitionPtwo != "direct democracy")
  {
    cout<<"I just typed ";
    cout<< definitionPtwo;
  }
 cin.get();
}


See what's different with mine and compare it with yours to see what may be the problem.
This is weird. I just copied your code over mine (in the actual program) and its still not getting it. It goes straight to saying I just typed.

What I'm going to do is try putting it all into a switch. Maybe something is wrong with the ifs.
I don't understand, hunter. What is your exact problem using getline?

And cin will not work because it stops reading after a space is found.
There's one or more trailing newlines in the input buffer from a previous std::cin >>.
std::cin.ignore(INT_MAX,'\n'); should solve it.
Sorry if I'm hijacking the thread, but with the example I posted above:

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

int main()
{
    char *str;
    char buf[80];
    cin.get(buf, 79);
    str = new char[strlen(buf) + 1];
    strcpy(str, buf);
    delete [] &buf;
    
    return 0;
}


If I change line 12 to delete [] &buf which is what I meant to put is there still a problem with it?
If I change line 12 to delete [] &buf which is what I meant to put is there still a problem with it?


Yes!
1: buf is not dynamically allocated, it's allocated on the stack =>> you don't have to manually delete[] it.
2: altough str IS dynamically allocated which you don't delete[] anywhere =>> memory leak.
3. Even if bufwas dynamically allocated like char *buf = new char[80];, then you wanna release the memory like this : delete [] buf;, without the & operator
Pages: 12