mrnutty 761 Senior Poster
int cnt = 0;

for infinite loop
{
   //do stuff
   if(//do stuff is what i need) // then break;
  else cnt++;

  //if cnt is above the limit then break;
}
mrnutty 761 Senior Poster

sizeof(array)/sizeof(double) ?

mrnutty 761 Senior Poster

opengl is a graphics library while glut is not.
You use glut with opengl to handle graphics. glut handles with mostly
the input. It could create the graphics window, handle key input and some other fancy stuff. You use opengl to draw primitives onto the window
that glut creates.

mrnutty 761 Senior Poster

goggle Separating axis theorem.

jasimp commented: Love your avatar pic. Nice suggestion too :) +11
mrnutty 761 Senior Poster

call your stack something like Stack. Because the standard c++ has
a stack class.

mrnutty 761 Senior Poster

Post the whole code.

mrnutty 761 Senior Poster

template class and its definition has to be on the same file, unless
your compiler supports the keyword export.


and you need a forward declaration :

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
template<typename T>
class stack;

template <typename T>
class ListNode
{
public:
  ListNode();	
private:
    T value;
    ListNode<T> *next;
friend class  stack<T>;
    	
};

template <class T>
class stack
{
public:
  stack( ); 
  ~stack( ); 
  void push(T const&);
void makeEmpty();
  bool isEmpty() const;
  void pop();
 T top();
  
 private:
  ListNode<T> *topOfStack;

};
#endif
mrnutty 761 Senior Poster

Iwhen to use a void function verses a bool or int or other type of function.

Whenever you create a function, you need to know what it does
first. Does it sort an array, does it return the square of a number,
does it print a triangle? Whatever, it does, you need to know its
objective. After knowing its objective, you can easily figure out
what type function you need. If it calculate the square of a number
then make it return int, since it make sense to return the square
of number. If it sorts an array, then it should be void function
because array are pointers. If it just prints stuff then make it
void because there is no need to return anything. So analyze what
a function does and then look at the different possibilities, and
pick one that fits the situation.

Also how to know what to call into the Function?

Again this is when you know what you want case. When
you know what a function does and you know what you need
it to do, then passing the required parameters isn't hard.
Also you should have functions that you don't use. The
reason you should create functions is for clarity, mostly.
So if you decide to create a function make sure you use it,
its not too big, and you know what its supposed to do.
After …

mrnutty 761 Senior Poster

So do it in stages, say the spaces up to and including the first hash.

You don't have to write the whole thing just to make progress.

Exactly the idea behind " Divide and conquer " strategy, which
in fact is very intuitive.

mrnutty 761 Senior Poster

Have a left, middle, and end variable. Make middle static.

Move left up 1, move end down 1. If left == mid || right == mid then stop.

mrnutty 761 Senior Poster

Surely you're joking Mr. firstPerson. STL is for Standard Template Library.

One of the few times someone has called be Mr.
No I know what STL is, my question was with your response

It is about STL?

I didn't quite get your question.

mrnutty 761 Senior Poster

Dear firstPerson,
It is about STL?

What do you mean?

mrnutty 761 Senior Poster

" num.at(i) "

num is an integer and not an array. This does not work.
I think what you are trying to do is something like this :

int digits[4] = {0};
int num;
cin >> num;
int i  = 0;
while(num)
{
     digits[i] = num%10; //get the last digits
     num /=10; //remove the last digit
}

Although not tested, I think that will extract the digits from num into
an int array. Then you can convert it into char if you want.

mrnutty 761 Senior Poster

Enquiries about printf function is more of a c question don't you think?

mrnutty 761 Senior Poster

To convert string to numeric datatype look here

mrnutty 761 Senior Poster

For now, I would suggest openGL, as it is easier to learn. Google
tutorials for it. This link
would help you get opengl installed.

mrnutty 761 Senior Poster

Well to make a GUI program in opengl, say a basic triangle, all you need
to know about c++ is, functions,libraries,if-else,and switch-statements.

From that you can make your first triangle. Although it might be hard
to understand what which function does from the GUI library.

My suggestion would be to program for a at least a year, before jumping
into GUI.

mrnutty 761 Senior Poster

or you can use pointer member function, and pass the class a function
to call.

