Agni 370 Practically a Master Poster Featured Poster

I can't really pin-point the error from this much code but you can try some general tips first to improve your code so that the problem can be easily identified.

For all classes with pointers define a copy-ctor, assignment operator and destructor to properly manage copying of pointers, freeing of memory etc.

If you are returning NULL from CopySubtree and assigning it to Root, it is possible that somewhere you use it without checking. So make sure you are not doing that.

Don't use reinterpret_cast unless you totally know that you cannot do without it. It usually means that the design is flawed and you should try and improve that. I don't really understand reinterpret_cast and stay away from it.

Once you do all this it might be easier to locate the error. If possible you can create a smaller compilable version of the code which reproduces the error and then people here will be able to execute it and provide better inputs.

Agni 370 Practically a Master Poster Featured Poster

1->// only works if "using namespace std" exists, what's the problem?

Because you have not scoped 'string'. If you don't specify, using namespace std, then you have to scope all namespace types with std:: .

2-> I don't see a problem with copying of vectors of strings bu why have you declared it like

contact& contact::operator=(const contact& record);

instead of

contact& operator=(const contact& record);

in the header file?

3-> The GetInfo function according to me should be defined like

const ItemType& GetInfo()

and should return Info. Why are you passing it a reference, which should be passing to some object, and then actually overwriting that object by the value of Info ?

Agni 370 Practically a Master Poster Featured Poster

Can you do something like

ifstream dbfile(DB_FILE_NAME);
	
if (dbfile.is_open()) {
	// functions are performed here.
} 
else {
        ofstream newdbfile(DB_FILE_NAME);
	cout << "You have no database file. Creating one now..." << endl;
	dbfile << "# This is your database file.\n";

	// Lots of irrelevant code omitted
	newdbfile.close(); // This is only here because I was trying to see if it would cause the file to write. It isn't writing when it closes, so I'm clearly having problems with this.
}

So if a file is not found you create a new one using ofstream. There might be a better way to do this may be.

Agni 370 Practically a Master Poster Featured Poster

Even though the thread is marked solved I would like to point out that your earlier code was much nicer. When you changed it you added a few things which are better avoided, like eof() and system("pause"). If you want to know why, read the link system("pause") and there is a similar article there on use of eof.

Agni 370 Practically a Master Poster Featured Poster

Why are you creating the istringstream object in between? Just print the string. And move the cin.get outside the loop.

Agni 370 Practically a Master Poster Featured Poster

Ok what I meant is when you are calculating max , store the maximum of each city in an array, like int maxim[number of cities] and then use that array to check if it has all same elements or different.

Agni 370 Practically a Master Poster Featured Poster

So basically all you want to check is the most frequent bird in all cities was same ?
Instead of having 'maxim' as in integer you should declare it as an array and populate the maximum value of each city in the array and then check if each element in the array is same. something like

int maxim[number of cities]
for(int i=0;i<number of cities;i++){
     int maximum = getMaximumForCity(i);
     maxim[i] = maximum;
}

//maxim has the maximums for all cities, now just iterate and compare each value with previous value.

You can use std::algorithm also but I'm not sure if you have studied those so I'm suggesting the simplest way to go about it.

Agni 370 Practically a Master Poster Featured Poster

Can you try and explain the problem a little more? I can make out that you populate an array of cities and an array of species but what are you doing with the array 'x' (as an aside 'x' is not a useful name for a variable, please give it a name which makes some sense) ? And what do you want to compare when you have the maximum values?

Agni 370 Practically a Master Poster Featured Poster

It's too abstract for anyone to help. I am not an expert at this stuff but if you need good feedback then post whatever code you have written and clearly mention the issue.

Agni 370 Practically a Master Poster Featured Poster

<Ignore > When you do data[index] if the value of index is greater than the size of vector you can get a out-of-range error. Ensure that you are inserting at valid index or resize your vector </ignore>

Sorry I did not see that you posted the output at the top and the problem was coming at some other point. But the explanation remains same , the value of 'i' is going beyond the range of vector at some point. Use vector.size() to limit value of 'i' may be.

Agni 370 Practically a Master Poster Featured Poster

Look at string class reference at string cpp. The functions find and erase might be helpful in finding a whitespace and then erasing the character at that location.

Agni 370 Practically a Master Poster Featured Poster

That sounds very abstract for anyone to help you. How do you know you have memory issues? I can't make out any memory issues from the code snipped you have given here. You should though check if your iterator points to end() before you dereference it.

