what you are looking for is std::vector<string> . Here are some examples.
std::vector<string> v;
v.push_back("hello");
v.push_back("goodbye");
cout << v[0] << endl;
cout << v[1] << endl;
what you are looking for is std::vector<string> . Here are some examples.
std::vector<string> v;
v.push_back("hello");
v.push_back("goodbye");
cout << v[0] << endl;
cout << v[1] << endl;
>>*array[4]; // pointer to 5th content of array.
The above is correct only if array is declared as DataType*** array ,
because you deference it twice. If you declare array as DataType array[] then *array[4] is invalid syntax, because you are using the dereferencing operator on a DataType, and from your post, DataType is something like int,char, or some POD.
>>what is the difference between these?
char num1 = 64;
char num1 = '64';
Try it, the second one is a compiler error.
For #2, iterators are just classes. They have certain operation defined for them.
For example they define the deference operator, which is why you can deference them.
Similarly, the library does not define the operator << for the iterators, thus it is
a compiler error.
For #3, "selection < gameLibrary.end())"
.end() function returns an iterator to the one-pass-the-end element. It is not what
you want there. What you want is the .size() function.
@dusktreader: You can combine your convert code into 1 :
template<typename ReturnType, typename InputType>
ReturnType convertTo(const InputType& in){
std::stringstream ss;
ss << in;
ReturnType ret = ReturnType();
if( (ss >> ret).fail()) throw std::exception("Conversion failed");
return ret;
}
int main(){
float pi = convertTo<float>("3.1415");
string strPi = convertTo<string>(pi);
}
Oh this is a silly mistake, your function name and the variable name are the same.
Change one of them. Plus you are not returning anything? Do this :
double length ( const int a[], int size )
{
double len = 0;
for ( int i = 0; i < size; i++ )
{
len += pow((double)a[i],2);
}
return sqrt( len );
}
oh i got it! thanks!
Are you sure? I feel like I made a mistake, and just given you a code you can copy/paste?
Why dont you do a test run and see if its correct for your self. Use your calculator
to check if Question #1 is correct, and use the web to check if Question #2 is correct.
I can't really tell you because your code is hard to read.
Wait how is boost timer supposed to help? He wants to time the input from the user.
umm...couple of ways, first is the easiest.
//option #1
string path = "AMBER/data/runx/amhsk_run";
string runNumber;
cin >> runNumber;
validate(runNumber);
path += runNumber + ".root";
//option # 2
string path = "AMBER/data/runx/amhsk_run*.root";
string runNumber;
cin >> runNumber;
path.replace( path.find('*'),1,runNumber) ;
This my friend is what true love
is. I feel the need to share you a story, a wonderful story. Yesterday night, I pondered
about heaven and how I couldn't wait for the eternal happiness. I prayed and talked to god.
Today I received a message. I felt such a rush of happiness that I never felt before in my
life. This was during work at Dunkin-Donuts. I cannot even begin to describe the happiness
that I felt. I was about to tear up. From that moment on, I seen people in a whole
different light. I have learned to love god's creation. I am very happy that god accepted
me and gave me a feeling of immense happiness. This happiness that I felt, is which I
believe, I will feel again once I enter heaven. I felt the need to spread the word about
god. Thank you very much for reading.
Seems like at your level, you don't need to worry about header files, because it will
only complicate things for you. Just stick with 1 file for now, the main file.
And by the way all of this is valid :
int a;
int aa;
int aaa;
int b;
int c;
int abc;
int ac;
int ab;
You see, what matters is that NO TWO VARIABLE NAMES ARE THE SAME, if you follow that
then you will be fine.
I haven't compiled myself, but this is very confusing:
typedef struct node{ int data; struct node *next; struct node *previous; }mynode; mynode *head,*temp,*current,*tail;
First, why have you used a typedef? Second, you seem to have declared an object named mynode, then tried to declare things (head,temp,etc) of type mynode. Shouldn't they be type 'node'?
The main reason for doing this is backward compatibility. In C, you have to use the
keyword struct before declaring a struct variable. So usually, programmer use typedefs
to simply things and make things easier. But In C++, thats not required.
Or just this :
string str[5] = {"hello","ramble","people","kitty","rope"};
vector<string> vec(str,str+5);
How do I get over a break up? I am feeling really shitty. My ex girl who was my
first just gave me the "we should be friends" card. Actually we been on and off
lately because of some complicated stuff. But now, I don't want to go back, and since
she broke up( again ), I am not going back if she asks because I am tired of this rollercoaster. But right now I feel really really shitty. Been thinking about her the
whole day. Went to gym to relax my mind, and it worked a little but now the shitty feeling
is back. Any advice from the more experience?
You can prove this by giving just 1 simple example. No need for general proof, because
if it can't work for even 1 case, then its incorrect.
First some design problem. This function is pretty bad :
void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright){
if (checkup == true || checkdown == true || checkleft == true || checkright == true){
hero.damage(damage);
}
}
First an obvious way to make this better is to use the boolean as boolean, no need
for conversions, thus you should just do this
void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright)
{
if (checkup || checkdown || checkleft || checkright ){
hero.damage(damage);
}
}
But its still bad. Specifically, you have a variable for each direction. What you
should do is abstract the direction. So the param is just 1 Direction and in that
Direction it has left,right,up,and down states. So do Something like this :
struct Directions{
public:
enum Dir{EAST,WEST,NORTH,SOUTH,SIZE};
private:
bool dir[SIZE];
Directions(){
std::fill(dir,dir+SIZE,false);
}
public:
void east(bool r){ dir[EAST] = r; }
void west(bool l){ dir[WEST] = l; }
void south(bool s){ dir[SOUTH] = s; }
void north(bool n){ dir[NORTH] = n; }
bool get(const Dir direction)const{
return dir[direction];
}
};
class Player{
private:
Directions playerDirection;
public:
void attackplayer(Player& hero,const Directions& dir){
if(dir.get(dir.EAST) || dir.get(dir.WEST) || dir.get(dir.NORTH) || dir.get(dir.SOUTH)){
/* Do stuff */
}
}
};
No that might look like a little more work, but end the end it will save you more trouble.
If you know how to convert from base 10 to base 2, then you know how to convert
from any base to any other base. So the question remains, do you know how to convert
from base 10 to base 2?
1) Write code.
2) Write more code.
3) Read up on design patterns, whether it be wiki or whatever
4) goto 1 until comfortable
5) Now every time you write code, you can either :
5a) Finish the code, and the project ( depending on its size) then take a step back
and see how you can better the system, decouple it, give each class its minimal
job.
5b) Or take a step back first, and think about how each class interacts with the
other and then how all class interacts to make the program. This is where UML
comes in. It provides a visual
6) Repeat all of this procedure for like your whole life, and eventually you'll get
to be a better programmer.
>> why Turbo C++ is still the dominant compiler in universities that still teach C++
[citation?]
And, if you are using C++, then definitely use C++ containers. else if you are using C, then don't use C++ containers , lol.
Because you essentially, creating a pointer of pointers. So that means you need
to deference it 2 times.
>> don't I need to derefence
Yes you do
>>so the -> operator should be necessary on the first case
Nope. The operator[] deferences the pointer.
Better yet, you should decouple the system. A player class should not have an array
that stores the animation sequence. Try something like this for example.
class Player{
//blah blah blah
void move(int x, int y){ /*logic goes here */ }
//more blah blah blah
};
template<typename CoordType>
struct Animation{
typename typedef std::pair<CoordType,CoordType> Coordinate;
typename typedef std::vector<Coordinate> AnimationType;
virtual void animate()= 0;
};
class PlayerAnimation: public Animation<float>{
private:
AnimationType sequenceOfMoves;
Player thePlayer;
public:
PlayerAnimation(const Player& player,const AnimationType& c) : thePlayer(player), sequenceOfMoves(c){};
virutal void animate()const{
//psuedocode here
foreach(Coordinates coordinate in sequenceOfMoves){
thePlayer.move(coordinate.x(), coordinate.y());
}
}
};
void fillPlayerMoves(const Animation::AnimationType& seq){
/* logic to animate player goes here */
}
int main(){
Player player;
Animation::AnimationType listOfMoves;
fillPlayerMoves(listOfMoves);
Animation animation = new PlayerAnimation(player,listOfMoves);
animation.animate();
}
If you ever decide to release the source code I would love to take a look at it.
Actually, its not hard at all. The method I used is actually an old one, called finite
difference. Here is an example.
Consider the following sequence,
[TEX]S = {1,2,3 ... }.[/TEX]
To find a approximated polynomial function that maps into those data points, we need to
do three things.
1) Find the polynomial degree needed.
2) Create a linear system of equation.
3) Solve the linear system of equation.
To do 1), we do compute the difference between i, and i+1, if possible, until we get
a constant row of numbers. So for the above example,
ROW 0 = 1 2 3
ROW 1 = 1 1
From above, we computed the difference between the adjacent numbers. And we see that
there is a row of constant number, namely Row 1. This tells us that there is a polynomial
of degree 1, that maps into ROW 0.
A polynomial of degree 1, has the following form :
[TEX]p(n) = an^1 + b;[/TEX] [a and b are scalers]
Now for 2), we need to come up with an linear equation for p(n). We can do the
following,
[TEX]p(1) = a(1) + b[/TEX]
[TEX]p(2) = a(2) + b[/TEX]
and by …
Its this side thing I was working one, when I got the chance. Its not very good right
now, as its still in its developing stage. But its still ok, where I can show it of, lol.
Attached is an exe, that given a set of data points, it tries its best to return a POLYNOMIAL function that maps to those data points, and emphasis on polynomial!
For example here are some test runs :
<enter data points( '.' to end) > 1 2 3 .
<function : f(n) = n >
<f(n) evaluated from n = 1 to 20 : { 1,2,3,4,....19,20 }
Another example run :
<enter data points( '.' to end) > 2 4 6 8 10 .
<function : f(n) = 2n >
<f(n) evaluated from n = 1 to 20 : {2,4,6,8,10 ... 40 }
You get the idea. Again, its not very good. It can only do the simple things. And be nice .
Oh wow. This is so shocking. At least, now he's in a safe place with nothing but
happiness. Dave, if your listening, say hi to Jesus for me.
Which version of MS Wrod do you have. In version 2007, it saves the formatting
and the colors. Perhaps, try copy/pasting it on a text file. And then export it on
M.S word.
In C++ you cannot seperate the template header from its definition. So You can't have
LinkedList.cpp. You need to put the definition in the same file as the header file.
Hey Narue, let me ask you something. If you got an offer to write a software,
and your choices were only C and C++. Which would you choose ?
"Different", yes. "Better", no"
Can you show why this is ? Thats a big claim. I understand that OOP programming
can lead to bad software design because of people not knowing the language well
enough. But for the people that are very proficient, in C++. Surely, they can
write very robust and maintainable software using C++, maybe much better than C programming. I see so much advantages that C++ provides over C, yet your claim
is that C++ is not better than C. Is it because, its in a "different" category
perhaps?
I'm just asking out of curiosity, not trying attack you or anything. Thanks
Maybe this will help you get started :
#include <stdio.h>
int power(int base, int exp){
//logic goes here
}
int main(){
int res = power(2,10); //res = 1024
printf("%i",res);
return 0;
}
Here is a way to do it using metaprograming. That way you get the result before you
even start the program.
#include <iostream>
template<int binarySequence>
struct Binary{
enum{ result = Binary<binarySequence/10>::result << 1 | binarySequence % 10 };
};
template<>
struct Binary<0>{
enum{ result = 0 };
};
int main(){
using namespace std;
cout << Binary<1>::result << endl; // outputs 2^0
cout << Binary<10>::result << endl; // outputs 2^1
cout << Binary<100>::result << endl; // outputs 2^2
cout << Binary<1000>::result << endl; // outputs 2^3
cout << Binary<10000>::result << endl; // outputs 2^4
cout << Binary<10100>::result << endl; // outputs 20
}
I admit, that the formula used is not credited to me. Although the above works.
It does not have a error checking, for input that does not consists of 1's and 0's.
Isn't this so cool? You get the answer before you even run your program!
I don't know if its right. But I would suggest to apply the euler method, or better
yet, the RK4 method. Google it.
what was the best moment of your life so far??
common start sharing ur memories here!!! :)
When I lost my virginity.
What do you guys think of this new daniweb design? Personally, I'm not
very fond of it. The simpler the better. I just don't like the design. It
needs more "flavor" or something. Idk, what do you guys think?
In java you do not need put a semicolon after the class deceleration. In
C++ you need it. So this part :
#define FERMATH
#ifndef FERMATH
#include <iostream>
#include <cmath>
using namespace std;
class Fermat {
private:
long prime p;
public:
Fermat();
Fermat(long num);
long getNum();
bool isPrime();
};
#endif
You should not worry about how fast a fundamental loop is. If you
care about speed, then your bottleneck is probably elsewhere.
First thing that you need to do is write a driver program that has a main function in order to test it. Test the constructors and printRational. If you haven't written printRational, write it because you'll need it a lot when testing.
// driver program // includes int main () { RationalNumber a(); RationalNumber b (3, 4); a.printRational (); b.printRational (); return 0; }
See if it compiles/runs/gives good results. If not, debug. If yes, you're on your way and you can write some of the operators.
Minor error on the red highlighted part. The compiler will see that
as a function. So to fix that we remove the parenthesis. Making it look like this :
RationalNumber a;
>>I want to calculate sqrt(2) to the maximum level of digits possible...
You can't. Its a irrational number.
We can but not the way you showed it.
if ( (choice == 'e' || choice == 'E') && (cur == 'd' || cur == 'D' ) ){
//...
}
Have a device where men can see what women are thinking, so they wont be so complicated.
>> So it seems i basically just need to reverse the direction it's rotating.
Try reversing the angle its rotating about. Also make it an else if statement.
That's why people shouldn't jump too early into graphics without good
knowledge of the basics. Ball is a function. It is not a class nor a struct.
Thus you cannot use the dot operator on ball.
A easy way to get the ball to move is by doing something like this :
float ballX = 0.0f;
float ballY = 0.0f;
float ballZ = -1.0f;
void drawBall(void) {
glColor3f(0.0, 1.0, 0.0); //set ball colour
glTranslatef(ballX,ballY,ballZ; //moving it toward the screen a bit on creation
glutSolidSphere (0.6, 7, 7); //create ball.
}
void display(){
glClear(...);
//more stuff
drawBall();
//...
glutSwapBuffer();
}
void keyboard_s (int key, int x, int y)
{
switch (key)
{
case 'a' : ballX -= 0.05f;
case 'd' : ballX += 0.05f;
case 'w' : ballY += 0.05f;
case 's' : ballY -= 0.05f;
case 'r' : ballZ += 0.05f;
case 'f' : ballZ -= 0.05f;
}
All you its doing there is updating the position of the ball. The ball
gets redrawn every frame, but ballX, ballY , and ballZ might does not
have to be the same at each frame. Thus changing ballX, ballY, ballZ
would change the position of the ball.
And P.S : I would advice you to learn more about classes before you
go to far with this graphics. Trust me, its gonna pay off later.
>>can someone show me an example of how you would initiate a variable.
int variable = 0; //declare and initialize the variables
>>how you would fix a undeclaired identifiers.
an undeclaried identifier means that you used a variables that has not
been declared. For example :
int n = 1;
int m = 2;
sum = n + m; //error indeclared identifier
The way to solve this problem is to declare the variable. In the above
code, you need to declare the variable sum. So the correct code should be :
int n = 1;
int m = 2;
int sum = n + m; //notice the "int sum" part. Thats the declaring the variable part.
Yep, I think you need to include sstream as well. stringstream is
defined in the sstream header file.
Most likely, you did not initialize your variables. Also you probably might be
writing into memory that you are not supposed to. Because, in debug, the
compiler pads the memory more than in release.
This is a simple demonstration of meta programming. It shows the idea
of meta programming. It basically calculates the factorial of a number
before runtime. When I first saw the concept of meta programming,
it blew my mind so badly. I was so surprised that one could do this.
Its soooo kewl, don't you think?
First off, this code :
#define false 0
#define true 1
char s[100];
char sr[100];
Is bad already. You do not need to use define. C++ already has, "true" and "false" defined.
Next :
int main(){
InputString();
system("PAUSE");
return 0;
}
No need to use a system command. Use cin.get(); Do not use global variables when local will do. Make your getInput() function return the input. Why are you using a char array. Use the string header.
Get into good habit early. I would suggest your main to look something like this :
int main(){
string input = getInput();
bool isPalin = isPalindrome(input);
handleMessage(isPalin);
return 0;
}
Next :
void InputString(int n=0)
{
cout<<"Input the string you wish to be tested";
cin.getline(s, 100);
if(strlen(s)>n)
PalindromeTest();
}
There is a lot of bad things that could happen. Instead of listing them all
I will tell you the solution. Use C++ strings. Do not use char arrays.
The whole point of this exercise is to get your logical thinking skills up.
So don't make use of the strrev function. Its a good idea to use the stl
for solutions, but thats not the point here. Create your own for loops
and apply some logic.
I hope that helped somewhat, and you take it into consideration.
Because what you got right now is not very good.
>>If I erase the nodes from the list (using list::erase) will this delete the actual objects themselves, or just the pointers on the list.
It will delete the object, you don't need to worry about freeing the memory any longer.
Lets examine this for a second :
while(!isEmpty()){
ListNode<char *> *temp = head;
head = head->next;
head->previous = NULL;
delete temp;
}
Assume that the list contains, A,B,C. Where A-C can be anything.
Now your delete function will do this :
//Iteration # 1
1) while(!isEmpty()) is true go the code block will get executed.
2) ListNode<Type> *temp = head; Now Type is any data type. Now temp points to the head which points to A.
3) head = head->next; now head points to B
4) head->previous = NULL; now head->previous points to A right? So you are setting A to null. Remember temp points to A, so temp now is null. This is called a memory leak.
5) delete temp; now you delete a null, which does nothing.
//Iteration # 2
//now the list is {null B C }, where null means it does not exist
1) while(!isEmpty()) this is true so the block of code gets executed
2) ListNode<Type> *temp = head; Now temp points to the head which points to B.
3) head = head->next; now head points to C
4) head->previous = NULL; now head->previous points to B right? So you are setting B to null. Remember temp points to B, so temp now is null. This is called a memory leak.
5) delete temp; Now you delete a null, which does nothing.
//Iteration # 3
//now the list …
So, I am guessing you learned about structs and classes? If so then
go ahead and make a Residential and Commercial class. In fact you can
use some inheritance, and polymorphism here, but I guess we can keep it simple.