NathanOliver 429 Veteran Poster Featured Poster

please post the code you are using right now

NathanOliver 429 Veteran Poster Featured Poster

this is from lazyFoo's faq page
Q: Can I use your code to make a game?
A: Basically my rules are:
1) If you're using it for a closed source program, go nuts.
2) If you're using it for an open source program, please cite the chunks of code you used with something like:

/*This piece of code was originally from Lazy Foo' Productions
(http://lazyfoo.net/)*/

3) I you're using it for a GPL, LGPL or other such program which gives public rights to the code, I'm going to have to say no.
4) If you're using it for a tutorial, you're going to have to use your own original code.

You still need Contact me and show me specifically the code you plan to use and what you're going to do with it so I can approve.

NathanOliver 429 Veteran Poster Featured Poster

I believe the reason for this is the or in the while condition. If you change it to and it will work. If I remember correctly this happens because the compiler evaluates the whole statement and causes a false negative. I know there is a post on here about this problem but I can not remember what it is. Either that or I'm losing my mind which I can't rule out. :)

NathanOliver 429 Veteran Poster Featured Poster

the reason he has it returning the an istream reference is to be able to chain it. by that I mean

cin >> input >> anotherInput >> read_hw(cin, homework);
NathanOliver 429 Veteran Poster Featured Poster

it erases the last element in the string which happens to be the newline. here is a good reference for working with strings.

NathanOliver 429 Veteran Poster Featured Poster

the part that says run error code is for you to complete. You need to put the code there to handle an invalid object.

NathanOliver 429 Veteran Poster Featured Poster

Are you supposed to implement your own list class?

NathanOliver 429 Veteran Poster Featured Poster

To follow WaltP's adive you could simply do

srand(unsigned(time(0)));
string names[] = {"Jim", "Tom", "Jeff", "Larry"};
cout << "The person that should drive is: " << names[rand % 4] << ".";
NathanOliver 429 Veteran Poster Featured Poster

well if that is the only data in the text file and there is a space between the : and the info you want then it is pretty simple. You read in the first part of the line into a dummy variable and then the you read in the second part into the variable you want.

