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 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

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 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

@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

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

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

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

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

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

The since you are using operator bool it will only be false when badbit and or failbit is set. The first time you close the file and evaluate operator bool none of those are set so the operator returns true. Calling close() on the stream a second time will fail since it is already closed and since the function fails the failbit is set. The next time you evaluate the stream using operator bool it returns false.

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

What no one helped you the first time so you are trying again? It's been 5 days and all you have come up with is "give me the code".

NathanOliver 429 Veteran Poster Featured Poster

I just want to point out that if part of the requirement is to not use any dynamic memory allocation you shouldn't be able to use a vector. A vector is just a wrapper for a dynamic array. I am being a little nit picky about that but your professor doesn't seam to care so vector FTW.

NathanOliver 429 Veteran Poster Featured Poster

And? Is there a problem or you just showing the final code? I do think while (MinutesOfWork > MinutesVisit); should be while (MinutesOfWork >= MinutesVisit);

NathanOliver 429 Veteran Poster Featured Poster

You should not be getting any input from the user. You need to figure out how many appointments the sales person can do per day. Once you know that then you know how many visits he can do in 2 weeks. Once you have that then you can multiply the 2 week total by 110% to find out the second 2 week amount. Add those two numbers together and now you have the total profit per month.

Most of programing is problem solving. Solve the problem manually and then translate those steps into code. using psuedocode is another way to help the transition from pen and paper to code.

ddanbe commented: extensive advise. +15
NathanOliver 429 Veteran Poster Featured Poster

You have to initialize the map. Take a look at this post As how you can do that with or without C++11 support.

NathanOliver 429 Veteran Poster Featured Poster

You have to initialize the static member outside of the class.

/Foo.h
class Foo
{
    static int bar;
    //...
};


// Foo.cpp
int Foo::bar = 1;

If the initialization is in the header file then each file that includes the header file will have a definition of the static member. Thus during the link phase you will get linker errors as the code to initialize the variable will be defined in multiple source files.

NathanOliver 429 Veteran Poster Featured Poster

I have to add that if you have a C++ 11 compiler then you should be using nullptr.

rubberman commented: Which is probably #define'd to 0... :-) +12
NathanOliver 429 Veteran Poster Featured Poster

backslashes should be \\ since \ is an escape character. Try:

infile.open("C:\\Projects\\C++\\infile.txt");
outfile.open("C:\\Projects\\C++\\outfile.txt");
NathanOliver 429 Veteran Poster Featured Poster

haveimage() returns true or false so if haveimage() returns true then if(inst->imgtest.haveimage()) becomes if(true) and the body of the if statement is executed. If it were false then if(inst->imgtest.haveimage()) becomes if(false) and the body of the if statement is not executed.

let me ask: why, sometimes, we use '*' and othertimes don't with 'this'?

I assume you are talking about return types when asking this question. this is a pointer to the object that the function belongs to. If the function is returning itself as an object like SomeObject foo() then you use
return *this so that you return a SomeObject and not a SomeObject* since * dereferences the pointer. If you had SomeObject* foo() then you would use return this

cambalinho commented: thanks for all +0
NathanOliver 429 Veteran Poster Featured Poster

You wouldn't have to write

if(inst->imgtest.haveimage()==true)

That could be expressed as

if(inst->imgtest.haveimage())

When using an IDE that has auto-complete it is even faster to write.

I also want to stress that if you are using a C++11 compliant compiler to stop using NULL and start using nullptr. Now that we have nullptr there is no reason to ever use NULL.

cambalinho commented: thanks for that +0
NathanOliver 429 Veteran Poster Featured Poster

Well if the default construction of an image leaves the image in an invalid state you could use:

if(inst->imgtest != image())

this will test imgtest against an default object. As I said earlier NULL only makes sense to test for a valid pointer but you should use nullptr if you can. I don't see why you wouldn't just write an isValid() function.

NathanOliver 429 Veteran Poster Featured Poster

Is imgtest a pointer? I am guessing it is not because of the error :

"ambiguous overload for 'operator!=' (operand types are 'image' and 'int')"

If a type is not a pointer you should not try to comapre it to NULL. NULL is just #define NULL 0 so anywhere you have null it just gets substituted with 0. With C++ 11 you should use nullptr to check if a pointer is null. If you are trying to check that imgtest is valid then I would suggest you write a function in you class that will check the validity of the object and return true or false. Then you cold write somethin like:

if(inst->imgtest.isValid())
cambalinho commented: thanks for all +3
NathanOliver 429 Veteran Poster Featured Poster

@search_not I made a mistake in the corrected for loop. The code should be

for(unsigned i = 0; i < data->size(); i++){
    appendTextToEdit(hEdit, (data->at(i)).c_str());
}

That way you dont have to create the temporary string line.

NathanOliver 429 Veteran Poster Featured Poster

You need to use L before string literals now that you are using wstrings for lines 2-5.

Your for loop that is giving you your second error:

for(unsigned i = 0; i < data->size(); i++){
    std::wstring line = data->at(i);
    appendTextToEdit(hEdit, /*(LPCWSTR)*/ line);
}

