NathanOliver 429 Veteran Poster Featured Poster

There are two main ways to delete a list that I know. You can use recursion or you can iterate.

// recursion
void delete(listNode node)
{
    if (node.next != nullptr)
        delete(node.next);
    delete node;
}

// iteration
void delete(listNode node)
{
    listNode temp;
    while (node != nullptr)
    {
        temp = node.next;
        delete node;
        node = temp;
    }
}
NathanOliver 429 Veteran Poster Featured Poster

@rubberman - I think the main reason C++ added the brace initialization is that it now allows for more robust object initialization which enhances RAII. With brace initialization we can now write code like:

std::vector<int> numbers {1,2,3,4,5};

Instead of:

int array temp {1,2,3,4,5};
std::vector<int> numbers(temp, temp + 5);

Here are some addtional references for you:

http://en.cppreference.com/w/cpp/language/aggregate_initialization
http://en.cppreference.com/w/cpp/language/list_initialization

NathanOliver 429 Veteran Poster Featured Poster
B& operator=(const B& b)            // Copy Assignment
{
    if (&b != this) // you should always do this so you dont do a self assignment
    {
        A::operator=(b) // call A copy assignment
        i2=b.i2;
    }
    return *this;
}
NathanOliver 429 Veteran Poster Featured Poster

I dont think so. Maybe you should read the Rules.

NathanOliver 429 Veteran Poster Featured Poster

That is because you have the declaration commented out on line 92.

void Hand::Clear()
{
    //iterate through vector , freeing all memory on the heap vector<Card*>::iterator iter = m_Cards.being(); for (iter = m_Cards.begin(); iter !=m_Cards.end(); ++iter)
    {
        delete *iter;
        *iter = 0;
    }
    //clear vector of pointers
    m_Cards.clear();
}

Should be

void Hand::Clear()
{
    //iterate through vector , freeing all memory on the heap 
    vector<Card*>::iterator iter = m_Cards.being(); 
    for (iter = m_Cards.begin(); iter !=m_Cards.end(); ++iter)
    {
        delete *iter;
        *iter = 0;
    }
    //clear vector of pointers
    m_Cards.clear();
}
NathanOliver 429 Veteran Poster Featured Poster

What do you need help with? Your instructor gave you how the program should function and work. Is there a part you don't understand?

NathanOliver 429 Veteran Poster Featured Poster

I would use the modulo operator (%) and division for this. Modulo gives you the remainder of integer division so 100 % 10 = 0. So if 100 % 10 = 0 then you need to get rid of the rightmost 0 by division. Divsion by 10 will do that. Put it together and you need a while loop that checks if the value modulo 10 is 0 and then in the body of the loop you need to trim off that zero using division by 10.

NathanOliver 429 Veteran Poster Featured Poster

You have ab extra } on line 197. get rid of that and it should compile. No sure if your code is exactly right though. it looks weird that you have 3 if(!found) statements.

NathanOliver 429 Veteran Poster Featured Poster

You need to actually change input every loop. You can do that by using input /= 2; after line 17.

NathanOliver 429 Veteran Poster Featured Poster

You also need to get rid of line 23. that will force you out of the loop on the first iteration which is not what you want.

NathanOliver 429 Veteran Poster Featured Poster

Show we what code you used for that. Did you remove the line inFile >> number; from inside the while lop as that will cause a double read.

NathanOliver 429 Veteran Poster Featured Poster

Give this article a read.

NathanOliver 429 Veteran Poster Featured Poster
CTime_24& CTime_24::operator+=(int seconds)
{
    m_Seconds += seconds;
    m_Minutes += this->m_Seconds / 60;
    m_Seconds %= 60;
    m_Hours += this->m_Minutes / 60;
    m_Minutes %= 60;
    m_Hours %= 24;
    return *this;
}

////+= operator ///
CTime_24 CTime_24:: perator+=(CTime_24& sTime)
{
    m_Seconds = m_Seconds + sTime.m_Seconds;
    m_Minutes = m_Minutes + sTime.m_Minutes + m_Seconds / 60;
    m_Seconds %= 60;
    m_Hours = m_Hours + sTime.m_Hours + m_Minutes / 60;
    m_Minutes %= 60;
    m_Hours %= 24;
    return *this;

}
NathanOliver 429 Veteran Poster Featured Poster