string trash, username, password;
ifstream fin("userinfor.dat);
fin >> trash >> username;
fin >> trash >> password;

The reason this works is because the >> operator reads until it see white space. So the first part reads in Username: and then there is only the username left. The same thing goes for the second line. If you have multiple usernames and password then you can also put this in s loop but the data has to be continuous. Thinking of getting info from a file as the opposite of displaying data to the screen.

NathanOliver 429 Veteran Poster Featured Poster

You might be able to do something like this.

//...
      if((strcmp(token[0], "PRISM") == 0))
      {
        if((prismSide1 = atof(token[1]) = 0.0);
            run error code
        if((prismSide2 = atof(token[2]) = 0.0);
            run error code
        if((prismSide3 = atof(token[3]) = 0.0);
            run error code
        prismCalc(prismSide1, prismSide2, prismSide3);
      } //if
//...

Personally I would use string streams for the conversion and then check if the stringstream failed or not.

NathanOliver 429 Veteran Poster Featured Poster

look at this part of your code

for(int n=0;n<11;n--)

What happens to n as the loop goes on? Does n ever become greater than or equal to 11?

Your else statement is also wrong. Have you ever seen else followed by a condition? here is a link for if, else syntax.

NathanOliver 429 Veteran Poster Featured Poster

What are the error that you are getting?

NathanOliver 429 Veteran Poster Featured Poster

The problem with your algo is that when you get to numbers like 15 which are divisible by both 3 and 5 you add it in twice. I think a a better way would be to use one for loop from 3 to 1000 and check with an if statement if the number is divisible by either 3 or 5. If it is add it to the total if not continue. One way you can make it run a faster is to skip all even numbers. (hint) You can do this in the step condition in the for loop.

Grovega commented: Thank you :) +0
NathanOliver 429 Veteran Poster Featured Poster

In between lines 5 and 6 try adding

timestamp.erase(timestamp.end() - 1);
iamthesgt commented: Thanks man! +2
NathanOliver 429 Veteran Poster Featured Poster

The second is invalid. Only a pointer to can use the new operator. Here is a little breakdown for you.

int foo = 10;  // normal assignment;
int *fooPointer = new int;
*fooPointer = foo;  // now fooPointer is a pointer to an int that holds the value of 10
int & fooReference = foo;  // reference.  now anything that happens to fooReference happens to foo.
NathanOliver 429 Veteran Poster Featured Poster

There was a bug in the code that I posted. you need to move line 46 in your code to be in between lines 48 and 49. Your isPrime function also needs some work. You have 9 in your prime pairs and 9 is not a prime number. Here is a isPrime function for you to digest

bool isPrime(int number)
{
    if (number == 1 || number == 2)
		return true;
    for (int i = 3; i * i <= number; i+=2)  // only check odd numbers and limit checks to sqrt(number) using * instead of sqrt()
    {
	if (number % i == 0)
		return false;
    }
    return true;
}
NathanOliver 429 Veteran Poster Featured Poster

There is no way to get the size of an array after it has been passed to a function. here is a good explanation.

NathanOliver 429 Veteran Poster Featured Poster

If you are checking that data inside the containers against the supplied response then you might want to think about using templates. Here is a simple way to check through containers using iterators.

template<typename ForwardIterator>
void CheckResponse(int response, ForwardIterator first, ForwardIterator last) // last should be one past the end
{
    while (first++ != last)
    {
        if (response == *first)
            cout << "found";
    }
}

// now you can use this function like
const int listSize = 20
int list1[listSize];
vector<int> list2(listSize);
list<int> list3(listSize);
int response;

CheckResponse(response, list1, list1 + listSize);  // array going one past the end
CheckResponse(response, list2.begin(), list2.end()); // vector, end goes one past the end
CheckResponse(response, list3.begin(), list3.end()); // list, end goes one past the end
NathanOliver 429 Veteran Poster Featured Poster

The way I would go about this is to use a prime number function. Then use a while loop to keep track of how many sets of primes you have found

bool isPrime(int number)
{
    // put code here to check if a number is prime;
}

int main()
{
    int numberOfSets;
    int SetsFound = 0;
    int primeNumber = 3;
    cout << "How many twins do you want to find: ";
    cin >> numberOfSets;
    while (setsFound < numberOfSets)
    {
        if (isPrime(primeNumber) && isPrime(primeNumber + 2))
        {
            cout << "(" << primeNumber << ", " << primeNumber + 2 << ")\n";
            primNumber += 2
            setsFound++;
        }
    }
    return 0;
}

The font gets converted to how the bbcode is set up. I don't believe there is a way to change that.

NathanOliver 429 Veteran Poster Featured Poster

When you declare an array or a vector you don't subtract one from the size when you declare it.

int foo[30];  // creates a array of size 30 indexed from 0-29
vector<int> bar(30);  // creates a vector of size 30 indexed from 0-29

if you do

int foo[29];  // creates a array of size 29 indexed from 0-28
vector<int> bar(29);  // creates a vector of size 29 indexed from 0-28

@Ancient Dragon 0,1,2...29,30 counted up is 31

Ancient Dragon commented: good catch :) +17
NathanOliver 429 Veteran Poster Featured Poster

I would have to agree with caut_baia that we need to see find_original and also some more of your code.

NathanOliver 429 Veteran Poster Featured Poster

you need to reset your i variable back to zero after your for loops that start on lines 8 and 34. By the way what exactly are you trying to accomplish here?

NathanOliver 429 Veteran Poster Featured Poster

you need to change your function decleration on line 8 to

void Read_Data(ifstream &, string[], double[], double[], double[], double[], double[]);
NathanOliver 429 Veteran Poster Featured Poster

since a and b are pointers you need to use the -> operator not the . operator

NathanOliver 429 Veteran Poster Featured Poster

Please post an example of what you are trying to do

NathanOliver 429 Veteran Poster Featured Poster

What are you trying to do with it? Also please use code tags when posting code. I would also change your for loop to

for (size_t i = 0; i < str.size(); i++)
{
    //...
}
NathanOliver 429 Veteran Poster Featured Poster

The way you have your driver class setup right now you would need get functions like I previously posted. The reason for this is that the name variables in driver are private. The only way to access private members is either in a function of the class or to use access-or functions. this might also help.

NathanOliver 429 Veteran Poster Featured Poster

What do you have so far?

NathanOliver 429 Veteran Poster Featured Poster

Well you can make an acessor function in your driver class that will return the name like

class Driver
{
public:
    //...
    std::string GetFirstname() const { return firstname; }
    std::string GetSurname() const { return surname; }
    //...
private:
    //...
};

class Manufacturer
{
    //...
};

void Manufacturer::WhatIsYourTeam
{
    std::cout << "Driver 1: " << driver1.GetFirstname() << " " << driver1.GetSurname();
    std::cout << "Driver 2: " << driver2.GetFirstname() << " " << driver2.GetSurname();
}
NathanOliver 429 Veteran Poster Featured Poster

What is user input declared as? It looks as if you a re trying to use an initialize list. Make sure your compiler is able to use that feature if that is what you are doing.

NathanOliver 429 Veteran Poster Featured Poster

on lines 64 through 67 you are passing the drivers address to the functions but you defined the functions as taking a driver by value. Here is the different ways you can pass an argument to a function.

#include <iostream>

using namespace std;

int PassByValue(int value)
{
    return value += 5;
}

int PassByReference(int & value)  // & is used here to signify a reference
{
    return value += 5;
}

int PassByPointer(int * value)
{
    return *value += 5;
}

int main()
{
    int value = 5, returner;
    cout << value << endl;
    returner = PassByValue(value)
    cout << After PassByValue() value is: " << value << "\treturner is: " << returner << endl;
    returner = PassByReference(value);
    cout << After PassByReference() value is: " << value << "\treturner is: " << returner << endl;
    returner = PassByPointer(&value); // & is used here to pass an address
    cout << After PassByPointer() value is: " << value << "\treturner is: " << returner << endl;
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

Why are you including a .cpp file? The general structure of the the files should be like

main.cpp: This is the cpp file where main is. You can call it anything. In this file you will include all header files needed

className.h: This is where the declaration of the class goes

className.cpp: This is where you define your class.

//main.cpp
#include <iostream>
#inlcude "Player.h"
#inlcude "Game.h"

int main()
{
    //...
}
//  player.h
class Player
{
   // just the declaration
};

// player.cpp
Player::Player()
{
    //...
}

Player Player::GetName() const { return name; }

//...
// game.h
class Game
{
    // just the declaration
};

// game.cpp
Game::Game()
{
    //...
}

Player Game::GetPlayer(int index) const { return Players[index]; }
NathanOliver 429 Veteran Poster Featured Poster

Use std::vector<std::string>

NathanOliver 429 Veteran Poster Featured Poster

if you want to display a number try doing this to your for loop.

for (count = 0; count < items; count++)
		cout << "**  Sales Item " << count + 1 << ":    $" << setw(9) << setprecision(2) << price[count] << "           **" << endl;

You can split up your output almost any way you want to

NathanOliver 429 Veteran Poster Featured Poster

[EDIT]disregard[/EDIT]

NathanOliver 429 Veteran Poster Featured Poster

That is because you are displaying garbage values and since you are using ios::fixed it is displaying the entire value without an exponent.

NathanOliver 429 Veteran Poster Featured Poster

line 29 should be total = total + price[count]; . What total = total + price does is say add total to the address of the first element in prince and then store it in total. You cant do that because total is a double. What I did say to add the price entered with total and store the result back in total,

NathanOliver 429 Veteran Poster Featured Poster

The data in a vector is held in a dynamic array. As such the memory has to be contiguous. The address of the array can change when the array is re-sized but the elements are guaranteed to be contiguous.

NathanOliver 429 Veteran Poster Featured Poster

Sorry The code should look like this

std::vector<team> GetTeamInfo()
{
    int numTeams = 0;
    std::cout << "Please enter the number of teams: ";
    std::cin >> numTeams;
    std::vector<team> teams(numTeams);
    for (int i = 0; i < numTeams; i++)
    {
        std::cout << "please enter the name of team "<<(t)<<endl;
	std::cin >> teams[i].name;
	std::cout << "please enter the attack score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].atk;
	std::cout << "please enter the defence score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].def;
	std::cout << "please enter the keeper skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].keeper;
	std::cout << "please enter the striker skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].striker;
    }
}
NathanOliver 429 Veteran Poster Featured Poster

Line 7 in your header file should be <string> not "string.h"

NathanOliver 429 Veteran Poster Featured Poster

On line 29 you are declaring structs for each variable.

void setup (struct team one, struct team two, struct team three, struct team four, struct team five, struct team six, struct team seven, struct team eight)

It should be

void setup (team one, team two, team three, team four, team five, team six, team seven, team eight)

Also if you want your structs to retain what happens to them in the functions then you need to pass them by reference.

void game(team & one, team & two);

You also might want to look up vectors. using vectors and loops would significantly cut back on the code you are writing. As an example here is how you could write an input method.

void GetTeamInfo(std::vector<team> & teams)
{
    int numTeams = 0;
    std::cout << "Please enter the number of teams: ";
    std::cin >> numTeams;
    for (int i = 0; i < numTeams; i++)
    {
        std::cout << "please enter the name of team "<<(t)<<endl;
	std::cin >> teams[i].name;
	std::cout << "please enter the attack score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].atk;
	std::cout << "please enter the defence score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].def;
	std::cout << "please enter the keeper skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].keeper;
	std::cout << "please enter the striker skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].striker;
    }
}
NathanOliver 429 Veteran Poster Featured Poster

Actually a vector is guaranteed to have there data in one block of memory.

from http://www.cplusplus.com/reference/stl/vector/

Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

a linked list actually can have its data all over the memory because they only point to other nodes.

from http://www.cplusplus.com/reference/stl/list/

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the element preceding it and a link to the element following it.

Does the code work properly and you are only getting an error from Memory Validator? If that is the case it might be a false positive. Do you modify the vector in any way before you try to get the string from it?

NathanOliver 429 Veteran Poster Featured Poster

Depending on your knowledge you can do a couple things. You could read the entire line into a string and then use a stringstream to parse it. you could put the other 7 variables in a vector or some other sort of container. If you want all 7 in the same string then after you read in the first 3 variables then just use getline and read the rest into the fourth variable.

NathanOliver 429 Veteran Poster Featured Poster

Well if you are only using positive numbers you can use setfill() and right() to get padded zeros. if you are going to have negative numbers then I think some string manipulation will be in order.

setfill -> http://www.cplusplus.com/reference/iostream/manipulators/setfill/

right -> http://www.cplusplus.com/reference/iostream/manipulators/right/

NathanOliver 429 Veteran Poster Featured Poster

Is preferredPaths a member of your CDirLoader class? How is the vector filled? Also there is no return type for the function. Did you omit it or is there not one?

NathanOliver 429 Veteran Poster Featured Poster

I had asked why you included <stream>. You should include <string> in your code.

NathanOliver 429 Veteran Poster Featured Poster

How is the file formatted? What type of data is in the file?

NathanOliver 429 Veteran Poster Featured Poster

You can use setpercision() to do this. Here has some good examples.

NathanOliver 429 Veteran Poster Featured Poster

cout << "Welcome to the gradebook for\n" ; getCourseName() there is a semicolon when there should be a <<. As far as i know there is not a <stream> header file. Why are you including it?

NathanOliver 429 Veteran Poster Featured Poster

I'm not going to give any code but I will help you out with the logic.
1) Set a counter for letters removed to 0.
2) Using a for loop grab a char from the array.
3) Using another for loop start from the position of the char you grabbed go to the end checking for like letters.
4) If you find a duplicate set it to "" and add 1 to letters removed.
5) Continue looping through the loop from step one untill you reach the end of the array.
6) After the loop finishes return size - letters removed for the new size