#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;
}
Spaces in string
say I have the source:
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?
I think cin.getline() is used like..
cin.getline(VarToStoreEnteredStringIn, HowManyChars);
soo..
i'm pretty sure thats how it works, keep checking back though, if I'm wrong i'm sure someone will correct me..
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:
would grab everything before a line break (up to 255 characters max, hence the 255)
char buffer[255];cin.getline(buffer, 255);
would grab everything before a line break (up to 255 characters max, hence the 255)
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
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
Now its different, ive changed a little, but still have errors.
and then the errors are
[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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement