cin & getline

I'm using getline:

cin.getline(c.buffer, sizeof(c.buffer));

My program works fine except I'm having one problem I can't seem to figure a way around. It's a problem that's plagued me for quite some time. I've searched the net and haven't found a solution.

My problem is this: Every time I run the program the last thing that was typed is entered if I just hit return. I'd like to have it clear it all out each time. Any tips is appreciated. Thanks.
http://www.cplusplus.com/forum/articles/6046/

Edit: Your problem probably relates to not correctly initializing your variable when it's declared.
Last edited on
Ok, I'm pasting my code as is. Far as I know I've doing things right. After checking your reply, I still didn't see any solution to the problem I'm having but I could have missed something?

Header:

#include <iostream.h>
#include <vector.h>
using namespace std;

class base_game
{
public:
bool online;
vector<string> buffer;
struct base_c
{
char buffer[256];
char* token;
struct base_pp
{
string buffer;
}; base_pp pp;
}; base_c c;
struct base_itr
{
vector<string>::iterator c;
}; base_itr itr;
/* base_game functions */
void command_line(void),
execute_command(void);
}; extern base_game game;

Code:
#include "main.h"

base_game game;

int main(void)
{

while(!game.online)
{
game.command_line();
}

return 0;
}

void base_game::command_line(void)
{
printf("> ");

cin.getline(c.buffer, sizeof(c.buffer));

buffer.clear();
c.token=strtok(c.buffer, " ");
while(c.token!=NULL)
{
buffer.push_back((string)c.token);
c.token=strtok(NULL, " ");
}

if(!buffer.empty())
{
execute_command();
}
}

void base_game::execute_command(void)
{
itr.c=buffer.begin();
while(itr.c!=buffer.end())
{
printf("new word: (%s)\n", (*itr.c).c_str());
itr.c+=1;
}
}
I'm guessing your using Turbo C++?

Some concerns with your code:
- Your using <vector.h> instead of <vector>
- using <iostream.h> instead of <iostream>
- Your using a mix of C and C++ structures and IO.

While your using a C++ vector of strings. You've reverted to using character arrays and array functions for manipulation. The string class has many functions you can access so you don't need to use character arrays.

Printf should be replaced with Iostreams cout << syntax. This is because printf is highly unsecure and vulnerable to format string exploits. It's a good habbit to become familiar with the C++ IOStreams as soon as possible to help your learning.

The link in my previous reply showed you how to use getline() on a string, instead of a character buffer. This avoids a few problems to do with line length etc that are better handled.

As for your exact problem. It likely lies as I said in my previous reply.
Your problem probably relates to not correctly initializing your variable when it's declared.


char buffer[256] = {0}; // Clear it initially.
> The link in my previous reply showed you how to use getline() on a string, instead of a character buffer. This avoids a few problems to do with line length etc that are better handled.

Ah, I already understood this little part and I've already been seriously considering making the switch.

I've already tried the following:

string the_buffer;

while(true)
{
the_buffer=""; (assuming you are speaking of this)

getline(cin, the_buffer);
}

I still encounter the same problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include <string>

using namespace std;

int main() {
  string input = "";

  cout << "Enter first text:";
  while(getline(cin, input)) {
    if (input == "")
      break;

    cout << "You entered: " << input << endl;
    cout << "Enter new text:";
  }

  return 0;
}


In your case. After the cin.getline(); Check for strlen() == 0; (or 1 if it pulls the \n)
Well I've tried the last thing you suggested, no change. Initializing isn't changing anything either. At this point I'm beginning to wonder if it's a bug with getline. :/
Last edited on
Topic archived. No new replies allowed.