sorry..string problem continues

Started by
8 comments, last by Stashi 22 years, 5 months ago
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?
Advertisement
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
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)
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)
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.
i didn''t use cin.getline() i used std.getline(cin, ...) or is there no difference
http://support.microsoft.com/support/kb/articles/q240/0/15.asp?LN=EN-US&SD=g
This will explaian why your original code didn''t work properly.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Martee, you have infinite credit in my eyes!
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
nice one martee
(always anonymous)

This topic is closed to new replies.

Advertisement