Agni 370 Practically a Master Poster Featured Poster

If you just want to read from a file and print it to the output stream you can do

std::string line;
while(getline(infile,line)){
        std::cout << line << std::endl;
	//outfile << line << std::endl; or this to write to output file
}
Agni 370 Practically a Master Poster Featured Poster

What errors?

Agni 370 Practically a Master Poster Featured Poster

Is it because you don't have {} after the for statements in both the cases? Without the braces only the next line is part of for loop. And you will also need a

data << "\n";
Agni 370 Practically a Master Poster Featured Poster

If you need a dynamically increasing array of structures you can use a vector. Vector is an STL container which will do all the memory management itself and you can add whatever number of elements to it without having to worry about allocating-deallocating memory. Read more here vector. I'm not sure about your matrix requirement but may be you can use a vector of vectors.

Agni 370 Practically a Master Poster Featured Poster

Is it mandatory to use arrays? If not you can look at vectors or other STL containers to make your life easier.

Agni 370 Practically a Master Poster Featured Poster

-- Double post --

Agni 370 Practically a Master Poster Featured Poster

Declare an integer counter at the start of the program, increment it every time you change case, at the end of while loop read the value of the counter.

To post code use the code tags

. This will properly indent your code and make it easier to read.

Agni 370 Practically a Master Poster Featured Poster

Welcome to DW .. Sure you will have a great time here !! Enjoy :)

Agni 370 Practically a Master Poster Featured Poster

Is your code even compiling? The first error I see is on line 20 where the array size is a non-const integer.

Agni 370 Practically a Master Poster Featured Poster

I think your first link is pointing to some other page and not the one on plancast. Good interview and well time to check out plancast !

Agni 370 Practically a Master Poster Featured Poster

autocar is a pointer but autocar[i-1] refers to a struct and so you should use '.' to access its functions, attributes.

Agni 370 Practically a Master Poster Featured Poster

Why don't you write some code and test it yourself?

Agni 370 Practically a Master Poster Featured Poster

I think you're just not closing the while loop started on line 85.

Agni 370 Practically a Master Poster Featured Poster

This just means that Apple could end up using their devices as some sort of surveillance device and raises a number of privacy issues, one of them being pointed by cscgal above. To be able to distinguish between users they will have to store a lot of information about the real owner of the phone and how they use the phone on a regular basis. Overall it sounds very fishy and definitely has some hidden motives.

Agni 370 Practically a Master Poster Featured Poster

Are you sure that the pointer returned by mimeData(modes) cannot be null? If not then you should probably test for NULL before inserting the pointer into the list. Also whenever you store pointers in a container you have to be careful that you do not in some other part of the code delete the object through some other pointer and then later try and use the stored pointer.

Agni 370 Practically a Master Poster Featured Poster
Ancient Dragon commented: Great link :) +31
Agni 370 Practically a Master Poster Featured Poster

Then why are you declaring the string array dynamically? Since you already know the size of the array at compile time just say

string grade[SENTINEL];

and then add elements to the array like

grade[0] = "Pointers";

etc..

Agni 370 Practically a Master Poster Featured Poster

I would do this

#include <iostream>

class Rect{
        public:
                enum UNIT {CENTIMETER,METER};
                Rect(double len,double wid,UNIT unit){
                        if(unit == CENTIMETER){
                                length=(len/100);
                                width=(wid/100);
                        }
                        else{
                                length=len;
                                width=wid;
                        }
                        std::cout << length << ":" << width << std::endl;
                }
        //rest of implementation
        private:
                double length;//in meters
                double width;//in meters
};      

int main(){

        Rect obj1(100,100,Rect::CENTIMETER);
        Rect obj2(10,5,Rect::METER);
}
Agni 370 Practically a Master Poster Featured Poster

'new' returns a pointer to the allocated memory so the variable should be a string* not a string.

Agni 370 Practically a Master Poster Featured Poster

yes job interviews, what a nightmare.. but well you're on another world all together :) !!

OP sorry for this digression

Agni 370 Practically a Master Poster Featured Poster

lol ..@mike you have obviously not given many interviews. I know people are supposed to be using strings and forget about char arrays, pointers, null terminated etc, but that's probably the most sought after questions in an interview and they make you write all sorts of string reversal, copy,strchr, palindromes blah blah to test the pointer stuff and well I always find it so difficult to conjure up these in an interview.

