Spaces in string

Started by
13 comments, last by Oluseyi 19 years, 3 months ago
say I have the source:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string inventory[9];
    string item;
    cout<<"What would you like to add to the inventory?"<<endl;
    cin>>item;
    inventory[0] = item;
    cout<<inventory[0]<<endl;
    return 0;
}

I typed in "Long Sword" as what I want to add to the inventory, but when it throws it back, it only says long. So this tells me you cant have spaces in string. How can I get it so I can have spaces?
Meta AdamOne of a million noob C++ programmers.
Advertisement
std::getline or std::istream::getline.
Elaborate please?
Meta AdamOne of a million noob C++ programmers.
I think cin.getline() is used like..

cin.getline(VarToStoreEnteredStringIn, HowManyChars);
soo..
cout << "Enter whatever you want:";cin.getline(inventory, 255);


i'm pretty sure thats how it works, keep checking back though, if I'm wrong i'm sure someone will correct me..

what getline does is pulls all the characters until you get to the line break character (hence getline) and places it into a buffer you create. (there's a few other details and options, but I won't go into them for simplicity ;) example:

char buffer[255];cin.getline(buffer, 255);


would grab everything before a line break (up to 255 characters max, hence the 255)
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
I get an error.
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\HP_Owner\Desktop\Test.cpp" -o "C:\Documents and Settings\HP_Owner\Desktop\Test.exe" -I"C:\Dev-Cpp\include\c++\3.3.1" -I"C:\Dev-Cpp\include\c++\3.3.1\mingw32" -I"C:\Dev-Cpp\include\c++\3.3.1\backward" -I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.3.1\include" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:/Documents and Settings/HP_Owner/Desktop/Test.cpp: In function `int main()':

C:/Documents and Settings/HP_Owner/Desktop/Test.cpp:8: error: no matching
function for call to `std::basic_istream<char, std::char_traits<char> >::
getline(std::string&, int)'
C:/Dev-Cpp/include/c++/3.3.1/bits/istream.tcc:664: error: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =
std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.3.1/istream:401: error:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int) [with _CharT = char, _Traits =
std::char_traits<char>]
C:/Documents and Settings/HP_Owner/Desktop/Test.cpp:9: error: syntax error
before `[' token

Execution terminated
correction errors. hehe
Meta AdamOne of a million noob C++ programmers.
can you show us the source?
Now its different, ive changed a little, but still have errors.
[source lang = "cpp]#include <iostream>#include <string>using namespace std;int main() {    string inventory[9];    for(int i=0; i<=9; i++)    {    cout<<"What would you like to add to the inventory?"<<endl;    cin.getline(inventory, 20);    }    for(int x=0; x<9; x++)    {    cout<<inventory[x]<<endl;    }    return 0;}

and then the errors are
Executing  g++.exe...g++.exe "C:\Documents and Settings\HP_Owner\Desktop\Test.cpp" -o "C:\Documents and Settings\HP_Owner\Desktop\Test.exe"    -I"C:\Dev-Cpp\include\c++\3.3.1"  -I"C:\Dev-Cpp\include\c++\3.3.1\mingw32"  -I"C:\Dev-Cpp\include\c++\3.3.1\backward"  -I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.3.1\include"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" C:/Documents and Settings/HP_Owner/Desktop/Test.cpp: In function `int main()':C:/Documents and Settings/HP_Owner/Desktop/Test.cpp:9: error: no matching    function for call to `std::basic_istream<char, std::char_traits<char> >::   getline(std::string&, int)'C:/Dev-Cpp/include/c++/3.3.1/bits/istream.tcc:664: error: candidates are:    std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,    _Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =    std::char_traits<char>]C:/Dev-Cpp/include/c++/3.3.1/istream:401: error:                    std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,    _Traits>::getline(_CharT*, int) [with _CharT = char, _Traits =    std::char_traits<char>]Execution terminated
Meta AdamOne of a million noob C++ programmers.
heres how I made it..i'm sure its not the most effective..but it works

#include <iostream>using namespace std;int main(){	string inventory[9];		for (int a=0; a<=9; a++ )	{		//make a buffer		char buffer[255];		//ask for the item		cout << "enter inventory item " << a <<": ";		//collect whatever the user entered..up to 255 chars		cin.getline(buffer, 255);		//set the buffer equal to the string		inventory[a] = buffer;		//display the new inventory item		cout << inventory[a].c_str();	}	return 0;}
as above, its probably a problem putting them into a string, put it into an array if chars first, and then put that into the string.
www.stickskate.com -> check it out, some gnarly stick skating movies

This topic is closed to new replies.

Advertisement