NathanOliver 429 Veteran Poster Featured Poster
NathanOliver 429 Veteran Poster Featured Poster

For three integers you can hardcode it fairly simply as

int a, b, c;
std::cin >> a >> b >> c;
int positiveCounter = 0;
int negativeCounter = 0;
if (a < 0)
    negativeCounter++;
else
    positiveCounter++;
if (b < 0)
    negativeCounter++;
else
    positiveCounter++;
if (c < 0)
    negativeCounter++;
else
    positiveCounter++;

std::cout << positiveCounter << " positive integers are entered\n";
std::cout << nagativeCounter << " negative integers are entered\n";

If you wan to do this for an indefinte number of variables then you should write a function to to this for you. You would get all off the input into some sort of data structure (array, vector, set, list, ...). Then you would pass that to a function which would do the counting. Tha function would look like

template<typename ForwardIt>
std::pair<int, int> CountPosativeAndNegative(ForwardIt it, ForwardIt end)
{
    std::pair<int, int> returner{0,0};
    while(it != end)
    {
        if (*it < 0)
            returner.second++;
        else
            returner.first++;
        ++it;
    }
    return returner;
}
NathanOliver 429 Veteran Poster Featured Poster

Well if we use integer math then the answer would be

#include <iostream>

int main()
{
    std::cout << 1;
    std::cin.get();
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

Derived classes only need to construct the classes the directly derive from. If those classes derive from something else then it the responsibility of those classes to construct what classes the directly derive from. Take the follwing example

class Foo {};

class Bar : public Foo
{
public:
    Bar() : Foo() {}
};

class Foobar : public Bar
{
public:
    Foobar() : Bar() {}
};

In Foobar we call the constructor for the Bar part but not the Foo part as that is Bar's responsibility. Bar then handles creating the Foo part and everything is good.

NathanOliver 429 Veteran Poster Featured Poster

Memeber functions are members of a class. Because of that they are not the same as free functions when it comes to function pointers.

void (*ptr)(void);

Will match any function that returns void at takes no parameters that is not a memeber function.

void (A::*ptr)(void)

Will match any function that returns void and takes no parmeters that is a memeber of A.

The reason for this is

void show(void)

Actually has a hidden parameter in it as non static class memeber functions take in the this pointer from the instance that calls it.

NathanOliver 429 Veteran Poster Featured Poster

What is your question? 26,760,055 bytes is only 26.7 MB which is not very large in todays world.

NathanOliver 429 Veteran Poster Featured Poster

As Bjarne Stroustrup has said

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

NathanOliver 429 Veteran Poster Featured Poster

The first issue is your comparison operator is not folowing the strict weak ordering that std::set requires. You are only comparing L when you need to check if the L's are equal and if they are then comapre against r.

bool operator<(const _point &a)const
{
    if (L == a.L)
        return r < a.r;
    return L < a.L;
}

The second issue you have is that erase() invalidates the iterators since they point to the location you erased. If you are going to erase like this you will need two other iterators to point to the next item in each set. then after the erase you can assign those pointer back to the ones you just erased.

if((*it_up).L+(*it_down).L!=c1||(*it_up).r+(*it_down).r!=c2||(*it_up).r+(*it_down).L!=d2||(*it_up).L+(*it_down).r!=d1) 
{
    set<_point>::iterator up_temp = std::next(it_up);
    set<_point>::iterator down_temp = std::next(it_down);
    up.erase(*it_up);
    down.erase(*it_down);
    it_up = up_temp;
    it_down = down_temp;
}
NathanOliver 429 Veteran Poster Featured Poster

You might be able to use a std::mutex for this. You might also want to rethink your data structure. You could have all of the thread writing to a thread safe queue and the reader could be processing it at the same time. This might make your execution faster.

NathanOliver 429 Veteran Poster Featured Poster

You could do this fairly easily with a stringstream and getline(). The split function would look like:

std::vector<std::string> split_string(const std::string & line, char delim = ' ')
{
    std::vector<std::string> sep;
    std::string token;
    std::stringstream ss(line);
    while(getline(ss, token, delim))
        sep.push_back(token);
    return sep;
}

And then you could use it like

int main ()
{
    std::vector<std::string> sep = split_string("Name|Phone Number|Account Number|Price|Tariff|6079777", '|');
    for(const auto & e : sep)
        std::cout << e << std::endl;
    return 0;
}

See this Live Example

NathanOliver 429 Veteran Poster Featured Poster

Um 1, 4, 6, 12, 15, 20, 30 and 60 are not prime factors. the prime factors of 60 are 2, 2, 3, 5. You can prove that since 2*2 = 4 and 4 * 3 = 12 and 12 * 5 = 60

New Jack commented: But it seems that you didn't look at the problem clearly. +0
NathanOliver 429 Veteran Poster Featured Poster

More than likely one of the headers that you are including in your code is including that header. This is something you should NOT rely on. You should explicitly include all headers needed to compile your code and not rely on something else including it for you. A different compiler might not include that header for you and then your code will fail to compile making it non portable.

NathanOliver 429 Veteran Poster Featured Poster

You are not allowed to define a function in a function and that is what the compiler is complaining about. There are two solutions to this. You could move the function out of main and put it in the global scope or you can change the function into a lambda which is allowed at the local scope.

#include<iostream>
// global function decleration
void hello()
{
    std::cout << "HI";
}

int main()
{ 
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}

or

int main()
{ 
    auto func = []() { std::cout << "HI"; };
    func();
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

You are still using the wrong overload for std::transform. Since you are modifying the string inplace you need the overload that takes the range to work on and the iterator to where to insert.

transform(theReadMessage.begin(),theReadMessage.end(),
          theReadMessage.begin(),theReadMessage.end(),
          [](unsigned char ch){ return tolower(ch);});

Should be

transform(theReadMessage.begin(),theReadMessage.end(),
          theReadMessage.begin(),
          [](unsigned char ch){ return tolower(ch);});
NathanOliver 429 Veteran Poster Featured Poster

reading the answer here it looks like you can register an event handler with the OS and handle the closing of the cmd window youself. in the closing routine you could cleanly close out the API so it usable the next time.

NathanOliver 429 Veteran Poster Featured Poster

And the reason you posted this again instead of using the wonderful advice Moschops gave you? If you need to follow up with you question please use: https://www.daniweb.com/software-development/cpp/threads/498358/game-on-c-

NathanOliver 429 Veteran Poster Featured Poster

@nullptr you should really be using a unsigned char as any other value is UB. http://en.cppreference.com/w/cpp/string/byte/tolower

NathanOliver 429 Veteran Poster Featured Poster

As Moschops said you are moving the data from p so trying to use it after that results in your segfault. std::unique_ptr doe have an operator bool() that will return true if get() != nullptr. Using that we can protect against using the pointer if it no longer manages a pointer.

#include<iostream>
#include<memory>

using namespace std;

class Test
{
public:
    Test(int a = 0) : m_a(a)
    {
    }
    ~Test()
    {
        cout << "Calling destructor" << endl;
    }
public:
    int m_a;
};


//***************************************************************
void Fun(std::unique_ptr<Test> p1)
{
    cout << p1->m_a << endl;
}
//***************************************************************
int  main()
{
    std::unique_ptr<Test> p(new Test(5));
    Fun(std::move(p));
    if (p)
        cout << p->m_a << endl;
    else
        cout << "p is empty";

    return 1;
}

Live Example: http://coliru.stacked-crooked.com/a/398497798ee1825f

As a rule of thumb if you move() something you should no longer use it.

NathanOliver 429 Veteran Poster Featured Poster

@vijayan121 I just wanted to let you know about this other online compiler with assembly view: http://goo.gl/64kMZO

mike_2000_17 commented: awesome online compiler! +15
NathanOliver 429 Veteran Poster Featured Poster

Silly question but can't you have your teacher explain what you should have done not to have points taken off? It is hard for us to know what he is thinking.

NathanOliver 429 Veteran Poster Featured Poster

@ravenous just using size_t upper_case_count = std::count_if( password.cbegin(), password.cend(), std::isupper ); will not compile. If you use a lambda then it will.

NathanOliver 429 Veteran Poster Featured Poster

If you indent you code correctly we might be able to help you better.

NathanOliver 429 Veteran Poster Featured Poster

What don't you understand about it?

NathanOliver 429 Veteran Poster Featured Poster

I will have to agree with @Suzie999 on this. Once you get into the mindset of using const whenever you have a constant value then you should just keep on using it. Not only does it keep the that behavior front an center but it is someting you should always because it makes the type system work for you.

There is also another posible side effect of doing this. If the compiler can determine what the value is at compile time the compiler coudld subsitute the value of the calclation into an actual constant in the code. This would save you the runtime of the computation during runtime as well as the function call.

NathanOliver 429 Veteran Poster Featured Poster

Well if you store the password as a std::string then you can iterate over each character and check if it is upper or lower case with std::isupper and std::islower. To count upper case letters it would look like:

int uppercaseCounter = 0;
std::string password = "PassWord";
for (auto& e : password)
{
    if (std::isupper(e))
        uppercaseCounter++;
}
std::cout << uppercaseCounter;

See this Live Example

Ahmed91za commented: thanks +0
NathanOliver 429 Veteran Poster Featured Poster

No one is going to write the code for you. If write some code youself and you have an issue with it then post the code you have and what the issue is. also include any sample data and expected output. See http://sscce.org for more on writing a good code question.

NathanOliver 429 Veteran Poster Featured Poster

No one is going to write the code for you. If write some code youself and you have an issue with it then post the code you have and what the issue is. also include any sample data and expected output. See http://sscce.org for more on writing a good code question.

NathanOliver 429 Veteran Poster Featured Poster

You cannot use a switch case with a std::string. switch can only evaluate integer type expressions. See this for more information on switch: http://en.cppreference.com/w/cpp/language/switch

NathanOliver 429 Veteran Poster Featured Poster

And your question is?

NathanOliver 429 Veteran Poster Featured Poster

Help with which part?

NathanOliver 429 Veteran Poster Featured Poster

Don't. graphics.h comes from the old wild west days of C++ and is not portable/standard.

NathanOliver 429 Veteran Poster Featured Poster

So your university doesn't want you to post code to help sites but it is okay to get and use code from help sites?

NathanOliver 429 Veteran Poster Featured Poster

Its hard to tell you how to code it if we can't see your code.

NathanOliver 429 Veteran Poster Featured Poster

std::vector is a dynamically resizing array. This is one of the first standard containers you should learn how to use. It has a [] operator so you can use it like an array and get any element and it also has a size() function that will tell you how many elements are in it. You can read all about it here on cppreference

NathanOliver 429 Veteran Poster Featured Poster

If you have an unknow amount of data to read then you can read in each object/line from the file and store it into a vector. Lets say you have a file containing only integer data. You coud read that with:

std::vector<int> data;
std::ifstream fin("name_of_file.txt");

if(!fin)
    std::cout << "unable to open file!"
else
{
    int temp;
    while (fin >> temp)
        data.push_back(temp);
}
Slavi commented: This should be a code snippet =] +6
NathanOliver 429 Veteran Poster Featured Poster

We will not do your homework for you. If you have a problem with the code you have then post the code and what the problem is.

NathanOliver 429 Veteran Poster Featured Poster

Okay. All done. That wasn't to bad.

Did you have a question?

NathanOliver 429 Veteran Poster Featured Poster

Lines 8-12 will never be executed with the code you have. In your function you have:

if(b<=4)
    return b;
else 
    return go(b+1)*go(b-2)-(b*2);

So if b <= 4 then you return b;. is be is anything else then you return go(b+1)*go(b-2)-(b*2);. Anything after the if will never be executed as both paths out of the if statement exit the function.

NathanOliver 429 Veteran Poster Featured Poster

I doubt anyone is going to do that for free for you. There is a Jobs and Resumes forum where you could post this as a contract job and pay someone to do it for you.

NathanOliver 429 Veteran Poster Featured Poster

@Zoran_1 Your code is not C++. This is a question is asking about solving this with C++.

NathanOliver 429 Veteran Poster Featured Poster

Doing most things with standard containers involves using iterators. Even using the new ranged based for loops does. If you take for example:

for ( range_declaration : range_expression ) loop_statement

This becomes:

{
    auto && __range = range_expression ; 
    for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) 
    { 
        range_declaration = *__begin; 
        loop_statement 
    } 
}

Most things deal with iterators as it makes for generating portable function a lot easier. Virtually anything that implements a begin() and end() can be used in functions. The main cavet is that the type of iterator the function requires.

Source for code example: http://en.cppreference.com/w/cpp/language/range-for

NathanOliver 429 Veteran Poster Featured Poster

Sure. There are plenty of good book here and here you could use.

NathanOliver 429 Veteran Poster Featured Poster

It should be possible. It depends on how platform dependent the code bases are and how standard compliant the code is.

NathanOliver 429 Veteran Poster Featured Poster

@basit_3 please refrain from just giving code to homework vultures. If they have a problem with code helping them is fine. Just give me the code questions should be ignored.

NathanOliver 429 Veteran Poster Featured Poster

@basit do encourage menex's behavior. If they have a question they should ask there own question. Also you shouldn't answer question just asking for code.

NathanOliver 429 Veteran Poster Featured Poster

the >> and << operators are overloaded for char*. Since you have an operator char* defined for you class it is implicitly convertable into a char*. Since it is implicitly convertable you can use it with the default stream operators.

For your second question strcmp() reutrns an integer depending on how the string compare:

int strcmp ( const char * str1, const char * str2 );

Returns an integral value indicating the relationship between the strings:

<0   the first character that does not match has a lower value in ptr1 than in ptr2
0   the contents of both strings are equal`
>0  the first character that does not match has a greater value in ptr1 than in ptr2

so return (strcmp(ptr, other.ptr) == 0); says return the return of strcmp(ptr, other.ptr) comapred to 0. So if the strings are equal then strcmp(ptr, other.ptr) will return 0 and 0 == 0 is true so the return becomes return true;. if the strings are not equal it will return false.

NathanOliver 429 Veteran Poster Featured Poster

@nathan.pavlovsky All member functions of std::string reside in the <string> header. Here is a good reference that shows you everything in <string>

NathanOliver 429 Veteran Poster Featured Poster

No we will not give you the code for you assignment. This is called cheating and in any decent educational institution you should get expelled for that.

Now if you work on your own code and you run into a problem with it then you can post that problem here and we will help you with it. You still have to be careful with that as a lot of instructor will still consider that cheating as your assignments should be done by you and not someone else.

Please read this for further information.

NathanOliver 429 Veteran Poster Featured Poster

Define by what you mean with:

if I want to use string methods, I #include <string> right?

If you want to use anything release to a std::string or std::wstring then you need <string>. If you want to use things like strcmp(), strncat() and other from the C library they are in <cstring> and/or <string.h>

NathanOliver 429 Veteran Poster Featured Poster

string is defined in <string> and the defenition of string is typedef basic_string<char> string;. string is just a typdef and there is no actual string class in the standard. Whenever you want to use a std::string then you need to #include <string>