Skip to main content
gamedev.net
🔒 Locked

sorry..string problem continues

Started by Stashi 8 replies 350+ views
Original Post
Stashi
Stashi
ok disregard my last post for now... my real goal is to work on an AI bot program, basically a simple console program that lets you talk to an AI bot. my problem is, i want the user to be able to say anything he or she wants to. so i don''t want my input string to have a fixed length because you won''t know what the user will be typing in. so how should i go about doing this? what kind of variable, how should i input it, etc?
Stashi
Stashi
also i cant seem to get to store strings with spaces in them..
for instance, typing "hello my name is bob" only seems to store "hello" in the string.

this is my coding right now

    
#include <iostream>
#include <string>
using namespace std;
string str1;
void main()
{
cout << "Enter any text you want: ";
std::getline(cin,str1);
cout << str1 << endl;
}


that coding seems to properly store my spaces, but i have to hit enter twice for this to work. is there any way to do what i'm trying to do w/o all this crap, heh.


Edited by - Stashi on November 18, 2001 1:31:20 PM
ALCOMADUM
ALCOMADUM
instead of using a string use an array of chars ie: char str[];
just don''t specify a length so you could go

cout << "ENTER STRING";
cin >> str;

to view the string then just go:

cout << str;


strings suck don''t use em unless you have something set to say but otherwise use arrays of chars.
you can lead a horse through water, but you CAN make him drink.(he may or may not drown with this process)
Stashi
Stashi
alco, your coding of
  
#include <iostream.h>
void main()
{
char str[];
cout << "enter: ";
cin >> str;
cout << endl << str << endl;
}

gives me the error..

Compiling...
main.cpp
c:\microsoft visual studio\myprojects\str\main.cpp(4) : error C2133: ''str'' : unknown size
Error executing cl.exe.

str.exe - 1 error(s), 0 warning(s)
Krunk
Krunk
Hmmm, strange - that first example you gave (using std::string and cin.getline()) worked fine for me. I only had to press enter once. That code is exactly what I was going to suggest you use, before I realised you`ve already tried it.
Stashi
Stashi
i didn''t use cin.getline() i used std.getline(cin, ...) or is there no difference
Oluseyi
Oluseyi
Martee, you have infinite credit in my eyes!
Martee
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.