NathanOliver 429 Veteran Poster Featured Poster

Argh. Thanks fo spotting the typo.

NathanOliver 429 Veteran Poster Featured Poster

What a great post Mikael. I never though about sorting derived objects in the container so that the same function keeps getting called. I would suppose this would also help with vtable lookups?

I do have a question regarding precomputation of values. Lets say that I have a std::vector<std::vector<int>> where the nested vector can be of different sizes and I need to visit every element. Since ranged based for loops get converted to using iterators would you use:

std::vector<std::vector<int>> values;
for(auto& row : values)
    for(auto& val : values)
    { 
        // code here
    }

Or would you compute the size of each nested vector and use those resaults

std::vector<std::vector<int>> values;
std::vector<size_t> sizes;

for (auto& row : values)
    sizes.push_back(row.size());

for (size_t i = 0; i < values.size(); ++i)
{
    for (size_t j = 0; j < sizes[i]; ++j)
    {
        //code goes here
    }
}
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 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

1) Open program to write code
2) Write code
3) Compile code
4) If you have 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

Move lines 25-32 to be before main()

NathanOliver 429 Veteran Poster Featured Poster

You would create a function like what I have shown. Then instead of having lines 27-31 you would just call that function passing the array.

NathanOliver 429 Veteran Poster Featured Poster

The problem is that cin.get(response); is leaving an '\n' in the buffer so when you call cin.get(c); at the beginning of the loop it pulls in the '\n'. Since '\n' stops the inner loop it never gets executed and then you are asked to continue again. To fix this you need to get rid of the '\n' in the buffer. You can use a simple hack and use cin.get() after cin.get(response); which will eat the '\n' and you can enter another sentance. I prefer to use ignore() and I use it as

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
NathanOliver 429 Veteran Poster Featured Poster

Doing a linear search through a list or an array to find the minimum is O(N) or linear time. If you were to sort and then grab the first element using either qsort or std::sort that would be O(Nlog2(N)) which is log(N) times more inefficient. The only way to get min or max in a more efficient way than O(N) that I know of is to use a container that sorts itself when elements are entered. Then you either get the fist or last element from the container which is generally O(1) or constant time. check out set for a sorted container. You are paying for that constant time though as the set is sorted with every element that get entered.

NathanOliver 429 Veteran Poster Featured Poster

You need to have a void function that displays the area of the circles in the array. It should be like

void DisplayArea(Circle[NUM_CIRCLES] circles)
{
    for (int index = 0; index < NUM_CIRCLES; index++)
    {
         cout << "Circle " << (index+1) << setw(8)
                << circle[index].findArea() << endl;
    }
}

Then you main function will get changed to:

    //...
    cout << "\nHere are the areas of the " << NUM_CIRCLES
     << " circles.\n";
    DisplayArea(circles);
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

@Zee Khan - Start a new thread, don't resurrect a 6 year old thread. You should try to see if you can do it first and then if it doesn't work then ask for help with the code you have.

NathanOliver 429 Veteran Poster Featured Poster

Start a new thread and post the code that you have and the problem it is having.

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

What is the value of count when you exit for (; x <= userMax;)?

NathanOliver 429 Veteran Poster Featured Poster

What part are you having trouble with?

NathanOliver 429 Veteran Poster Featured Poster

@phony You need to do the check while you are getting the input from the user. Otherwise you need to iterate through the contents of intList and find the smallest element. As an example this this how you would find the smallest value in an array of integers:

int data[arraySize] // arraySize is some size for the array
// fill data with values

int min = data[0]; // set min to the first element and then check the rest
// start at 1 since min is already data[0]
for (int i = 1; i < arraySize; i++)
{
    if (data[i] < min) // if the element is smaller then min then make min that value.
        min = data[i];
    // otherwise we do nothing and let the loop continue.
}
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

Sounds like this is something you should do yourself. If you have code and you are having problems with it then post the code you have and what problem you are having. If it is a compilation error included the error from the compiler and what line it is referring to.

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

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

To get a double one of the numbers needs to become a floating point number so the compiler knows to do floating point math.

bmi = static_cast<double> (703 * weight / (height*height)); //BMI calculation

becomes

bmi = 703.0 * weight / (height*height); //BMI calculation

As an FYI if none of your number a literals you can use c style cast you c++ cast to convert one of the number of the calculation to a double.

bmi = static_cast<double>(weight) / (height*height);
//or
bmi = (double)weight / (height*height);
NathanOliver 429 Veteran Poster Featured Poster

main() needs to be moved from line 105 to line 6. It should also be int main(). main() should always return an int. You also need to get rid of lines 108-110. You should also add a break after case 1.

NathanOliver 429 Veteran Poster Featured Poster

What happens if you run the code? I can tell you 5%3 is 2 not 1. % works as 5 - ((5/3) * 3) or a%b = a - ((a/b) * b). Line 4 should be 7 based on order of operations.

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

Are you having an issue with your code are are you just showing us it? If you are having a problem explain what it is. If you are having compiler errors tell us that the error is and what line it is happening on.

NathanOliver 429 Veteran Poster Featured Poster

Is the power cord plugged in?

Is the power turned on?

NathanOliver 429 Veteran Poster Featured Poster

@ Nimra_1

1) Dont post your own question in a different thread.
2) We dont do homework for others
3) Read the Rules

NathanOliver 429 Veteran Poster Featured Poster

It would only be fair. Unless he wants to pay for the work to be done. I'll offer my services for $250 USD per hour.

NathanOliver 429 Veteran Poster Featured Poster

When you write FILE *out , *in you can be translated to FILE *out; FILE* *in. When using , in a function call it is used to separate the paramaters that are being used in the function. With that fclose(in, out) translates to call function fclose that takes 2 FILE pointers. Since that function doesn't exist you get a compiler error. If you really want that functionality you can write your own function template that uses variadic templates that will call fclose() on all of the paramaters in the pack.

NathanOliver 429 Veteran Poster Featured Poster

Which means it is a program that loads on startup. That still doesn't changethe fact that it is not working.

NathanOliver 429 Veteran Poster Featured Poster

Navigate to the folder location the error gave you. progra~3 would be something like program files, program files(x86), program data.

NathanOliver 429 Veteran Poster Featured Poster

There is an entire standard header file full of functions to do this. Check out ctime.

NathanOliver 429 Veteran Poster Featured Poster

The jist of the issue is >> leaves the "enter key" in the input buffer. If you don't remove it before getline() is used then getline() will see the "enter key" and grab it. Since getline() reads until it sees an "enter key" and then returns you wont get anything.

BTW when I say "enter key" what really is in the buffer is a newline character. with text based programing it is often assumend that once a newline is encountered it is the end of the input since the only easy way to insert a newline into the input buffer is to press enter.

NathanOliver 429 Veteran Poster Featured Poster

This isn't really a C++ problem. Try uninstalling the program and reinstalling the program that uses EEDE77.ccp

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

fclose() is declared as int fclose ( FILE * stream );. As you can see it only takes one FILE*. So the answer to your question is no.

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!";