Well you can read this why gets is bad
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351
Well you can read this why gets is bad
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351
I wonder when I get a job if I can post my work and ask people to fill in the blanks for me. Moral of the story what do you need help with not I need help heres my code fill it in.
I am not sure what a viva is but it sounds like a test or quiz or homework, so http://www.daniweb.com/techtalkforums/announcement8-2.html.
Maybe if you explain what you were thinking the answers are we will be more than glad to help you out.
Ummm. Do your own work
Write a "Hello World" program in 'C' without using a semicolon
Who comes up with these brilliant ideas?
Well the linking error means it finds the prototype of the function but doesn't find the actual defination of the function. I am not sure why it does that since I would not be an VC++ expert. Maybe look up how the pragma comment has to be.
Exactly what he said I mean you can overload the << to do something like this it is ugly as sin though I think.
#include <iostream>
#include <ostream>
#include <istream>
class Person
{
public:
Person()
{
age = 20;
}
std::ostream &operator<<(std::ostream &out)
{
out<<age<<std::endl;
return out;
}
private:
int age;
};
int main()
{
Person a;
a<<std::cout;
std::cin.get();
return 0;
}
Think of writting the overloaded << as an extension to the C++ ostream operators.
Well you are probably getting that error because you are not making it a friend of your class it should look similar to this
#include <iostream>
#include <ostream>
#include <istream>
#include <string>
class Person
{
public:
Person();
const std::string &getName() const;
const int &getHealthLevel() const;
const int &getHappinessLevel() const;
const int &getSleepLevel() const;
const unsigned int &getMoneyLevel() const;
const unsigned int &getAge() const;
friend std::ostream &operator<<(std::ostream &out,const Person &p);
private:
std::string name;
int health;
int happiness;
int sleep;
unsigned int money;
unsigned int age;
};
Person::Person()
{
name = "Brian";
health = 10;
happiness = -5;
//Good lord I am tired
sleep = -60000;
//Shocker huh
money = 0;
age = 20;
}
const std::string &Person::getName() const
{
return name;
}
const int &Person::getHealthLevel() const
{
return health;
}
const int &Person::getSleepLevel() const
{
return sleep;
}
const unsigned int &Person::getMoneyLevel() const
{
return money;
}
const unsigned int &Person::getAge() const
{
return age;
}
std::ostream &operator<<(std::ostream &out, const Person &p)
{
out<<p.getName()<<std::endl;
out<<p.getHealthLevel()<<std::endl;
out<<p.getSleepLevel()<<std::endl;
out<<p.getMoneyLevel()<<std::endl;
out<<p.getAge()<<std::endl;
return out;
}
int main()
{
Person mainP;
std::cout<<mainP;
std::cin.get();
return 0;
}
Well. You should change the Person::function(). To p.function().
std::ostream &operator<<(std::ostream &stream,const Person &p)
Narue,
What are you talking about professors know everything.......
atoi for converting a char * to an int C or Or stringstreams for C++
The reason RAND_MAX exists is for the maximum digit the rand() function can return. If you want a quick and dirty random number just use the modulus.
Like this:
#include <stdlib.h>
/*Or <cstdlib> for C++*/
int main()
{
/*Will return a number between 0 - 9*/
int number = rand() % 10;
return 0;
}
Welcome to the forums.
1. When you have a problem(s). Ask a question releating to that problem.
2. Post any realated code with the problem.
3. Wait until someone like me, who is a genius, to help you with your problem ;).
Enjoy your stay at the Daniweb C And C++ forums and make sure you keep your arms and legs inside while you are here
operator ++ and -- with no arguments return a reference to an object and not an object itself. bman you should have known better.
Not really since I have never overloaded the ++ and -- in my travels. Thanks for the information :).
I am not sure what you wanted this compiles and runs so here added comments for some info.
#include <iostream>//This is the proper header.
using namespace std;//Put everything in the std namespace
//not always the best idea but the easiest
class Positive_Fraction
{
public:
Positive_Fraction();
Positive_Fraction(int,int);
void Print(); // added 9-19
int Get(); // added 9-19
int Assign(int s, int t);
Positive_Fraction operator++();
Positive_Fraction operator--();
bool operator==(Positive_Fraction f2);
friend ostream& operator<<(ostream& c, Positive_Fraction f1);
friend istream& operator>>(istream& c, Positive_Fraction& f1);
private:
int top;
int bottom;
void Reduce();
};
Positive_Fraction :: Positive_Fraction()
{
top = 0;
bottom = 1;
}
Positive_Fraction :: Positive_Fraction(int a, int b)
{
Assign(a,b);
}
void Positive_Fraction :: Print()
{
if (top == 0)
cout << 0 << endl;
else
{
if (top / bottom != 0)
{
cout << top / bottom;
if (top % bottom != 0)
cout << " and ";
}
if (top%bottom != 0)
cout << top % bottom << '/' << bottom;
}
}
int Positive_Fraction :: Get()
{
char ch;
int s,t;
cin >> s >> ch >> t;
if (ch != '/')
{
cout << "Positive_Fraction not assigned!\n";
return 1;
}
return Assign(s,t);
}
int Positive_Fraction :: Assign(int s, int t)
{
if (t == 0)
{
cout << "Positive_Fraction not assigned!\n";
return 1;
}
else
{
top = s;
bottom = t;
Reduce();
return 0;
}
}
ostream& operator<<(ostream& co, Positive_Fraction f1)
{
if (f1.top == 0) co << 0;
if (f1.top >= f1.bottom)
{
co << …
You would need to change your [] operator to return a reference and then you could use that as you would.
So what exactly is your question? Do you need to know how to average, find the second highest, print out the color assiocated with the number?
Ussually when you are writting the + operator it is not really a member function of a class here is how I would implement it
#include <iostream>
#include <ostream>
class someClass
{
public:
int getX();
void setX(const int &toSet);
someClass &operator=(const someClass &toSet);
someClass &operator+=(const someClass &toAppend);
friend someClass operator+(const someClass &a, const someClass &b);
private:
int x;
};
int someClass::getX()
{
return this->x;
}
void someClass::setX(const int &toSet)
{
this->x = toSet;
}
someClass &someClass::operator=(const someClass &toSet)
{
this->x = toSet.x;
return *this;
}
someClass &someClass::operator+=(const someClass &toAppend)
{
this->x += toAppend.x;
return *this;
}
someClass operator +(const someClass &a, const someClass &b)
{
someClass temp = a;
temp+= b;
return temp;
}
int main()
{
someClass a;
someClass b;
someClass c;
a.setX(10);
b = a;
c = a + b;
std::cout<<"a's x = "<<a.getX()<<std::endl;
std::cout<<"b's x = "<<b.getX()<<std::endl;
std::cout<<"c's x = "<<c.getX()<<std::endl;
std::cin.get();
return 0;
}
yaa you an do that but it will be unncessary burden for you
i'll suggest you to go for turbo c++ compiler in case you want use graphics
it fully supports it
Gooog. Don't recommend that compiler. Go and get a graphics lib like SDL or Allegro you can draw shapes with them.
What? You need to show some code so we can see what your problem is.
umm what's with the unneeded goto is while(1) not good enough anymore?
Here:
while(1)
{
//your code here
}
Couldn't you just do something like this? Remeber if this sucks I am not good at the API ;)
#include <windows.h>
#include <iostream>
int main()
{
char buf[30] = {'\0'}; //Null terminate
LPVOID lpMsgBuf;
DWORD byteWritten = 0;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
BOOL ReadFileReturn;
HANDLE hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr
ReadFileReturn = ReadFile(hFile,buf,30,&byteWritten,NULL);
if(ReadFileReturn)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
WriteFile(hStdOut,buf,sizeof buf,NULL,NULL);
}
else
{
WriteFile(hStdOut,"It Failed",sizeof "It Failed",NULL,NULL);
}
std::cin.get();
return 0;
}
;
What do you mean? You didn't finish your thought there.
Code tags please.
>what are the member functions here of class Record that can access private data of class Record.
Any member function of it own class can access the private data of its class.
>and which member functions of class Record can access private data of class Music.
None of record's member functions can access music's private data.
>and if there is a external routine that has an object instance of class Record, what member functions of class Record can it access.
Any functions that are public in Record.
>also if a new class newRecord is derived from class Record, what member functions in Record can newRecord access
Ussually the public ones.
>also does anybody know why compiler says it is dangerous to convert base class pointer into a derived class pointer, whats dangerous about it.
I am not sure.
Here you go.
if(upkeypressed)
{
yPos--;
}
if(downkeypressed)
{
yPos++;
}
Post questions and if he is intrested he will answer or others will if they are.
Here we go.
#include<conio.h> //Non standard header do you really "need" it?
//This is the way the headers are no .h's
#include<cstdio>
#include<iostream>
#include<string>
//Got to put it in the std namespace
using namespace std;
//I would recommend some minor style changes.
class link_stack
{
struct node
{
int id;
char name[10];
node *next;
};
node *top,*x,*ptr;
public:
link_stack()
{
top = x = ptr =NULL;
}
void push()
{
x = new node;
cout << "Enter an ID number and name: ";
cin >> x->id >> x->name;
x->next = top;
top = x;
}
int pop(char n[])
{
int result;
if(top==NULL)
{
cout<<"\nStack is Empty";
}
else
{
result = top->id;
strcpy(n, top->name);
x = top;
top = top->next;
delete x;
return result;
}
}
void empty()//What is this function really doing?
{
char name[10];
//while(!obj.empty()) Where is obj declared?
//{
// cout << pop(name) << ": " << name << endl;
//}
}
};
int main()//It is int main not void main!!!!!!!!!!!!!!!!!!!!!
{
link_stack obj;
int choice;
do
{
cout << "\n ----------MENU---------- \n";
cout << "1.Push\n"
<< "2.Pop\n"
<< "3.Exit";
cout << "\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
obj.push();
break;
case 2:
//obj.pop(); Is this how you defined pop?
obj.empty();
break;
case 3:
cout << endl;
break;
}
}
while (choice != 3);
getch(); //Why not cin.get()?
//cin.ignore();
//cin.get();
return 0; //Need to return a value from main.
}
I would recommend that you don't use a system("pause").
There is no way with cin>> because the input is buffered.
I think VS 6.0 automatically places a pause after your program has executed.
It is all about the const :)
#ifndef REF_H
#define REF_H
template<typename T> class ref;
template<typename T1, typename T2>
ref<T1> ref_cast(ref<T2>& r2)
{
return ref<T1>(static_cast<T1>(r2.c_ptr()), r2.num_ref()+1);
}
template<typename T>
class ref
{
private :
T* _ptr;
unsigned long *_ref_count;
public :
inline explicit ref(T* ptr) : _ptr(ptr), _ref_count(new unsigned long)
{
*_ref_count=1;
}
inline ref() : _ptr(0), _ref_count(new unsigned long)
{
*_ref_count=0;
}
inline ref(const ref<T>& copied)
{
_ref_count=copied._ref_count;
_ptr=copied._ptr;
(*_ref_count)++;
}
inline ~ref()
{
*_ref_count-=*_ref_count?1:0;
if(*_ref_count == 0)
{
delete _ref_count;
delete _ptr;
}
}
inline T* operator->()
{
return _ptr;
}
inline bool operator!()
{
return _ptr==0;
}
inline bool operator==(ref<T>& r1)
{
return *((T*)_ptr)==*((T*)r1._ptr);
}
inline bool operator!=(ref<T>& r1)
{
return *((T*)_ptr)!=*((T*)r1._ptr);
}
//removed my other suggestion because I am dumb and changed your original to this
const inline ref<T>& operator=(ref<T>& r1)
{
*_ref_count-=*_ref_count?1:0;
_ref_count=r1._ref_count;
_ptr=r1._ptr;
(*_ref_count)++;
return *this;
}
//more goodies
const inline ref<T>& operator=(const ref<T>& r1)
{
*_ref_count-=*_ref_count?1:0;
_ref_count=r1._ref_count;
_ptr=r1._ptr;
(*_ref_count)++;
return *this;
}
inline T* c_ptr()
{
return _ptr;
}
inline unsigned long num_ref()
{
return *_ref_count;
}
};
#endif//REF_H
#include "ref-1.h"
class Foobar
{
public:
int x;
};
ref<Foobar> sample_manipulation(ref<Foobar> in)
{
ref<Foobar> f = in;
ref<Foobar> f2;
f2 = f;
return f;
}
int main(void)
{
ref<Foobar> *f1 = new ref<Foobar>;
ref<Foobar> f2;
f2 = sample_manipulation(*f1);
return 0;
}
Oh and I hate _ before data names it annoys me ;)
Well when I assign one class to another I ussually just set the data from the one equal to the other via a void operator eg.
#include <iostream>
class foobar
{
public:
void operator =(const foobar &a)
{
this->x = a.x;
}
void setX(const int &x)
{
this->x = x;
}
const int& printX() const
{
return this->x;
}
private:
int x;
};
int main()
{
foobar f1;
foobar f2;
f1.setX(10);
f2 = f1;
std::cout<<f2.printX()<<std::endl;
std::cin.get();
}
Yes. I believe it is beacuse you defined you operator = with just a reference and not a value. Which the sample_manipulation returns.
#ifndef REF_H
#define REF_H
template<typename T> class ref;
template<typename T1, typename T2>
ref<T1> ref_cast(ref<T2>& r2)
{
return ref<T1>(static_cast<T1>(r2.c_ptr()), r2.num_ref()+1);
}
template<typename T>
class ref
{
private :
T* _ptr;
unsigned long *_ref_count;
public :
inline explicit ref(T* ptr) : _ptr(ptr), _ref_count(new unsigned long)
{
*_ref_count=1;
}
inline ref() : _ptr(0), _ref_count(new unsigned long)
{
*_ref_count=0;
}
inline ref(const ref<T>& copied)
{
_ref_count=copied._ref_count;
_ptr=copied._ptr;
(*_ref_count)++;
}
inline ~ref()
{
*_ref_count-=*_ref_count?1:0;
if(*_ref_count == 0)
{
delete _ref_count;
delete _ptr;
}
}
inline T* operator->()
{
return _ptr;
}
inline bool operator!()
{
return _ptr==0;
}
inline bool operator==(ref<T>& r1)
{
return *((T*)_ptr)==*((T*)r1._ptr);
}
inline bool operator!=(ref<T>& r1)
{
return *((T*)_ptr)!=*((T*)r1._ptr);
}
inline ref<T>& operator=(ref<T>& r1)
{
*_ref_count-=*_ref_count?1:0;
_ref_count=r1._ref_count;
_ptr=r1._ptr;
(*_ref_count)++;
return *this;
}
//added this
inline ref<T>& operator=(ref<T> r1)
{
*_ref_count-=*_ref_count?1:0;
_ref_count=r1._ref_count;
_ptr=r1._ptr;
(*_ref_count)++;
return *this;
}
inline T* c_ptr()
{
return _ptr;
}
inline unsigned long num_ref()
{
return *_ref_count;
}
};
#endif//REF_H
#include "ref-1.h"
class Foobar
{
public:
int x;
};
ref<Foobar> sample_manipulation(ref<Foobar> in)
{
ref<Foobar> f=in;
//....
return f;
}
int main(void)
{
ref<Foobar> *f1 = new ref<Foobar>;
ref<Foobar> f2;
f2 = sample_manipulation(*f1);
return 0;
}
Or you could be smart and not use a system command.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385
Why are you dynamically allocating total why not just use a regular int?
It because you are setting the area at the begging to a garbage value if you want to update the area set it to height * width after the values are inputted
I just wrote this up real quick does this compile on your VC++. It works for me on Dev-C++(MinGW(GCC)) :)
#ifndef tester_h
#define tester_h
#include <map>
template<class K, class V> class tester
{
public:
std::map<K,V> myMap;
void addMap(std::map<K,V> mapToAdd);
};
#endif
#ifndef tester_hpp
#define tester_hpp
#include <map>
#include "tester.h"
template<class K, class V> void tester<K,V>::addMap(std::map<K,V> mapToAdd)
{
myMap.insert( mapToAdd.begin(), mapToAdd.end() );
}
#endif
Are you trying to define that function outside of the header? That ussually can cause problems.
Code tags please. http://daniweb.com/techtalkforums/announcement8-3.html.
Also it is int main not void main.
There is no way to do that using just good ol C++. You would have to go into system dependent console stuff. If you are using windows check out this site for a look into how to do some window console specific stuff.
http://www.adrianxw.dk/SoftwareSite/Consoles/Consoles1.html
Good?
That is a very broad question. 3D programming is a combo of knowing your the language, knowing some good math and alot of patience. This is not from personal experiance just from looking at some 3d stuff.
Here is the best openGL site as far as I know
http://nehe.gamedev.net/