#include <iostream>

using namespace std;
 

void callThisFunc() { cout<<"This function was called"; }

class CallFunc
{
	
	typedef void(*pF)();

private :
	int x;
public:
	CallFunc() : x(100){ }
	CallFunc(int xo) : x(xo) { }

	void callSomeFunc(pF yourFunc) { yourFunc();cout<<" and x is : "<<x<<endl; }

};

int main()
{
	CallFunc cF(123);

	cF.callSomeFunc(callThisFunc);
 
    return 0;

}
mrnutty 761 Senior Poster

Basing the code off of what firstperson has above, if you want to loop the program, you could use a while loop like so <snippet removed>

You know you should hint the answer and not give it out. Maybe give
him an example of how to use while loops first?

mrnutty 761 Senior Poster

.

mrnutty 761 Senior Poster

Here is your program written better. You could have used switch statements, but I wasn't sure if you knew them.

Use code-tags.

/*
Pick a problem and solve
*/

#include <iostream>
using namespace std;

int main()
{

    int x;
    float a,b;

    cout << "Hello, please pick a type of problem from the list. Please use the number of  each: \n\n"; 
    cout << "1. perimiter\n";
    cout <<"2. area\n";
    cout <<"3. addition\n";
    cout <<"4. multiplication\n";
    cout <<"5. subtraction\n";
    cout <<"6. division\n";
    cout <<"7. Quit program. \n";
    cin >> x ;

    //if x is less tha 1 or greater than 7 then throw error
    while(x<1 || x >7)
    {
        cout << "Hello, please pick a type of problem from the list. Please use the number of each: \n\n"; 
        cout << "1. perimiter\n";
        cout <<"2. area\n";
        cout <<"3. addition\n";
        cout <<"4. multiplication\n";
        cout <<"5. subtraction\n";
        cout <<"6. division\n";
        cout <<"7. Quit program. \n";
        cin >> x ;  
    }

    if(x == 1) 
    {

        cout << "Enter the length: "; 
        cin >> a;
        cout<<"\n\n";
        cout << "enter the width: ";
        cin >> b; 
        cout<<"\n\n";
        cout << "The perimiter is: ";
        cout << 2*(a+b);
    }

    else if( x == 2)
    {
        cout << "enter the length: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the width: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The area is: ";
        cout << a*b;
    }


    else if(x == 3)
    {
        cout << "Enter the first number: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the second number: ";
        cin >> …
mrnutty 761 Senior Poster

>In your function, you allocate new memory for the array
>that is passed, which already has been allocated memory.

I suppose he has commented that line.
Hence technically, there are no memory leaks in the first program.

I guess it was late. Didn't see the backlashes.

mrnutty 761 Senior Poster

also in the first program there is no memory leak.

Are you sure. His program is tricky in a sense that there is a memory
leak. His function is definitely not doing what he thinks its doing.

One way to tell if you have memory leak is to see if every new
is matched with delete. In your function, you allocate new memory for the array
that is passed, which already has been allocated memory. So when
inside the function, the arrays thats passed is located in whatever
memory address that was reserved inside the function. When he
exits out that functions, that memory is no longer in play. Inside
main he has already the array pointing at a specific address.

So the memory inside the function has a leak. Also you use
2 instances of new but only 1 delete. Does that give you a hint?

mrnutty 761 Senior Poster

forgot the int in int main(), or does your compiler support default
int. And system command, argghhh. Throw them away. cin.get() would
be a better choice to "pause" the screen.

mrnutty 761 Senior Poster

I think it would help if you look at it in binary perspective.

int x = 0x26;

in binary 0x26 =
0010 0110 //its separated for you to see it better

now we have this binary number : 0010 0110 and we want to toggle
the 3rd bit : the third bit is 00100110 the one on bold.

Lets use & to toggle it
int x = 0x26; // 0010 0110
int y = 0x22;// 0010 0010
int z = x&y; // ????

Lets see what happens in binary form.

Note that :
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0
0 & 1 = 0

0010 0110 //x
&
0010 0010 //y
--------------
0010 0010 //z

so , you see that the 3rd bit is 0 while rest is remained unchanged.

mrnutty 761 Senior Poster

adds and subtracts for doing encryption and decryption

If your program justs shifted character x units to the right for encryption then you need the same translation to the left for decryption

You cant use 2 different keys for simple shifting encrypt/decrypt

Unless for the decrypt, you have a formula that converts a
different key to the same key as the encrypt key.

mrnutty 761 Senior Poster

ok....

1)i want to set the lines at run time.( w.r.t. x-axis,y-axis,clockwise and anti-clockwise)

Ok whats the problem. In your drawFunc have the (X_i,Y_i,Z_i)
and the (X_f, Y_f, Z_f) as variables and change it accordingly. Maybe
if '+' is pressed, increase the length of each X coord by 1 unit...

2)i want to detect collision of a circle with the lines(which may be in slope), and after collision the circle should bounce in a paricular direction.....