while (!inFile.eof()) is never the way you want to read from the file. When reading the last line of the file the EOF flag does not get sent until it tries to read past the last line so while (!inFile.eof()) will actually read all lines in the file plus one. You should use a read operation to control the loop since once the read fails it stops the loop. Since you use inFile >> number; in your while loop you can rewrite it as the following:

while (inFile >> number)
{
    total += number;
    count++;
    cout << number << endl;
}
NathanOliver 429 Veteran Poster Featured Poster

This could be done very simply with the strings [] operator:

int main()
{
    std::string line;
    std::cout << "Please enter you string: ";
    getline(std::cin, line);

    // loop through each letter and add 2 to it while displayng it
    for(std::string::size_type i = 0; i < line.size(); ++i)
    {
        std::cout << char(line[i] + 2);
    }

    std::cin.get(); // use this to pause system.  not system("pause")
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

Did you copy this code from somewhere? I have a hard time believing that you could produce this code and not know how to generate random numbers.

NathanOliver 429 Veteran Poster Featured Poster

First you have to write classes that will define every object that is represented in that scene. Once you have that then you need to use those classes in your code to create each object. Just from looking at the picture you are going to need at least 10 different classes. There are kids and women in the scene so you would need a person class. There are animals so you would probably want a base animal class and then one class for each type of animal that inherits from animal. You will need a Property class that will hold everything that is in the property. There a few other things you need but I'll leave that to you. It wont be very complicated but it will be a decent amount of code.

NathanOliver 429 Veteran Poster Featured Poster

Can I ask what char *strerror(num) int num; means? I have never seen a function declared like that before.

NathanOliver 429 Veteran Poster Featured Poster

Here is your corrected code. I commented what I changed.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

void collegesUniversities()
{
    ifstream campuses;
    campuses.open("colleges.txt");

    vector<string> schoolVector;
    string schools;
    bool match = false;

    cout << "\nEnter the name of your college/university. ";
    // cin >> schools; <- dont use this since you are using getline
    getline(cin, schools);

    if (!campuses)
        cerr << "Error opening file. ";
    else
    {
        string temp;
        while (getline(campuses, temp, '\n'))
        {
            schoolVector.push_back(temp);
        }
    }

    for (size_t i = 0; i < schoolVector.size(); i++)
    {
        cout << schoolVector.at(i) << '\n';

        if (schoolVector[i] == schools)
            match = true;
    }

    // moved this outside the loop so it prints at the end of the list
    if (match)
        cout << "\n" << schools << " is on the list.\n ";
    else 
        cout << "\n" << schools << " isn't on the list.\n ";
    campuses.close();
}

int main()
{
    int choice;

    cout << "Welcome to my college and university search program.\n";
    cout << "\nPress any button to continue.\n ";
    system("pause>nul");

    do
    {
        cout << "\nPress 1 to enter possible colleges and universities.";
        cout << "\nPress 2 to find out how many colleges and universities";
        cout << " appear in your state.\n";
        cout << "Press 3 to find the total amount of colleges and";
        cout << " universities in our list. ";
        cout << "\nPress 4 to quit. ";
        cin >> choice;
        cin.get(); // <- added this to eat the newline left after getting the number

        if (choice …
NathanOliver 429 Veteran Poster Featured Poster

Change line 29 if(temp == schoolVector[i]) to if(schools == schoolVector[i]) since schools is what you ask the user for. temp is just what was last read from the file.

NathanOliver 429 Veteran Poster Featured Poster

Post the code that you have now.

NathanOliver 429 Veteran Poster Featured Poster

You should be using a different string to read from the file. You are overwriting schools with what is in the file. This:

while(getline(campuses,schools, '\n'))
{
    schoolVector.push_back(schools);
}

Should be

string temp;
while(getline(campuses,temp, '\n'))
{
    schoolVector.push_back(temp);
}

As far as problem 2 goes what you need to do is create a bool variable and set it to false. Then as you are going through the vector if you find the school then you should set the variable to true. After the loop then check the variable and print accordingly.

JasonHippy commented: Nice spot, I missed that! +9
NathanOliver 429 Veteran Poster Featured Poster

That's because you have a newline floating in the input buffer from line 32. To get rid of the newline in the buffer you can just put cin.get() after line 32. That should at least get you into the queue1 function.

Builder_1 commented: thanks alot man....really grateful for your assistance bro +1
NathanOliver 429 Veteran Poster Featured Poster

It is really weird that the lines with the text at the end don't have a coma where the lines with a number do. Since you do not need the extra column you should be able to get away with using a stringstream to split the columns. I would do something like the following. I am not sure how you are storing the information from the file in your program so in my example each part will just be a string stored in a vector. Each vector will be stored in a vector of vectors.

std::string line;
std::vector<std::vector<std::string>> lines;
stringstream ss;
ifstream fin("filename.csv");

while (getline(fin, line))
{
    std::string temp;
    std::vector<std::string> parts;
    // load line in stringstream
    ss << line;
    // we need for columns only so use a loop 4 times
    for (int i = 0; i < 4; i++)
    {
        getline(ss, temp, ',');
        parts.push_back(temp);
    }
    lines.push_back(parts);
    ss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    ss.clear();
}
NathanOliver 429 Veteran Poster Featured Poster

Well first off you need to write a function to compute x!. ! means factorial in math. Once you compute the factorial of the number then you need to go through each digit of the number and get the minimum digit. To do that you use the following algorithm:

min = 9;
number = factorial(x)
do
    digit = number % 10 <- this get the last digit of the number
    if digit is less than min and digit does not equal 0
        set min to equal digit
    number = number / 10 <- this drops the last digit off of the number
while number does not equal 0

and at last "cout" 886= 384

I have no idea what this means so i cant help you with that.

I would also like to point out that you have the wrong minimum for all of your answers 10! would be 2 and 11! and 12! would be 1.

NathanOliver 429 Veteran Poster Featured Poster

Can you paste about 10 lines or so from the file into here. Does every column value have a "," after it or does each line end with a newline? Generally with a CSV file if there is an empty column it is represented as ",," but I don't see that in your example. How you read in the file depends greatly on how the file format is defined.

NathanOliver 429 Veteran Poster Featured Poster

step through the code with the debugger or use cout statements and oupt what is happening so you know what is going. Debugging is an important skill to learn.

NathanOliver 429 Veteran Poster Featured Poster

move line 28 outside of the loop.

NathanOliver 429 Veteran Poster Featured Poster

In line 30 you are using 2 delimiters. I dont think that will work. What happenes if you just have '/n?

NathanOliver 429 Veteran Poster Featured Poster

Here is a better example for you.

//...

std::string type;
int count;
int candyTotal = 0;
int costumesTotal = 0;
int decorationsTotal = 0;

std::ifstream fin("myfile.txt");  // open file

while we read in what type of item we have keep looping through the file
while(std::getline(fin, type))
{
    fin >> count;
    if (type == "candy")
        candyTotal += count;
    else if (type == "costumes")
        costumesTotal += count;
    else if (type == "decorations")
        decorationsTotal += count;

    // ignore '\n' left in buffer from `>>`
    fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

//...
Jinxx commented: Thanks for your help! +0
NathanOliver 429 Veteran Poster Featured Poster

type is just a name for what the sales type is in the file. Every time you loop through readin the file you need to read 2 lines. You need to read the type and the amount.

NathanOliver 429 Veteran Poster Featured Poster

If you know what all of the types are then youcould do something like the folowing

while getting records from the file
    if type == candy
        add amount to candy total
    else if type == costumes
        add amount to costumes total
    else if type == decorations
        add amount to decorations total

If you do not know how many types will be involded then you will need to use 2 parallel arrays which gets a little more complicated.

NathanOliver 429 Veteran Poster Featured Poster

ifstream is a class that is used for reading files. file_in is an object of the class ifstream and it is used in the program to read from a file. Here is a good reference for what you dan do with an ifstream object.

NathanOliver 429 Veteran Poster Featured Poster
class A 
{
public:
    int x;
protected:
    int y;
private:
    int z;
};

class B : public A
{
    // x is public
    // y is protected
    // z is not accessible from B
};

class C : protected A
{
    // x is protected
    // y is protected
    // z is not accessible from C
};

class D : private A
{
    // x is private
    // y is private
    // z is not accessible from D
};
NathanOliver 429 Veteran Poster Featured Poster

Making things private vs protected deal with what access inherited classes have to members.

Public: everything has access to it.

Protected: Only acessible from the class or classes dervied from the class.

Private: Only accessible from the class.

NathanOliver 429 Veteran Poster Featured Poster

You are missing a < in between "\t" and program. Change line 19 to cout<<name<<"\t"<<rollno<<"\t"<<program<<endl; and you should be okay. Now all that's left to do is make your code standard compliant. What compiler are you using?

NathanOliver 429 Veteran Poster Featured Poster

dinad578 did you just copy rubberman's code?

NathanOliver 429 Veteran Poster Featured Poster

You are running out of the bounds of the array using i<=4 in your for loop. if(arr[i]>(arr[i+1])) will also run out of bounds when you are at the end of the array. You also don't want the print to be in the loop since it would print every time it finds a larger element. The most common way of findind the max of an array is as follows (pseudocode):

array a = {23,38, 81,12}
int max = a[0]  -- set max to the start of the array

for i = 1 to i < 4  -- start position 1 since you already have a[0] in max
{                   -- use < size of array and not <= since arrays are 0 index based
    if (a[i] > max)
        max = a[i]
}

display max
NathanOliver 429 Veteran Poster Featured Poster

Have you ever worked with iterators? Using iterators you could do code like this.

typedef vector<double> C1DArray;
double sum(C1DArray::iterator it, C1DArray::iterator end)
{
    double sum = 0;
    while(it++ != end)
        sum += *it;
    return sum;
}

int main()
{
    C1DArray data;
    for(int i = 0; i < 30; i++)
        data.push_back(i);

    cout << sum(data.begin() + 4, data.begin() + 24);
    cin.get();
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

You have a reference member (highscore) in LavaLand and that needs to be initialized upon creation. Either remove the reference or change the constructor to take in a value to initialize highscore.

NathanOliver 429 Veteran Poster Featured Poster

Look at your constructor versus my constructor.

NathanOliver 429 Veteran Poster Featured Poster

If you want LavaLand to have a default generated constructor then you need to get rid line 8 in LavaLand.h. You could also just change line 8 to LavaLand() {}; to make a default constructor yourself.

NathanOliver 429 Veteran Poster Featured Poster

Why is a captian a singleton? Are there not multiple captains in the world? If you want to make a singleton then you need to follow the correct syntax. Here is an example of a singleton:

class Singleton
{
public:
    // this is a lazy evaluated singleton
    Singleton GetInstance()
    {
        static Singleton instance;
        return instance;
    }
private:
    Singleton() {};  // code for constructor goes here
    // make sure these are here to stop the object from being copied
    Singleton(const Singleton &);
    void operator=(const Singleton &);
}
NathanOliver 429 Veteran Poster Featured Poster

Well you can have the StoneAdventure class hold a vector of rooms and then you can declare room to be a friend of StoneAdventure.

NathanOliver 429 Veteran Poster Featured Poster

Is it not working? It should work but I see you have the code commented out. Why is that? Here is a good reference for using sort.

NathanOliver 429 Veteran Poster Featured Poster

What is Error_code? Is the the enum type? Can you post your class?

NathanOliver 429 Veteran Poster Featured Poster

You are running Visual Studio 2012 which is version 11. version 11 has little to no c++11 support. You need to go to version 2013 update 3 to get te best support that Microsoft offers.

NathanOliver 429 Veteran Poster Featured Poster

Are you using Visual Studio 2012? I don't know of a Visual Studio 12 unless you are talking about version 12. If you go to Help -> About you will get a window like the attached. Post it here.

NathanOliver 429 Veteran Poster Featured Poster

What compiler are you using? MSVS 2013 does not have an issue with it. Also if you want to intialize number with 4 using braces you would use int number{4};

NathanOliver 429 Veteran Poster Featured Poster

Move line 19 to be between lines 6 and 7.