so my advice to the OP, please do spend time understand char *, char [] etc :)

Agni 370 Practically a Master Poster Featured Poster

one thread try to read from vector and another thread try to delete from vector how to do it

As a forum policy we do not give code on a platter. If you want help, show some effort, post what you have tried and people will try and guide you.

ZlapX commented: sucks balls +0
Agni 370 Practically a Master Poster Featured Poster

You must have declared a variable as the type vector<string>, return that and change the function prototype to reflect the return value as vector<string>.

Agni 370 Practically a Master Poster Featured Poster

I don't exactly know the reason for this error, but few things which you can correct in your code

1. I don't see the header file include in the .cpp file
2. In the definition of prompt_values and disp_point in the .cpp files you have a ';' after the ().

apart from this it seems correct. You have also not provided any definition for the ctor of your class.


EDIT: beaten by a couple of guys here !!

Agni 370 Practically a Master Poster Featured Poster

@mike, that does clear quite a few things. Thanks and welcome to Daniweb !!

Agni 370 Practically a Master Poster Featured Poster

The default constructor of istream_iterator points to eos (end of stream) and copy goes from the first iterator to second and copies to the ostream_iterator. So, assuming that the eos charachter is newline, once I hit enter the copy should stop because the second iterator is reached.

@Nick i find it strange that we have to use ctrl_z to end this, because this is given at a lot of places as the code to copy strings from standard i/p directly into vectors or file. ending this with ctrl_z defeats the purpose i guess. Check this link for many such examples, but none of them work for me :( link

Agni 370 Practically a Master Poster Featured Poster

@mike_2000_17 you are assuming that this is an exercise. what if he wants to put this code in a project or in a bigger code?

Agni 370 Practically a Master Poster Featured Poster

if you are not averse to using standard generic algorithms, you can look at 'count' in <algorithm> header file.

Agni 370 Practically a Master Poster Featured Poster

We could declare it as mutable.

Agni 370 Practically a Master Poster Featured Poster

I think if you can understand why it is happening you will be able to fix it yourself. If you try and dry run the 2 loops in your mind:

iteration 1:
outer loop, i = 0
inner loop j = 0 - max
compare a[0] (1) with a[j] and increment count if equal
print a[0] and count
iterator 2:
outer loop i = 1
inner loop j = 0 - max
compare a[1] (1) with a[j] and increment count if equal
print a[1] and count

so basically even though you count the # of occurrences of 1 in the first loop you still go to a[1] and that is also equal to 1 and then you repeat it for all the 1's and so on.

Once you compare a number you should remove all its instances from the array, to avoid recounting. You might find it easier to do this with vector or even a list.

Agni 370 Practically a Master Poster Featured Poster

Sorry, your answer is not clear. I do press enter but it takes in one line, prints it out and then waits for another instead of terminating.

Agni 370 Practically a Master Poster Featured Poster

Hi,

I'm using istream_iterator to read input from standard i/p and writing it to standard o/p using ostream_iterator. I expected it to print the input to the console when I hit enter and then exit but it loops for next input after printing and so on.

#include <iostream>
#include <iterator>
#include <algorithm>

int main(){
        copy(std::istream_iterator<std::string>(std::cin),
                std::istream_iterator<std::string>(),
                std::ostream_iterator<std::string>(std::cout," "));
}

-thanks

Agni 370 Practically a Master Poster Featured Poster

No it isn't. I would suggest that you read some tutorial on vectors, iterators to understand these in detail or else you will face these problems every now and then.

Agni 370 Practically a Master Poster Featured Poster

C is exactly same as B so it is also wrong for the same reason. and in D '1' is an integer and you are trying to initialize an iterator with an int. btw when you compile these, the compiler must be giving some meaningful error messages, you should read them carefully.

Agni 370 Practically a Master Poster Featured Poster

You need to link to function.o object file to your code. if you are using g++ you use the -c option to create the .o file and then link the .o's together to create the executable.

Agni 370 Practically a Master Poster Featured Poster

in B and C you when you say inventory[1] , it is a string there is no conversion from string to type iterator. And I'm not too sure what you are trying to accomplish in D by setting iterator to 1 ?

Agni 370 Practically a Master Poster Featured Poster

What is the error?

Agni 370 Practically a Master Poster Featured Poster

Have you tried looking at bitwise operators? See if you can use bitset class, representing each position as a bit with 0 representing OFF and 1 representing ON