DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    3

    C++ cin/cout error

    I compile the following code, and get an error to do with the cin, cout operators. Basically I am trying to input parameters to a self made class using cin. However, the code does not recognise cin and cout for some reason??? :

    #define PI 3.14
    #define G 9.81;
    #include <io.h>
    #include <cmath>
    #include <iostream>
    using namespace std;

    int main (void);

    class pendulum
    { //data
    double time;
    int length;
    double velocity;
    int mass;
    int energy;

    //functions
    public:
    void run ();
    double calculate_time();
    double calculate_velocity();
    };

    double pendulum::calculate_time()
    { double input = length/G;
    time = 2*PI*sqrt(input);
    return time;
    };

    double pendulum::calculate_velocity()
    { double input = (2*energy)/mass;
    velocity = sqrt(input);
    return velocity;
    };

    void run ()
    { cout >> "time: ";
    cin >> time;
    cout >> "length: ";
    cin >> length;
    cout >> "velocity";
    cin >> velocity;
    cout >> "mass";
    cin >> mass;
    cout >> "energy";
    cin >> energy;
    };

    and get the following error:
    c:\documents and settings\vishal\my documents\visual studio 2005\projects\exercise 1\exercise 1\exercise_1.h(38) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
    C:\Program Files\Microsoft Visual Studio 8\VC\include\istream(1018) : see declaration of 'std::operator >>'
    c:\documents and settings\vishal\my documents\visual studio 2005\projects\exercise 1\exercise 1\exercise_1.h(38) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
    .... and it continues on with the same message for the rest of the cins and couts...
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    I don't understand why the code does not recognise the cin and cout operators when iostream is clrealy included? Any insights here woud be much appreciated. Thank you for your time.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    I dont see an error. Try explicitly calling

    cout.operator<<(variable) ; //syntax may not be 100%

    and see if that helps any.

  3. #3
    Join Date
    Aug 2003
    Posts
    313
    You have typos with the output stream operator. It should be:
    Code:
    cout << ...;
    cin >> ...;
    Hope this helps.
    ~evlich

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by evlich
    You have typos with the output stream operator. It should be:
    Code:
    cout << ...;
    cin >> ...;
    Hope this helps.
    Precisely so!

    Remember: cout support <<
    cin supports >>
    Danny Kalev

  5. #5
    Join Date
    Jul 2005
    Posts
    3
    Quote Originally Posted by evlich
    You have typos with the output stream operator. It should be:
    Code:
    cout << ...;
    cin >> ...;
    Hope this helps.
    thanks problem sorted..

  6. #6
    Join Date
    Aug 2005
    Posts
    2
    the following should work for u

    #define PI 3.14
    #define G 9.81
    #include <io.h>
    #include <cmath.h>
    #include <iostream>
    //#include <conio>
    using namespace std;


    class pendulum
    { //data
    double time;
    int length;
    double velocity;
    int mass;
    int energy;

    //functions
    public:
    double calculate_time()
    {
    double input = length/G;
    time = 2*PI*sqrt(input);
    return time;
    }

    double calculate_velocity()
    {
    double input = (2*energy)/mass;
    velocity = sqrt(input);
    return velocity;
    }
    void run()
    {
    cout<<"time: ";
    cin>>time;
    cout<<"length:";
    cin>>length;
    cout<<"velocity";
    cin>>velocity;
    cout<<"mass";
    cin >> mass;
    cout <<"energy";
    cin >>energy;
    }
    };





    void main()
    {
    pendulum p;
    p.run();
    }

  7. #7
    Join Date
    Aug 2005
    Posts
    2
    yeah the problem in ur program was u did not use
    void pendulum::run()
    {
    .................
    }
    and u were terminating the function definetion using semicolon

    the following code should help u...
    [B]#define PI 3.14
    #define G 9.81
    #include <io.h>
    #include <cmath>
    #include <iostream>
    using namespace std;

    class pendulum
    { //data
    double time;
    int length;
    double velocity;
    int mass;
    int energy;

    //functions
    public:
    void run ();
    double calculate_time();
    double calculate_velocity();
    };

    double pendulum::calculate_time()
    {
    double input = length/G;
    time = 2*PI*sqrt(input);
    return time;
    }

    double pendulum::calculate_velocity()
    { double input = (2*energy)/mass;
    velocity = sqrt(input);
    return velocity;
    }

    void pendulum::run ()
    {
    cout << "time: ";
    cin >> time;
    cout << "length: ";
    cin >> length;
    cout << "velocity";
    cin >> velocity;
    cout << "mass";
    cin >> mass;
    cout << "energy";
    cin >> energy;
    }[/B]
    void main()
    {
    pendulum p;
    p.run();
    }


    regards
    maddy!

Similar Threads

  1. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 03:53 AM
  2. Returning errors from SQL Server Stored Procs
    By Khalizan in forum VB Classic
    Replies: 1
    Last Post: 11-28-2001, 01:32 AM
  3. Re: Error handling in components called from ASP
    By James Barbetti in forum ASP.NET
    Replies: 2
    Last Post: 11-06-2001, 01:13 PM
  4. DTS/linked server error #6
    By John M. in forum Database
    Replies: 0
    Last Post: 05-17-2001, 03:01 PM
  5. Winsock32, Connect fails to connect to a peer
    By Chris.G in forum VB Classic
    Replies: 4
    Last Post: 11-13-2000, 03:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links