GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Feb-2005, 20:24
SpudNuts
 
Posts: n/a

Fraction program


I wrote this to perform arithmetic and relational operations on fractions but getting a few errors and was hoping someone could clue me in.

fraction.h

CPP / C++ / C Code:
#ifndef Fraction
#define Fraction

class fraction
{
    public:
        fraction();
		void get();
		void display();
		double evaluate();
		friend fraction operator+ (fraction, fraction);
	private:
		int numerator;
        int denominator;
};
#endif

fraction.cpp

CPP / C++ / C Code:
#include <iostream>
#include <cmath>

#include "fraction.h"

void main ()
{
	fraction x,y,z;
	double i;
	x.get();
	x.display();
	i = x.evaluate();
	cout << i << endl;
	y.get();
	y.display();
	z = x + y;
	cout << "The sum of " ;
        x.display();
	cout << " and ";
	y.display();
	cout <<  " is " ;
	z.display();
}

fractionmain.cpp

CPP / C++ / C Code:
#include <iostream>
#include <cmath>

#include "fraction.h"

	fraction::fraction()
	{
        numerator=0;
        denominator=1;
	}
	void fraction::get()
	{
        cout << "numerator?" <<endl;
		cin >> numerator;
		cout << "denominator?"  << endl;
		cin >> denominator;
	}
	void fraction::display()
	{
		cout << numerator << "/" << denominator << endl;
	}
	double fraction::evaluate()
	{
		double temp;
		temp = (double) numerator / (double)denominator;
        return temp;
	}
	fraction operator+ (fraction f1, fraction f2)
	{
		fraction sum;
		sum.numerator = f1.numerator*f2.denominator + f2.numerator*f1.denominator;
		sum.denominator = f1.denominator*f2.denominator;
		return sum;
	}

Errors:

fractionmain.cpp
error C2065: 'cout' : undeclared identifier
error C2065: 'endl' : undeclared identifier
error C2065: 'cin' : undeclared identifier
error C3861: 'cout': identifier not found, even with argument-dependent lookup
error C3861: 'endl': identifier not found, even with argument-dependent lookup
error C3861: 'cin': identifier not found, even with argument-dependent lookup
error C3861: 'cout': identifier not found, even with argument-dependent lookup
error C3861: 'endl': identifier not found, even with argument-dependent lookup

fraction.cpp
error C2065: 'cout' : undeclared identifier
error C2065: 'endl' : undeclared identifier
error C3861: 'cout': identifier not found, even with argument-dependent lookup
error C3861: 'cout': identifier not found, even with argument-dependent lookup
error C3861: 'cout': identifier not found, even with argument-dependent lookup

It's obvious that the errors are with cout, cin, and endl
but what did I miss?

Thanks
Spuds
  #2  
Old 16-Feb-2005, 22:02
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
The identifiers cout, cin, and endl are all defined in the std namespace. To use them, either
a) insert the statement "using namespace std;" at the top of your cpp files, or
b) use the identifiers std::cout, std::cin, and std::endl

Matthew
  #3  
Old 17-Feb-2005, 19:05
SpudNuts
 
Posts: n/a
Quote:
Originally Posted by QED
The identifiers cout, cin, and endl are all defined in the std namespace. To use them, either
a) insert the statement "using namespace std;" at the top of your cpp files, or
b) use the identifiers std::cout, std::cin, and std::endl

Matthew

Worked like a champ, all my exercises have that statement. I just forgot about it, at least now I know what happens without it.

Thanks
  #4  
Old 21-Sep-2006, 05:57
Javed Javed is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Javed is on a distinguished road
Exclamation

Re: Fraction program



I also faced this problem
error C2065: 'cout' : undeclared identifier

and I TRIED WITH NAMESPACE But it did not work. The real problem was the

#include <StdAfx.h>
was not the first line of my .cpp file.
Do remember anything before this header is not compiled and hance #include<iostream.h> was not getting compiled.

  #5  
Old 22-Sep-2006, 09:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Fraction program


Quote:
Originally Posted by Javed

I also faced this problem
error C2065: 'cout' : undeclared identifier

and I TRIED WITH NAMESPACE But it did not work. The real problem was the

#include <StdAfx.h>
was not the first line of my .cpp file.
Do remember anything before this header is not compiled and hance #include<iostream.h> was not getting compiled.

The
CPP / C++ / C Code:
#include <StdAfx.h>
line should not even be in the file. It's not needed -- and it's not portable.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 25-Sep-2006, 02:23
Javed Javed is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Javed is on a distinguished road

Re: Fraction program


so what should I do. if I am havind
#include "StdAfx.h"
and I need to port the program
  #7  
Old 25-Sep-2006, 04:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Fraction program


Quote:
Originally Posted by Javed
so what should I do. if I am havind
#include "StdAfx.h"
and I need to port the program
This doesn't give you a clue?
Quote:
Originally Posted by WaltP
The
CPP / C++ / C Code:
#include <StdAfx.h>
line should not even be in the file. It's not needed -- and it's not portable.
Delete the line...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
Adding icon to my program? leitz C++ Forum 0 15-Nov-2004 13:47
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.