-
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.
-
I dont see an error. Try explicitly calling
cout.operator<<(variable) ; //syntax may not be 100%
and see if that helps any.
-
You have typos with the output stream operator. It should be:
Code:
cout << ...;
cin >> ...;
Hope this helps.
~evlich
-
 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
-
 Originally Posted by evlich
You have typos with the output stream operator. It should be:
Code:
cout << ...;
cin >> ...;
Hope this helps.
thanks problem sorted..
-
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();
}
-
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
-
By Martin in forum VB Classic
Replies: 22
Last Post: 12-03-2001, 03:53 AM
-
By Khalizan in forum VB Classic
Replies: 1
Last Post: 11-28-2001, 01:32 AM
-
By James Barbetti in forum ASP.NET
Replies: 2
Last Post: 11-06-2001, 01:13 PM
-
By John M. in forum Database
Replies: 0
Last Post: 05-17-2001, 03:01 PM
-
By Chris.G in forum VB Classic
Replies: 4
Last Post: 11-13-2000, 03:16 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks