mrnutty 761 Senior Poster

This is handy for you : sstream

Example :

#include<iostream>
#include<sstream>
#include<string>

using namespace std;

int main()
{
	stringstream ss;
	string myVal = "3.14159265";

	ss << myVal;

	float pi = 0;
	ss >> pi;
	cout<<"pi and pi*2 : ";
	cout<<pi <<"\t"<<pi*2<<endl;

	cout<<"\n\n";

	stringstream ss2;
	string myVal2 = "1.01 2.02 3.03";
	ss2 << myVal2;

	float val[3];
	int i = 0;
	while(ss2 >> val[i++])
		continue;

	cout<<"val contains : ";
	cout<<val[0]<<" "<<val[1]<<" "<<val[2]<<endl;
		

}
mrnutty 761 Senior Poster

maybe this will be of some use. Its C++ version of hangman.
Its from C++ Primer Plus 5th edition. I copied and pasted for you.

// hangman.cpp -- some string methods
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>

using std::string;

const int NUM = 26;

const string wordlist[NUM] = {"apiary", "beetle", "cereal",
"danger", "ensign", "florid", "garage", "health", "insult",
"jackal", "keeper", "loaner", "manage", "nonce", "onset",
"plaid", "quilt", "remote", "stolid", "train", "useful",
"valid", "whence", "xenon", "yearn", "zippy"};
int main()
{
	using std::cout;
	using std::cin;
	using std::tolower;
	using std::endl;

	std::srand(unsigned(std::time(0)));

	char play;

	cout << "Will you play a word game? <y/n> ";
	cin >> play;
	
	play = tolower(play);

	while (play == 'y')
	{
		string target = wordlist[std::rand() % NUM];
		int length = target.length();
		string attempt(length, '-');
		string badchars;
		int guesses = 6;
	cout << "Guess my secret word. It has " << length
	<< " letters, and you guess\n"
	<< "one letter at a time. You get " << guesses
	<< " wrong guesses.\n";
	cout << "Your word: " << attempt << endl;
	
	while (guesses > 0 && attempt != target)
	{
			char letter;
			cout << "Guess a letter: ";
			cin >> letter;
		if (badchars.find(letter) != string::npos
			|| attempt.find(letter) != string::npos)
			{
				cout << "You already guessed that. Try again.\n";
				continue;
			}

		int loc = target.find(letter);
		if (loc == string::npos)
		{
			cout << "Oh, bad guess!\n";
			--guesses;
			badchars += letter; // add to string
		}
		else
		{
			cout << "Good guess!\n";
			attempt[loc]=letter;
			// check if letter appears again …
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

When you say cover the grid, do you mean texture it? Are you
using GUI?

mrnutty 761 Senior Poster

>So if you did mean well.. and even if it came out in a bent, skewed
>and poorly communicated way, well thank you I do actually appreciate that.

Too little, too late, and wording it like your misunderstanding is my fault. It's a wonder you have any friends at all. By the way, if you were truly interested in peace and happiness, you should have taken more time to understand my post before mouthing off.

You fail. Plonk.

Your a cocky girl huh?

jephthah commented: did you have anything meaningful to add? -3
mrnutty 761 Senior Poster

Ask your question in a clear and concise manner.

mrnutty 761 Senior Poster

if(condition) { do this }
else if(another condition) { do this }
else if(another condition) { do this }
else { do default }

mrnutty 761 Senior Poster

Wiki

and another link LOL

mrnutty 761 Senior Poster

cin does read spaces or newline. For spaces and new line
you should use the .get method.

As in

ifstream inFile("a.doc");
char c;

//this will show everything in file including newline and spaces
while( inFile.get(c) ) { cout << c<<endl;}
mrnutty 761 Senior Poster

don't know or don't care on why you are reading the same file
twice, if i understand correctly, but to answer your question,

reset the file pointer to the beginning after reaching the eof(), by
using similar to this code :

ifstream inputFile("toBeOrNot2Be.doc");
inputFile.seekg(0,std::ios_base::beg);
mrnutty 761 Senior Poster

" I am tring to convert x, which is a float into an int. but I get this error"

convert float to int :

float pi = 3.14159265;
int pie1 = pi; //implicit conversion from float to int
int pie2 = int(pi); //explicit conversion from float to int, C++ style //recommended
int pie3 = (int)pi; //explicit conversion from float to int, C style

you use atoi ( a string to int ). Converts a string to int.

int i = atoi("100");

Also atoi is not a good idea. Use the standard sstream header to convert between numbers to string vice-versa.

mrnutty 761 Senior Poster

1) sort the data
2) count frequency of each data
3) find max number of frequency
4) return that number or index to that number

mrnutty 761 Senior Poster

Also, from experience. I see that using 2D array to store information
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.

If 2D is really needed , then use 1D array, and copy it into 2D array.

mrnutty 761 Senior Poster

1) Unless your compiler supports the keyword export,
you have to provided the template header and its definition
in the same file

2) Here is a example :

#include <iostream>
#include <ctime>
using namespace std; 

template<typename Type>
class Example
{
private:
	 Type var;
public:
	Example(Type T); //see definition below
	//inline definition
	void set(Type t) { var = t; }
	void show();
};

//Need explicit definition
//unless definied inside class
template<typename Type>
Example<Type>::Example(Type T)
{
	cout<<typeid(*this).name()<<" created\n";

	var = T;
}
template<typename Type>
void Example<Type>::show()
{
	cout<<typeid(*this).name()<<" contains : "<<var<<endl;
}
int main()
{
	Example<float> f(2);
	Example<char> c('2');
	
	cout<<endl;
	f.show();
	cout<<endl;
	c.show();

   return 0;
}
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

translate it it into the center then rotate, then translate it back

mrnutty 761 Senior Poster

Perhaps use windows.h if your OS is windows.

#include<iostream>
#include<windows.h>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	_SYSTEMTIME st;
	GetSystemTime(&st);

	cout<<st.wMonth<< "\\" << st.wDay <<"\\" << st.wYear<<endl;


}

or google

mrnutty 761 Senior Poster

" when the sprite rotates it isn't centered. "

elaborate a little bit more.

You don't want it to rotate around its center?

mrnutty 761 Senior Poster

Post more code.

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

Use iterator provided by list.

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

Admin, perhaps it would serve this user best, if you move this thread
to game development forum.

mrnutty 761 Senior Poster

change to

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

You know we can't see your code, so I can't help you.

mrnutty 761 Senior Poster

hmm ok lets see.

void rot(int a[], int sz)
{
   const int MY_MAX = 100; // assumes the array passed size is < 100
int  temp[MY_MAX];

	temp[0] = a[sz-1]; //copy last element from the array thats passed to the first element

	for(int i = 1; i < sz; i++ )  //now copy every element except the last 1 
		temp[i] = a[i-1];

	for(int i = 0; i < sz; i++) //now temp contains the rotated array so copy it back to the array
		a[i] = temp[i];
}
mrnutty 761 Senior Poster

Although inefficient I will suggest you a way.

void rot(int *a, int sz)
{
	int * t = new int[sz];

	t[0] = a[sz-1];

	for(int i = 1; i < sz; i++)
		t[i] = a[i-1];

	for(int i = 0; i < sz; i++)
		a[i] = t[i];

	delete [] t;
}
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

Are you using sdl to read in the file?

mrnutty 761 Senior Poster

your function prototype is declared like so :

//accepts 2 string not 1 or 3.
int name(string a, string name1);
cout<<"And what is yours?";
    getline(cin,name2);

    name(name2); //you call it with 1 paramter, or 1 string

Look below :

string name1 = "jack";
string name2 = "jill";
name(name1,name2); //valid -- Good
name(name2); //invalid -- bad
name(name1); //invalid -- bad
name(name2, name 1) ; // valid -- Good
name(name1,name2, name1); // invalid -- bad
mrnutty 761 Senior Poster

I can't use cin or cout, it's an SDL program.
Also, it's char TO string, not the other way around.

Explain a bit more. Why cant you use C++ with sdl.

mrnutty 761 Senior Poster

You could just use glPush/Pop Matrix. Makes it much easier.

1) draw sun say at (0,0,0)
2) draw earth at the same position as the sun
3) Translate earth to some radius
4) Now using glRotate on the earth would rotate the earth around
the sun.
5) use glPushMatrix() /** draw moon**/ glPopMatrix()
6) Moon should be draw at earth coodinate
7) Now translate moon to the center and back.
8) Now using glRotate, the moon would rotate around its center
which is at the earth position
9) Translate moon at radius r from earth position
10) Now using glRotate the moon rotates around the earth.
11) Use the same process for other planets.

mrnutty 761 Senior Poster

Break the pattern down.

Linearly :

some numbers | middle number| some number reversed

1 
2 |3| 2  
3 4 |5| 4 3
4 5 6 |7| 6 5 4
5 6 7 8 |9| 8 7 6 5
6 7 8 9 0 |1| 0 9 8 7 6
7 8 9 0 1 2 |3| 2 1 0 9 8 7 
8 9 0 1 2 3 4 |5| 4 3 2 1 0 9 8
9 0 1 2 3 4 5 6 |7| 6 5 4 3 2 1 0 9
0 1 2 3 4 5 6 7 8 |9| 8 7 6 5 4 3 2 1 0
mrnutty 761 Senior Poster
cin >> hex; //set the input stream to read in hexadecimal 
long num = 0;
cin >> num; //input a number, example 15
cout<<hex<<showbase; // shows output as  0xsomeNumber
cout << num; //prints  0xf instead of 15

