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

@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

@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

@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

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

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

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

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

@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

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

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>

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

Now that we have C++11 there are two options when you want an array object and don't want to use a native array. If you want an array of a fixed size you should use a std::array. This has the benefit over native arrays where if you pass it to a function you no longer lose the size of the array as it doesn't decay into a pointer.

If you need dynamic memeory allocation(ie you don't know how many things you are going to need to store) then you want to use a std::vector. A vector can resize durring runtime and handles all of the memory managment for you.

NathanOliver 429 Veteran Poster Featured Poster

More than likely cygwin is using some command to compile and that command doesn't change and either needs to be changed by you or can't be changed causing this issue. Take a look at this SO post on compiling with cygwin: http://stackoverflow.com/questions/1753239/compile-c-with-cygwin

NathanOliver 429 Veteran Poster Featured Poster

You should never include a .cpp file in another file. .cpp files should include .h/.hpp files and then all of those .h/.hpp and the .cpp file get compiled together to make an object file. This should happen for each .cpp file you have in your project. Once that happens then all of those objects should be linked together by the linker to make the final executable.

NathanOliver 429 Veteran Poster Featured Poster

@rubberman According to the standard 3.6.1.5 main has an implicit return 0; if one is not provided

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
return 0;

NathanOliver 429 Veteran Poster Featured Poster

1) Open program to write code
2) Write code
3) Compile code
4) If you have compiler errors fix them and go to step 3 else continue
5) Run code and make sure you get what you want else go to step 2

NathanOliver 429 Veteran Poster Featured Poster

Why dont you compile it and find out?

NathanOliver 429 Veteran Poster Featured Poster

Why dont you run it an find out?

NathanOliver 429 Veteran Poster Featured Poster

@serpi90 I think you are confusing how derived objects get created. If you have:

class A
{
    A() { /*do super complicated stuff in here to set up A*/ }
    virtual void Foo();
};

Class B : public A
{
    B() : A() { /*set up the B object here*/  }
    void Foo();
};

Class C : public A
{
    C() : A() { /*set up the C object here*/  }
    void Foo();
};

As you can see the derived object calls the base class constructor which handles constructing the base part of the derived object and then the B constructor takes care of setting the stuff up that belongs to B. The point of marking function virtual is so you can do this:

vector<A*> data;
data.push_back(new B());
data.push_back(new C());

for (auto& e : data)
    e->someFunc();

Here the right function gets called in the for loop. As you can see though we still have to construct the object with the right constructor.

NathanOliver 429 Veteran Poster Featured Poster

Why would you have a virtual constructor? The reason you use virtual is to create a virtual table so that when a function is called from a pointer to a base it will call the function of the derived object that was stored in that pointer. Since you have to explicitly call the constructor when you create an derived object there is no reason to have it virtual. The destructor needs to be virtual though since you are calling destroy on the base pointer and the program needs to know what derived object destructor to use.

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

Did you try to do this with pseudocode to see what the operations are? The folowing pseudocode should help you with how you need to write the program.

number age = 0,  max = 0;
string name, oldest
while(true)
    get name
    if name == "quit"
        break
    get age
    if age > max
        max = age
        oldest = name
end while
display << "the oldesy person is " << oldest
NathanOliver 429 Veteran Poster Featured Poster

This is not a code writing service. If you have code and it is giving you a problem then post the code and what the problem is. No one here is going to do your work for you.

rubberman commented: :-) +13
NathanOliver 429 Veteran Poster Featured Poster

@rubberman Microsoft does offer an express version of MS SQL Server that is free. I am looking at that right now for some time and attendance software that needs a DB for the backend. You can check it out here.

rubberman commented: I knew, but forgot about that. Thanks. +13
NathanOliver 429 Veteran Poster Featured Poster

Now if he offered to pay for this that would be a different story. I don't know about the rest of you but I think if the OP offered $1000 USD per hour at a minimum of 8 hours we would be more inclined to help.

NathanOliver 429 Veteran Poster Featured Poster

Just another person hoping that the internet is the answer to all of lives problems.

triumphost commented: Lol. I like this quote. Stealing it! +8
NathanOliver 429 Veteran Poster Featured Poster

If our output should be:

Popping...a
Popping...b
Popping...c
Popping...d
Popping...e
Popping...f

Then you need to be storing a,b,c,d,e and f in your stack. right now you are storing 1,2,3,4 and 5 with char a='1',b='2',c='3',d='4',e='5',f='6';. You can change it to char a='a',b='b',c='c',d='d',e='e',f='f'; to get the desired output. The stack is only going to give you what you put into it.

As a side note cout<<mystack1.pop(char)<<endl; makes no sense and should give you a compiler error. If you are trying to cast to a char then you and use cout<<(char)mystack1.pop()<<endl; or since this is C++ `cout<<static_cast<char>(mystack1.pop())<<endl;

TObannion commented: That's what I was looking for. As for "f", it's just an extra value to demonstrate when the stack is full. Thanks so much! +0
NathanOliver 429 Veteran Poster Featured Poster

@ddanbe: A char is an integer type so it can be implicitly converted to an int. Since sizeof(char) <= sizeof(int) there is no loss of precision. C++ uses 5.2.4.2.1 from the C standard for its integer sizes. http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf Page 22.

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

Compare each element entered by the user to the previous input. If the number is smaller then set it to min. Otherwise keep the min you already have. Since you don't know what min should be on the first entry you either need to set min to the first entry and then compare each subsequent iteration or you can set min to be the maximum number possible. You can get the largest possible number for a type by using:

int min = std::numeric_limits<int>::max()
ddanbe commented: Well explained. +15
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

Go through the string and change each letter to uppercase. You can use toupper() to make life easy.

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

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

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

Is the power cord plugged in?

Is the power turned on?