Option # 1) Check if the lines is within the circle's position. The
circle position has an X,Y,Z coordinate. If the line is within the area
of the circle then there is a collision. And move the circle -x.

Option # 2) Check if the circle + radius is within the Top height
and the low height of the line. If so then check if its X position + radius
is within the lines X coordinate of the line. If so then there is a collision and
move circle to -x, maybe by making its x velocity = -x velocity.

mrnutty 761 Senior Poster

Ask your question in a clear and concise manner.

mrnutty 761 Senior Poster

I think what you are looking for is something like this :

char *twoDee[][3] = {{"item1", "1", "1"}, {"item2", "1", "2"}};

You need to supply the column before hand.
Thus the code :

char *p[][] = {{"item1", "1", "1"}, {"item2", "1", "2"}};

Is an error because the compiler doesn't know the number of column before runtime.

mrnutty 761 Senior Poster

@firstperson i really appreciate the help
God bless....

Hope you learn from it. Put comments on the program so its better
and you will learn more.

mrnutty 761 Senior Poster

Fixed it a little. Please try the rest on your own, until you really need
help.

#include<iostream>
#include<algorithm> //for std::sort
#include<string>

using namespace std;

struct student
{
  int id;
  string name;
  string nationality;
  string gender; 
};

void insert(student array[],const  unsigned int MAX_STUDENT);  

bool sortByID(const student& a, const student& b)
{
	return ( a.id < b.id );
}


char sortByName(const student& a, const student& b)
{
	return ( a.name < b.name);
}

char sortByNationality(const student& a, const student& b)
{
	return (a.nationality < b.nationality);
}

void mySort(student array[],const unsigned int MAX)
{
	int opt = 0;
	cout<<"Welcome to sorting function please do the following selection"<<endl;
	cout<<"***************************************************"<<endl;
	cout<<"* 1.Sort by student ID"<<endl;
	cout<<"* 2.Sort by student Name"<<endl;
    cout<<"* 3.Sort by Nationality"<<endl;
	cout<<"***************************************************"<<endl;
	cout<<"Selection: ";
	cin >> opt;
	cout<<"\n\n";

	switch(opt)
	{
		case 1: std::sort(array,array+MAX,sortByID); break;
		case 2:	std::sort(array,array+MAX,sortByName); break;
		case 3:	std::sort(array,array+MAX,sortByNationality);break;
	}

}

void display(const student array[], unsigned int MAX);
int main()
{	
	const unsigned int MAX_SIZE = 2;
	student array[MAX_SIZE];

	int option = 0;
	bool exitProgram = false;

	do
	{ 
		cout <<"Welcome to student recording system"<<endl;
		cout <<"Please choose one of the following option\n\n"<<endl;
		cout <<"1.Insert new record"<<endl;
		cout <<"2.Sort record"<<endl;
		cout <<"3.Delete record"<<endl;
		cout <<"4.Display record"<<endl;
		cout <<"0) Exit program\n\n\n";
    
		 cin >> option;

		 switch(option)
		 {
			case 1: insert(array,MAX_SIZE);  break;

			case 2:	cout<<"you picked sorting\n";
					cout<<"Now sorting\n\n\n";
					mySort(array,MAX_SIZE);
					cout<<"Sorting done\n\n\n";
					break;

			case 3: //do delete
			case 4:   display(array,MAX_SIZE); break;

			default : exitProgram = true; break;
		 }

		}while(!exitProgram);     		
	  

	return 0;

}
void insert(student array[],const  unsigned int MAX_STUDENT)
{

	cout<<"\n\n";

  for(unsigned …
mrnutty 761 Senior Poster

When accessing elements of array, the index number must
be a integral type.

so

int Array[4];
Array[4] = 1;//valid
Array[2] = 9;//valid
Array[-3] = 3; //valid but a bug
Array[3.14] = 3; //Wrong

Your code :

pur[nWeek][counter] = purchase;

Booth nWeek and counter is of type float. So either change
the parameters or typecast like so :

pur[ int (nWeek) ][ int (counter) ] = purchase;

The above is dangerous and could be buggy

Salem commented: Got there first +36
mrnutty 761 Senior Poster

change to

void enterPurchase(float pur[][25], float nWeek, float counter);
mrnutty 761 Senior Poster

@VernonDozier : wow. When someone writes that long of a
paragraphs you know he means it

@xfreebornx take a look :

int option = 0;
int quit = -1;
do
{ 
        cout <<"Welcome to student recording system"<<endl;
	cout <<"Please choose one of the following option"<<endl;
	cout <<"1. Insert new record"<<endl;
	cout <<"2. Sort record"<<endl;
    
       cin >> option;

     switch(option)
    {
        case 1:  insert(array);  break;

        case 2: cout<<"you picked sorting\n". 
                     cout<<"Now sorting\n";
                    mySort(array, numberOfElements); //see way below
                    cout<<"Sorting done\n";
                    break;
    }

}while(option != quit)


//after end of main

bool mySort(int array[], int numberOfElements)
{
    if(numberOfElements == 0) return false;

   //Either create your own myCompareFunction, or use the ones that I provided in the previous post.
    std::sort(array,array+ numberOfElements, myCompareFunction); 

    return true;
}
mrnutty 761 Senior Poster

Compare and contrast (sorting wise) :

#include<iostream>
#include<vector>
#include<ctime> //for time.h
#include<algorithm>

using namespace std;

struct Info
{
	int id;
	char gender;

	Info() : id(0) , gender(' ') { }
	Info(int i, char g) :id(i), gender(g) { }
};

bool sortByID(const Info& a, const Info& b)
{
	return ( a.id < b.id ); // accending order
}
bool sortByName(const Info& a, const Info& b)
{
	return ( a.gender < b.gender); //accendign order
}
void myPrnt(const Info& a)
{
	
	cout<<"myHouse[i] = " << "id : "<<a.id<<"\t"<<"gender : "<<a.gender<<endl;
}
int main()
{
	srand( unsigned(time(0)) );
	const int MAX = 4;
	
	Info myHouse[MAX];
	
	//male,female,unknown
	char g[4] = "mfu"; //used below

	for(int i = 0; i < MAX; i++)
		myHouse[i] = Info(rand()%100,g[rand()%3]); //assign randomly

	cout<<"Original content : \n\n";
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\nSorting by id number(accending order)  : \n\n";
	std::sort(myHouse,myHouse+MAX,sortByID); //prnt
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\nSorting by gender(accending order) : \n\n";
	std::sort(myHouse,myHouse+MAX,sortByName);
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\n";
}
mrnutty 761 Senior Poster

Pick 1)

I)

std::cout<<hex;

II)

hex(cout);

III)

cout.setf(ios_base::hex,ios_base::basefield);

All these shows the output in hex. So no calculation needed.

mrnutty 761 Senior Poster

look at this Table and you
will see that each '0' through '9' has a value in numerical standpoint.

mrnutty 761 Senior Poster

You might want to change some part of the student to
accommodate strings. For example, a name should be a std::string
instead of a char.

struct student
{
  int ID;
  char name;
  char nationality;
  char gender;
};

To use the keyword delete, for your array, you have to dynamically
allocate memory. Since you are not doing that you can do
some other things. One that comes to mind is delete all
data by assigning them 0 and/or "".

To sort the members you can use std::sort, with your own compare
function.

mrnutty 761 Senior Poster

Consider separating the 1's and 0's with whitespace.
Otherwise, you may need to read each character as a char and convert to its equivalent digit.

the file contains 3 lines of 1,000 integers. That will be a waste of time
if you want to separate each int with white spaces.

just read it as a char, like he suggested. Then subtract '0' from it
to get the int value. Something like this :

char c;
ifstream iFile("num.txt");
int i = 0;
while(iFile.get(c) && i < 1000)
{
     int num =  c - '0';
    myArray[0][i] = num;
     i++;
}
mrnutty 761 Senior Poster

If you didn't realize this code :

int numX;
    int numY;
    int numZ;
    
    int sum1 = numX - numZ;
    int sum2 = numX - numY + numZ;
    int sum3 = numX + numY;
    
    int sum4 = numX + numY + numZ;
    int sum5 = numX;
    int sum6 = numX - numY - numZ;
    
    int sum7 = numX - numY;
    int sum8 = numX + numY - numZ;
    int sum9 = numX + numZ;
    
    int totalA = sum1 + sum2 + sum3;
    int totalB = sum4 + sum5 + sum6;
    int totalC = sum7 + sum8 + sum9;
    int totalD = sum1 + sum4 + sum7;
    int totalE = sum2 + sum5 + sum8;
    int totalF = sum3 + sum6 + sum9;

Is useless because numX,numY,numZ is not initialized. You need
to first get num* then calculate it. What you are doing is
calculating junk, then getting num* then showing the junk you
calculated.

Again get user input first into numX,numY,numZ, then calculated
whatever you need to calculate.

-------
num* = numX, numY, numZ

mrnutty 761 Senior Poster

Basic encryption, i.e weak but encrypted.

1) Read file content into string
2) Have a key number, say 5.
3) Shift all letter in the string by adding the key number

for(int i =0; i < string.size(); i+=) string[i] +=key_number

4) Now its encrypted.
5) For decryption, just using the same for loop but instead
do string -=key_number.

mrnutty 761 Senior Poster

"finding parts of files."

No, not finding parts of the file but read the whole file, while
analyzing it in the process.

Assuming the file is the something like this :

Info.txt
---------------------------------------------
Password = hello_world
Active t = false
---------------------------------------------
1) open up a file with fstream
2) read first line into string
3) increment the string until it reaches the equal sign, then the password something like this :

while( *stringVariable++ != '=' ) ; //increment until the = sign
while( ! (isalpha (*stringVariable++) ) ); //increment untill you reached the first letter in the password.

4) Now you have got the password. save it.
5) read the next line. do the same process like in # 3
6) compare stringVariable to flase or true. Set bool variable
accordingly.

7) Continue with the rest of your program.

mrnutty 761 Senior Poster

Thanks for helps. Can you tell me how can I fix it?

double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	while (source_file >> number_in_file)
	{		
		total = number_in_file + total;
		count ++;
	}	
	//average of numbers in file
	return (count);	
}

http://www.daniweb.com/forums/post155265.html#post155265

Pay attention.

mrnutty 761 Senior Poster

Nested class means that a class is "nested" i.e, in scope of
the outer class.
Thus the code :

class Outer
{
public:
    class  Inner{ ... };
}

has a nested class called Inner because its inside the scope of the
outer class. And if public, it could be used with the scope resolution operator ::, like so Outer::Inner::someFunc

mrnutty 761 Senior Poster

Are you including other header file that has classes?

If so then the class is probably missing a semicolon at the end of
its definition.

class A
{ ... 
} ; <--- need semi-colon?

If you do not have class defined in other header file, then something is missing a semi-colon.

mrnutty 761 Senior Poster

Because its inside the class definition. Technically, you can access a
class data member , if you are trying to access it inside its definition.

Think of class B like a member function accessing its private data,
except its a class instead of a function.

mrnutty 761 Senior Poster

string.erase might be what you are looking for.

mrnutty 761 Senior Poster

For your question # 2, you can use the public interface of the elevator,
but it should be only allowed to be used by passenger, and not the
building class.

And I am not sure what question # 1 is asking. Care to elaborate.

mrnutty 761 Senior Poster

"void fun() throw();"

That declaration tells compiler that this function , fun(), won't throw
any exception.

Similarly :
void fun() throw(myException& m);

Tells compiler and user that this function could throw a exception
shown.

mrnutty 761 Senior Poster

Is it possible to post your complete code, if its not to big?