Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 input of strings in c++
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
halo3d




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Nov 11, 2006 8:22 pm   Post subject: (No subject)

Works fine for me. Please post all of your code.
halo3d




PostPosted: 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
bugzpodder




PostPosted: Sat Nov 11, 2006 9:42 pm   Post subject: (No subject)

have a
#include<string>

somewhere
halo3d




PostPosted: Sat Nov 11, 2006 10:03 pm   Post subject: (No subject)

omg i cant believe i keep forgetting that
wtd




PostPosted: 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);
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: