input of strings in c++
Author |
Message |
halo3d
|
Posted: Sat Nov 11, 2006 8:15 pm Post subject: input of strings in c++ |
|
|
hi, i m currently on the file i/o part and then i notced something.
the cin doesnt accept strings why is that
e.g.
code: |
string str;
cin >> str; // <----- i get error here
cout << "you entered .. " << str;
cin.get();
|
the error i get is ..
no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
any way to get around that? im trying so that what ever string user enters is saved to a file.. the saving of predefined strings works but not when i try to input something as a string..
help would be appreciated ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Nov 11, 2006 8:22 pm Post subject: (No subject) |
|
|
Works fine for me. Please post all of your code. |
|
|
|
|
![](images/spacer.gif) |
halo3d
|
Posted: Sat Nov 11, 2006 8:38 pm Post subject: (No subject) |
|
|
Library.h has this...
code: |
#include <iostream>
#include <fstream>
#include <istream>
using namespace std;
void delay(){
cin.get();
}
void skip(int i){
for (int a = 0; a < i; a++){
cout << endl;
}
}
|
test.cpp, the main file, has this
code: |
#include "Library.h"
main(){
string str;
cout << "Enter something";
cin >> str; // <----- i get error here
skip(2);
cout << "you entered .. " << str;
cin.get();
}
|
the whole error is...
------ Build started: Project: test, Configuration: Release Win32 ------
Compiling...
test.cpp
test.cpp(5) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
test.cpp(6) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Build log was saved at "file://d:\C++\test\Release\BuildLog.htm"
test - 2 error(s), 0 warning(s)
im using visual .net 2003
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped |
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Sat Nov 11, 2006 9:42 pm Post subject: (No subject) |
|
|
have a
#include<string>
somewhere |
|
|
|
|
![](images/spacer.gif) |
halo3d
|
Posted: Sat Nov 11, 2006 10:03 pm Post subject: (No subject) |
|
|
omg i cant believe i keep forgetting that |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Nov 11, 2006 10:59 pm Post subject: (No subject) |
|
|
If you desire a challenge, I suggest trying to figure out why you might want:
code: | void skip(int i){
for (int a = 0; a < i; a++){
cout << endl;
}
} |
To become:
code: | class skip
{
private:
int _lines_to_skip;
public:
skip(int lines = 1);
skip(const skip& other);
skip& operator=(const skip& other);
int lines_to_skip() const;
friend ostream& operator<<(ostream& out, const skip& s);
};
skip::skip(int lines) : _lines_to_skip(lines)
{
}
skip::skip(const skip& other) : _lines_to_skip(other._lines_to_skip)
{
}
skip& skip::operator=(const skip& other)
{
_lines_to_skip = other._lines_to_skip;
}
int skip::lines_to_skip() const
{
return _lines_to_skip;
}
ostream& operator<<(ostream& out, const skip& s)
{
for (int i = 0; i < s._lines_to_skip; i++)
{
out << endl;
}
return out;
} |
Because then you can have, for instance:
code: | int main()
{
cout << skip(20);
} |
|
|
|
|
|
![](images/spacer.gif) |
|
|