you can do the same with ifstream iFile("number.txt")
or ofstream oFile("numbers.txt");

change cout to oFile, and cin to iFile.

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

I would advise you to read a good c++ book, maybe C++ primer plus
5th edition. Keep at it. Some part of your program was good.
And loose the goto and system commands.

mrnutty 761 Senior Poster

I'm sorry but I don't know where to begin.

Your program needs indentation, and spaces. Its unclear. It
uses system commands. Its uses goto commands all over the
place. you allocate 900000000+1 memory twice and never delete it.
Your encrypt and decrypt program is way out of hand. Your program
is pretty bad.

I don't mean to be rude but its the truth.

When you are reading in a file all you have to do is this to encrypt:

ifstream iFile(filename);
char c;
string fileContent;
while(iFile.get(c)) 
{ 
     fileContent += c;//now all the data in file is in fileContent.
 };

for(int i = 0; i < fileContent.size(); i++)
     fileContent[i] += 0x4f;

//open up another file to put the encrypted information
mrnutty 761 Senior Poster

Wait, here is the correct uploaded zip file :Land

mrnutty 761 Senior Poster

Personal preference...or is it just me because i was taught by a
teacher who was learning c++ along with the class...idk

Wow, its not a good idea for teacher to teach a subject when
he/she leaning with the class.

void main is not standard. System ("pause") is a bad command
to use. Either a get a better IDE or using cin.get(). Which is better
than system("pause")

mrnutty 761 Senior Poster

Oh sorry, its suppose to be :

int  *A = new int[s];

Also int main(), and cin.get(), not void main, system("pause");

mrnutty 761 Senior Poster

Well this snippet

int s = 0;
cin >> s;
int A = new int[s];

Lets user input the size of the array. You can use A as if its
were declared like int A[10];

Just don't forget to use delete [] A, when done using it.

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

Just an idea, check the window size with glutGet and if its not what
you want then resize it.

mrnutty 761 Senior Poster

The compiler complains because you need to initialize const int a.
Const variable are static binded by computer, so you cannot
change its value during runtime. If the above code was allowed
then you would be able to const variables during runtime. And this
would violate const data variable's purpose, to stay const.

To fix it you can do these :

const int MAX_SIZE = 10;
int Array[MAX_SIZE] = {0};

or

int MaxSize = 0;
cin >> MaxSize;
int Array = new int[MaxSize];

or just simply use vectors or deque.

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

If you want 3d the glOrtho is the wrong choice. glOrtho is for
2d. Use gluPerspective(45.0,width/(float)height,1.0,1000.0f); for 3d

your reshape functions should look something like this :

void reshape (int w, int h)
{
     if( !h) h = 1;
 
     glViewport(0,0,w,h);	
     glMatrixMode (GL_PROJECTION);
     glLoadIdentity();
 
     gluPerspective(45.0, //angle
                              (float)width/height, //ratio
                               0.001f, //znear
                               1000.0f //zfar
                             )
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}

Look at the documentation about gluPerspective

mrnutty 761 Senior Poster

"mobile+pond==a"

What you are doing is comparing with the '==' bool operator.
What you are saying with this statement is :
Is mobile plus pond equal to a? If so say true else say false.

What you want to do is use the assignment operator '='.
Setting a = mobile + pond.

This assigns a to what mobile represents plus what pond
represents.

Its might be easier to examine with numbers.

your statement :

mobile+pond==a

Let mobile = 5, and pond = 5, the above statement becomes
5 + 5 == a?
Is 5 plus 5 equal to a? But a is not initialized to anything so this
would be junk.

But this statement :

a = mobile + pond;

Let mobile = 5, and pond = 5 then the above statement becomes
a = 5 + 5;
a = 10;

This assigns a to 10. And now a has a good value and is not
full of junk.

mrnutty 761 Senior Poster

Look up ray casting for the mouse click and shoot. Basically what
you need to do is have a line defined from the mouse click point to
the player position vector. Then you have a line from the player
to the spot where you clicked with your mouse. Now you some
maybe particles system that shoots out some flares or whatever
you decide to shoot, following the line or a parabolic trajectory
the has its endpoint at the end of the line.

To move left, right, up or down, you need some sort of intermediate
that transfers data from your keys to the graphics windows.
glut has its own ways, and so does win32. Have some sort of
intermediate that tells your when the left,right,up or down keys
were pressed. When when one button is pressed, move the object
accordingly.

For jumping what I do is to have 2 functions. One function that
handles going up, and other going down. It makes it easier for
me later on to modify the height/speed of jumping up or down.

You could do it in 1 function. All you need is impulse, initial velocity,
acceleration, gravity, and other Newtonian equations that deals
with motion. The gravity should be the deciding factor on how high
an object goes up. It should slowly decrease the object
acceleration thus making it go up to a certain …

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.