Can be rewritten to be:

for(unsigned i = 0; i < data->size(); i++){
    appendTextToEdit(hEdit, line.c_str());
}
NathanOliver 429 Veteran Poster Featured Poster

Can you post the code that is giving you the problem?

NathanOliver 429 Veteran Poster Featured Poster

What is the type of read_in? If it an ifstream you need to make it wifstream. If you are going to write a program that supports unicode then you should convert the entire program to use unicode. all instances of string, ifstream, ofstream should become their wide counterparts.

NathanOliver 429 Veteran Poster Featured Poster

Yes.

NathanOliver 429 Veteran Poster Featured Poster

You are playing with undefined behavior. You should never go past the end of an array. Just because it worked doesn't mean it will always work. What you actually did was to write data into memory that p has no control over. The compiler does not check these conditions for you. This is why most people will say to use a std::array or std::vector that have at() member that check to make sure you are in bounds.

NathanOliver 429 Veteran Poster Featured Poster

Why not use std::wstring instead of std::string. Then you can call std::wstring.c_str() which will give you an wchar_t*.

NathanOliver 429 Veteran Poster Featured Poster

Is the power cord plugged in?

Is the power turned on?

NathanOliver 429 Veteran Poster Featured Poster

Well that's not good for you. We do not do your work for you. Show us what you have and what the problems you are having with it. Include any compiler errors as well.

NathanOliver 429 Veteran Poster Featured Poster

And you have done what so far? We do not do your homework for you on this site.

NathanOliver 429 Veteran Poster Featured Poster

Here is a listing of escape codes:

\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\ backslash ()

Ahmad Imran commented: so just a beepo +0
NathanOliver 429 Veteran Poster Featured Poster

Use an if statement and check to see if your files are actually open. You can do that with

if(!infile.is_open())
    cout << "Input file not opened!";
if(!outfile.is_open())
    cout << "Output file not opened!";
NathanOliver 429 Veteran Poster Featured Poster

Checking if the number is divisible by 3 or 5 can be switched around. The main thing is that you need to have the check for if it is divisible by 3 and 5 first otherwise the checks for 3 or 5 will trigger.

NathanOliver 429 Veteran Poster Featured Poster

I would think line 8 needs to be:

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=(c[c.size()-1])[0];
cambalinho commented: thanks +3
NathanOliver 429 Veteran Poster Featured Poster

What did you plug in for the missing values and what does your for loop look like now? Remember when you are doing the summation that if the number is divisible by 15 you add 15, if it is divisible by 5 you add 5, if it is divisible by 3 then you add 3, otherwise you add only 1. when I run my version of the program for 5 to 15 I get 39.

NathanOliver 429 Veteran Poster Featured Poster

@cambalinho The reason you are gettting that error is std::string.c_str() returns a char* to the c-string the string contains. A char and a char* are not the same thing. If you want just the first character from the c-string then you can use:

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=(j.c_str())[0];

// or

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=*(j.c_str());
cambalinho commented: thanks +0
NathanOliver 429 Veteran Poster Featured Poster

Is it working now? Also you shouldn't be changing the value of i. i should only be used for controlling the loop. You are adding 1,3,5 or 15 depending on what i is divisible by. The code should look like this:

if (i%3==0 && i%5==0)
    sum += ;
else if (i%3==0) 
    sum += ;
else if (i%5==0)
    sum += ;
else 
    sum += ;
NathanOliver 429 Veteran Poster Featured Poster
x<=i<=y

Is not valid c++. If you need to loop between all number in x and y then you can use:

for (int i = x; i <= y; i++)

Wich is translated to: start with i equall to x and loop while i is less than or equall to y increment i by one with every iteration.

NathanOliver 429 Veteran Poster Featured Poster

when something is evenly divisible by a number the modulo is 0.

15 / 3 = 5
15 % 3 = 0

Knowing this you can write the following code to see if something is divisble by a number:

if(someNUmber % 3 == 0)
    the number is divisble by 3
if(someNumber % 5 == 0)
    the number is divisble by 3
if(someNumber % 3 == 0 && someNumber % 5 == 0)
    the number is divisble by 3 and 5
NathanOliver 429 Veteran Poster Featured Poster

You can change your function to the following to take into account the filename.

bool operator() (finfo i, finfo j)
{
    if(i.fsize == j.fsize)
        return i.filename > j.file
    return (i.fsize > j.fsize)
}
NathanOliver 429 Veteran Poster Featured Poster

Your algorithm is a little wrong. Here is how you can do it (pseudocode):

string first, second;
// get the data for the string
for i = 0 to i < first size - second size
    if (substring(first[i], first[i+second size]) == second)
        counter++;
NathanOliver 429 Veteran Poster Featured Poster

line 5: void print(string *ar[]) which becomes
line 5: void print(string **)

line 23 print(&s) which becomes
line 23 print(s)

Since the name of an array is the address of the array. In your code if you put before line 23

cout << s;
cout << &s;

You should get the same value. If you want to get an array in your print function I would just write:

void print(const string * arr, int size)
{
    for (int i = 0; i < size; i++)
        cout << arr[i];
}