need some help again with finding the min and max from a text file
And where is your logic to calculate min and max? What do you think the algorithm should be on paper ?
need some help again with finding the min and max from a text file
And where is your logic to calculate min and max? What do you think the algorithm should be on paper ?
Yes. You are subtracting ascii value of char with ascii value of '0'
It seems like you want a subscriber-publisher model. There will be one publisher thread which calls a particular function on all subscribers every 60 seconds. The publisher can be a Singleton and subscribers could just subscribe with a callback function to it. However if there is only 1 publisher then it could be that a new foo object subscribes just a fraction of second before the publisher is going to wake up so the first call can be at any time after you subscribe. All other calls will happen at 60 seconds periods though.
Is there any way to have a pointer pointing to each line? Any way to implement it?
You could store the entire file as a vector of strings and then do whatever you want to do with them. Something like
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::ifstream repFile("file1.txt");
std::vector<std::string> fileVector;
std::string repLine;
while(getline(repFile,repLine))
{
fileVector.push_back(repLine);
}
for(int i=0;i<fileVector.size(); i++)
{
std::cout << fileVector[i] << std::endl;
}
std::cin.get();
}
Also if I compare one line of file 1 to all lines of file2 and if it doesnt match with any, print out that line...will that work?
That depends on what your program is expected to do. I can't possibly answer that.
You need to have some logic to find out which lines are the 'extra' lines and can be ignored while comparing. for example say a line starting with 'line67' is noise and should not be compared. If on the other hand it is random then I can't imagine a workaround and the comparison makes no sense.
Can you post the input files here ? A small portion may be ? As far as i know getline reads the entire line along with the spaces if any. Do you mean you have a few blank lines in between? This was the input I used and it worked fine
file1.txt
line0
line2
line3
line4
This is the last line
file2.txt
line0 are you there
line2
line4
line3
This isthe last line
Report.txt
line0 are you there
line4
line3
This isthe last line
I copied and ran your code and it worked fine for me. On unix though. Btw main should have a return type of int and you should remove all the unnecessary headers and don't use System("pause") , use cin.get if you want to pause your program.
Use couts to print out each line as you read and try using a debugger.
By last error I assume you mean
35 error: request for member 'name' in 'emp', which is of non-class type 'worker_t*'
emp is a pointer to the base address of the array of worker_t and not the class type itself. You cannot say emp.name hence. You have to get each element in the array and then call the appropriate function or memeber something line
emp[0].name // or emp[loop].name
Do you mean to pass the array and then the size separate?
Yes. When you say &employee[10] you are not passing the size, you are just passing the address (employee + 10) which is infact beyond your array. You need to pass the address of the start of the array and its size to be able to loop that many times.
input(employee,size); //you can exclude the '&' when passing the base address of an array
Welcome to Daniweb. Before anyone can help you please read this Read this before posting.
Following the tips here will make it easy for people to understand your problem, read your code, provide a better solution and will help you make the most of Daniweb.
And ..
1. line 22, You probably want to pass in the base address of the array.
2. For an array of size 10, index 10 will take you beyond the array into unknown territory
3. line 31, in the loop you go from 0 to 10 which is an off by one error.
Some more that you should notice as you fix these.
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.
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 ?
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.
Why are you creating the istringstream object in between? Just print the string. And move the cin.get outside the loop.
<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.
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.
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.
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
}
What errors?
-- Double post --
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.
Is your code even compiling? The first error I see is on line 20 where the array size is a non-const integer.
autocar is a pointer but autocar[i-1] refers to a struct and so you should use '.' to access its functions, attributes.
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..
'new' returns a pointer to the allocated memory so the variable should be a string* not a string.
yes job interviews, what a nightmare.. but well you're on another world all together :) !!
OP sorry for this digression
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 :)
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>.
@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?
if you are not averse to using standard generic algorithms, you can look at 'count' in <algorithm> header file.
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.
Derived class functions which have the same name as the base class functions hide the base class functions rather than overloading them. To allow overloading use the 'using' directive.
Use cin.get() instead of system("pause"). It is slow and non-portable. Read this thread for more info http://www.daniweb.com/forums/post58481.html#post58481
Does this code work?
You are not setting the pointer to NULL you are trying to assign an initialized object of type list to NULL and there is no such assignment operator.
My bad.. i didn't see that there was an else too. Well I don't see why the values will not be updated properly. I tried your code, removed the references to Cell class, just a simple code, add_to_array was changed to
void add_to_array(int *dest_s, int *buf_s)
and the values were updated properly. May be you can try doing the same and debug and then add the details.I just hope your application is not crashing with all those pointers in there. As mentioned above, using STL might be a good idea.
Now your class doesn't have any functions? How will this code ever execute? I'm sorry, either you have no clue what you are doing, or you are not able to express it well. This code, MortgageArray1, with a main function should probably work.
void add_to_array(Cell *cell, Cell ***dest, Cell ***buf, int *dest_s, int *buf_s) { if (*buf_s < *dest_s) { } }
Are you sure the control is going inside this if condition? Can you put some couts and confirm that?
Why do you have 2 programs in there (2 mains)? I don't see any Loan array in your code. can you mark the lines where you are printing the loan array? It is a little difficult to read.
Have you seen the reference on assert function? It says that it needs an expression, and if the value of the expression is '0' , it terminates the program. Now since what you have is just 2 ints that represent the day and the month, it is upto you to decide how to evaluate the correctness. What is your criteria for a legitimate date?
For strcat to work the destination should be large enough to hold the concatenated output. temp points to a memory location that is big enough to hold the filename and nothing more.
You can find a lot of material on coding up BFS on the net. What language are you going to code it in? If you have tried something, post in the relevant forum and you would get a lot more specific help. And try to explain it better.
I use MS VC++6 and when I type code I have problem...let's say I have 123456789 typed and I place cursor between 4 and 5 and than type "something" I get 1234something and not 1234something56789
so it deletes and wont shift right :S
also enter doesnt brake new line,space,backspace....
I guess its some editor setting I need to tweak?
I agree, this is probably one of the stupidest questions asked here !!!
Not necessarily. You can have the other class as a private or protected member too or a pointer, or reference, depending on how you are going to use it.
I think it is composition, which if I'm not wrong is a type of association, but you should cross check that.
Yes you can do that, for example
class A
{
public:
int func() { return 1;};
};
class B
{
public:
A a;
};
int main()
{
B b;
b.a.func();
}
You've passed the function pointer but where are you using it?
I have n!/((n-r)!r!)
which I figured out how to make recursive:
int crn (int n, int r) { if (r==0||r==n) return 1; return (n-1)+(n-1,r-1); }
how do I make it non-recursive?
what is this?? This is not a program, nor is it a recursive function and it does not calculate factorial. Did you even read this before posting it?
void SearchNames(string names[], int &rTotal, string &rNameONE, string &rNameTWO, int &rCount_NameONE, int &rCount_NameTWO, bool result_NameONE, bool result_NameTWO) { /* This is the search function that implements a basic brut force search to find the specified names that the user inputs. */ rCount_NameONE = 0; rCount_NameTWO = 0; result_NameONE = false; result_NameTWO = false; for(int i = 0; i < rTotal; ++i) { //assume names[] is the array that holds the sorted names if(rNameONE == names[i]) { // Increment the counter to record occurences of name ++rCount_NameONE; result_NameONE = true; continue; } else if(names[i] > rNameONE) { break; } else { result_NameONE = false; //break; } } for(int j = 0; j < rTotal; ++j) { if(rNameTWO == names[j]) { // Increment the counter to record occurences of name ++rCount_NameTWO; result_NameTWO = true; continue; } else if(names[j] > rNameTWO) { break; } else { result_NameTWO = false; //break; } } } bool WriteOutput(string *pName, string names[], int &rIndex, string &rNameONE, string &rNameTWO, int &rCount_NameONE, int &rCount_NameTWO, bool result_NameONE, bool result_NameTWO) { /* The WriteOutput() function recieves the values that were passed to them as well as the users input for the search and displays them to the screen. This file also shows the user the output file for the results and or summary of the outcome of the program. */ // Declared Variables string filename = "FIRST-NAMES_SUMMARY.txt"; result_NameONE = false; result_NameTWO = false; // This will make FirstNamesOutput cast as cin or cout ofstream FirstNamesOutput; // Needed to open output …
I think you have to re-implement this code from scratch, this just doesn't seem like an implementation of a binary tree. I would suggest that you read some tutorial on Binary trees before you start. Here are some good links Binary Trees , Binary search trees . Or just find some book and go through them and then try again.
I have the following code ( I'm creating a binary tree and then I want to print the values on it's leafs) but when I'm running it, it never enters in that if from the parcurgere function. What have I done wrong?
void creare(node *p) { //your code } void parcurgere(node *p) { if(p != NULL) { cout<<p->val; parcurgere(p->st); parcurgere(p->dr); } } void main() { node *t = NULL; cout<<"dati radacina arborelui "; creare(t); parcurgere(t); }
The function 'creare' does not change 't' , after the call 't' is still pointing to NULL and hence the 'if' condition in 'parcurgere' does not evaluate to TRUE. If you want 't' to point to a new object which is created in 'creare' then you should it pass the pointer by reference. So you function could be
void creare(node *& p)
And don't use void main