Jump to content
  • Advertisement
Sign in to follow this  
Meta Adam

Spaces in string

This topic is 5062 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

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?

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!