Archived

This topic is now archived and is closed to further replies.

sorry..string problem continues

This topic is 5870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites