tux4life 2,072 Postaholic

Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?

If you mean all resources (also variables which aren't on the heap) , then I would answer no, in a class you put elements and functions related to each other.

However, it seems like you're pulling in the direction of a garbage collector.
If you like this concept, then it would be probably better to Google on "C++ Garbage Collector" or something :)

tux4life 2,072 Postaholic

1. When passing objects between each other is it preffered to use references if not what then?

5. When allocating resources should you always do it inside of a class?

To answer your first question: yes, the preferred way of passing objects is pass by reference

In your fifth question you make an assumption which is wrong (unless you implement a destructor for your class as well), if you use dynamic memory allocation inside a class, and you don't make use of a destructor, the memory won't be freed :)

And regarding question 4, what SDK are you talking about? It's common in class libraries that there are rules to name for example variables or functions, i.e. to make it easier for the programmer to go through/read the code :)

Could you be more specific in your second question?

tux4life 2,072 Postaholic

Please post using code tags (click the [B]#[/B] button and paste your code in between the tags)

>I want to read all the file at once

Do you mean all the files at once, then read this:
Why would you do that? You can't process them all at once :P
But you could do it as follows: you open one file at once, you read the current file's contents, write it to the other file, close the file and open another one, ... (repeat this process until all files have been processed) :)

Or did you mean: I want to read the whole file in one time?

tux4life 2,072 Postaholic

Not really, I am a beginner of C. could you tell me more about that?

Take a look at this web page :)

tux4life 2,072 Postaholic

Take a look at this excellent snippet and call the Split function as follows:
(I assume you already read a line from the file) Split( [I]string_from_file[/I], [B]"\t"[/B], words ); Remember that string_from_file has to be a c-string :)

tux4life 2,072 Postaholic

Why would you want to mix up c functions with c++?

Remember that it's still a part of C++, I only gave him an advice, do you know something better, then just tell it OK?

tux4life 2,072 Postaholic

>Can't you read each line in and then use the tab as a delimiter?
It is possible, if you use strtok for example, you could use a TAB as delimiter as follows: strtok(tmp, "[B]\t[/B]"); (assume that tmp is a character string)

tux4life 2,072 Postaholic

Well, I did a small test to see what your problem is, but with me it works fine, this program reads successfully from a file which contains tabs:

The code I used:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	string tmp;
	ifstream infile("t.txt");
	while( infile >> tmp )
		cout << tmp << " ";
	infile.close();
	return 0;
}


t.txt file contents:

Hello			beautiful world   1230 		!!!!

The output I'm getting:

Hello beautiful world 1230 !!!!

Just try and adapt it to your program.

iamthwee commented: noob error. -4
tux4life 2,072 Postaholic

Don't use [B]!inf.eof()[/B] as a condition in a loop for reading a whole file:

void get_graph ( string const &filename, NodeMap &node_map )
{
   ifstream inf ( filename.c_str() );
   string from, to;
   double weight;
   while ( [B]!inf.eof()[/B] )
   {
      inf >> from >> to >> weight;
      if ( inf.good() )
      {
         Node *Target = node_map.find_in_nodemap ( to );
         Node *Source = node_map.find_in_nodemap ( from );
         Edge *connector = new Edge ( weight, Target );
         Source->neighbors.push_back ( connector );
      }
   }
}

You could begin by changing your code to:

void get_graph ( string const &filename, NodeMap &node_map )
{
   ifstream inf ( filename.c_str() );
   string from, to;
   double weight;
   [B]while ([/B] [B]inf >> from >> to >> weight[/B] [B])[/B]
   {
         Node *Target = node_map.find_in_nodemap ( to );
         Node *Source = node_map.find_in_nodemap ( from );
         Edge *connector = new Edge ( weight, Target );
         Source->neighbors.push_back ( connector );
   }
}

By the way: where do you close that file ?

tux4life 2,072 Postaholic

iamthwee, you should have known better: Do you know that we don't give full code on this forum?
How is he supposed to learn from code we write for him?
You can only learn to program by writing code yourself, not by copying it from someone else.
BTW, Apparently you even don't understand why to avoid [B]system("pause");[/B] , and because some people are too lazy to just take a look at my signature and review the link I am going to copy what's in my signature and paste it below:
Avoid using system("pause"); use cin.get(); instead.
(and here's the reason: http://www.gidnetwork.com/b-61.html, and please take the time to review it carefully!!)

amrith92 commented: I agree :) +1
tux4life 2,072 Postaholic

>Nope, I've tested it with both dev-cpp and in ubuntu.
Essentially they both use the same compiler :)

To the OP: Have you already noticed number 3 of my signature ?

tux4life 2,072 Postaholic

>which works fine except that it prints the last character twice
That's because of while(!inFile.eof()) , take a look at William's post :)

tux4life 2,072 Postaholic

Or read the following (IMO the best) free e-book about C++ programming: http://msdn.microsoft.com/en-us/beginner/cc305129.aspx :)

tux4life 2,072 Postaholic

Start with these:

Then you should explain what you mean with: ...output should be in alphabetical order... (give an example)
Do you mean the following?

[U]File looks like:[/U]
[I]lalal nn[/I]
[U]Output should be:[/U]
[I]aalllnn[/I]
tux4life 2,072 Postaholic

>You know how sometimes you google one thing then it just pops up with something else you don't know?

Yes, but essentially you already know string streams, if you can do I/O with cin/cout, you do already know the basics of string streams, so I would advice to Google up some information about string streams :)

tux4life 2,072 Postaholic

You don't understand what I'm saying

I don't believe that ArkM doesn't understand what you're saying, in fact he does understand what you mean (perfectly), take a look at his code in your previous thread, it's very understandable and well documented.
And about the string streams: I linked that post to a web page, it might be helpful if you read this info as well, don't understand it directly? Well, then you'll have to Google a bit, remember: Google is your friend!

Information is useless if you don't understand how to use it.

Or if you don't do any effort to understand and use it!

If you have the ability to look at a book and immediately understand every single detail of the subject being described. Then congratulations to you. Your a genius, but I'm not.

Sure he's a genius, probably he wrote the code himself so he won't have to take a book to understand his own code, by the way: nearly every line is commented, did you notice this?

tux4life 2,072 Postaholic

contents of the data.txt file are like this
70 97 104 97 100

I'm thinking of extracting each ascii number and convert it to char and print it to the screen
but how I do the extracting process in code

Start with my previous post (read the comments, they might be useful :P) , remember, we aren't writing code for you!

BTW, the following is useless IMO:

for (int i=0;i<=str.length()-1;i++)
{
	if(str[i] == ' ')
	{
		
	}
		
}

If you get the integers in the same way as I demonstrated in my previous post, then you even don't have to bother with spaces, you're just making it yourself difficult if you would do :)

>but how I do the extracting process in code
Take a look at my previous post, and put the code mentioned there in a loop (I hope this isn't too difficult for you).

tux4life 2,072 Postaholic

The problem with cin >> a >> b;
is that it'll make the program prompt for 2 input values every time.

Another approach:
Get the whole line (you were already doing this)
Make use of string streams to parse the whole command !

tux4life 2,072 Postaholic

What do you think of: cin >> [I]your_command[/I] >> [I]your_integer_parameter[/I]; ?

tux4life 2,072 Postaholic

This example doesn't put the whole stuff in a string, but it gives you a good start:

int ascii;                     // for holding the ASCII value
inFile >> ascii;                // get the next ASCII code from the file
cout << (char)ascii << ' ';  // print out the character equivalent of the ASCII code
tux4life 2,072 Postaholic

Just get the ASCII codes from the file (as a number), then put it in a char-variable and print it to the screen or append it at the end of the string variable where you want to put the converted string in :)

tux4life 2,072 Postaholic

Remember KISS principle: no need in temporary strings! ;)

I thought that was the simplest way, however he could also just shift each character to the left if there's a space in between. Or do you know the 'ultimate' way? :P

tux4life 2,072 Postaholic

How I would do it:

  1. Create a temporary variable of the same length as the one were you want to delete the spaces from
  2. Use a for-loop to loop through the whole string where you want to delete the spaces from, every time you check whether the read character is a space or another character, when the character is a space, don't copy it to the temporary string, otherwise: copy it to the temporary string.
  3. Eventually you could use strcpy to copy the string without the spaces to the variable which was holding the string with the spaces (note that it will be overwritten then!)
tux4life 2,072 Postaholic

How would I make a program that can insert text into a text field in another program? Or click a button in another program?

You could implement your application as one which communicates with it's own instances over a network, then you can let your program send messages to the other instance, when the other instance recognizes the right message you could for example change the text in a text field :)

tux4life 2,072 Postaholic

I will try. because I am a Vietnamese, so be able to write non-standard English. You please guess.

We don't care about the fact whether English is your native language or not(*), it's your effort which counts !
Let me clarify this: You have posted your assignment, but what do we have to do with it? Don't expect from us that we're going to make it for you, Daniweb isn't a homework completion service, but tell us what you don't understand and we'll be glad to help you out, are you having problems with the coding? Well, then just post the code you have so far, tell us what your problem is and if your program is giving unexpected output, please also tell us what output you're expecting.

Question: How can you become a good programmer if you don't write code yourself?
Answer: Never!

(*) As long as we can understand you it's OK :)

tux4life 2,072 Postaholic

ok so could you give me some ideas/examples please, am really struggling with this part
Thanks

Take a look at the following:

>Push it and put your code inside it. Use different code tags for each of your classes
Or just paste your code in the message, select the code and push it (#) :P

tux4life 2,072 Postaholic

suppose not sorry i have been ill - and did not read everything i was supposed too. so can anyone help me with my problem please.

And selecting your code and clicking the code tag button ( # ) was asked too much?
You must be very ill then...
If you're ill then it's better to stay away from your computer and rest a bit :P

Salem commented: LOL, "ill", that's a new one. +32
tux4life 2,072 Postaholic

Is it so difficult to post using code tags?
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.

tux4life 2,072 Postaholic

Yes, on my machine it did also run correctly, did you follow siddhantes' instructions carefully?
(Nope, otherwise it would work)

while ( ask_them_again [B]=[/B]= 1 )
{
    cout << " \n Please enter the time in hours minutes second";
    cout << " \n Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) \n\n==> ";
    cin >> *h_ptr >> colon >> *m_ptr >> colon >> *s_ptr;
.
.
.
tux4life 2,072 Postaholic

but you guys, when I enter the number, it doesn't show correctly

What's the code of this?
You probably did something wrong.

BTW, why don't you just pass these variables as a reference?
Let's do it the C++ way !

tux4life 2,072 Postaholic

Here's an example on how to get the time:
I don't know whether it's good to recommend you, but you can also achieve the same by using scanf from the standard C library (cstdio):

int hours, minutes, seconds;
scanf("%d%*c%d%*c%d", &hours, &minutes, &seconds);

or you could rewrite it in C++ (recommended way) as:

int hours, minutes, seconds;
char c;
cin >> hours >> c >> minutes >> c >> seconds;

These snippets will allow the user to input the time as 15:43:22 for example :)

BTW, if your compiler supports it, then use #include <cstdlib> instead of #include <stdlib.h> .

tux4life 2,072 Postaholic

I don't know where you're talking about, with me it works fine, could it be that you did something wrong?

tux4life 2,072 Postaholic

Hi, tonig_ccc, do you know why they only provide the source?
That's because people who don't have the right knowledge about that kind of things (exploits etc.) shouldn't use these software ...

BTW, I don't know any "mocrosoft visual studio" :P

tux4life 2,072 Postaholic

i wanted to ask you guys for some help
i use microsoft visual studio 9.0
and i have downloaded hotspotter
but it resulted it was all a source
now i dont know how to compile it with mocrosoft visual studio
any help???????????

You could start by giving us the link to where we can download hotspotter, so we can check on our computers (with our compilers) to see if it compiles, BTW: aren't there any instructions? That would be hard to believe!

Salem commented: No, just incompetence. Just another noob playing with chainsaws. +32
tux4life 2,072 Postaholic

Very common mistake in for loop:

for(i=0; inputDone == 'y'; i++)

did u get this!!

Don't blame him for making a mistake, everyone makes mistakes (me too), let me explain:
If you write for(i=0; inputDone [B]=[/B] 'y'; i++) with one '=' sign then you're doing an assignment to a variable, which in this case means that everytime when the loop has run one time variable inputDone gets assigned value 'y'.
The assignment operator '=' always returns the assigned value (in this case the assigned value is 'y' every time), which means that if the assigned value was anything other than zero (or some value which will be implicitly converted to zero), the condition in the loop will be evaluated to true which means the loop will be running non-stop (otherwise the condition will be evaluated to false, which means the loop won't run, but this isn't the case here).
So how do we get over this?
Well you probably wanted to compare the value in variable inputDone with 'y', so you'll have to use the '==' operator: for(i=0; inputDone [B]==[/B] 'y'; i++) .

Remember: If you want to compare two variables or two values (or a variable and a value) you use the '==' operator, if you want to assign a value/variable to a variable, you use the '=' operator

tux4life 2,072 Postaholic

Hi,
I am trying to declare a 3D array with the size of 6,400 and 400 in each dimension using the code as following:

short matrix[6][400][400];

So what's wrong with this code?

Nothing, with me it compiles and runs fine :)
Try compiling it as a release executable, you're probably compiling it in debugging mode ...

tux4life 2,072 Postaholic

Why don't you post using code tags?
Is that maybe too difficult?

tux4life 2,072 Postaholic

Did you define MIRACLES? #define MIRACLES 1 :P

tux4life 2,072 Postaholic

>Which is why I said that it's not a problem with the code.
Yes, but I tried it in three different ways:

  1. One time when the program wasn't in the same directory as the Input.txt file: error
  2. One time when Input.txt didn't exist in the directory where the executable was in: error
  3. And at last, when they both were in the same directory and when Input.txt exists: no error :)
tux4life 2,072 Postaholic

I would suggest you to go to your local bookstore and buy that one :) (both books are exactly the same, I compared the ISBN code)

tux4life 2,072 Postaholic

#include <stdlib.h> is old style C++, if your compiler supports it you should consider using the new-style header file: #include <cstdlib> BTW, Read the third remark in my signature :P

tux4life 2,072 Postaholic

You should check whether the program is in the same directory as the Input.txt file, if you're using an IDE, your executable is probably stored in another directory :)

I can only say that I tried it on Windows and it did work perfectly :)

tux4life 2,072 Postaholic

Try ifstream Input("Input.txt", ifstream::in); instead of ifstream Input("Input.txt"); :)

tux4life 2,072 Postaholic

No!...i am using Windows XP!....i have also tried this code on Visual Studio C++ 2008.and why do you think suggesting a c++/CLI solution aint a good idea?....

Because the OP probably uses Unix/Linux and they don't support CLI/.NET (yet? :P)

tux4life 2,072 Postaholic

>Alternatively, is there a way to convert from string to char * (not convert from string to const char *, as c_str() does)?
Yes there is:

string s1 = "Hello World";
char *s2 = new char[s1.size()+1];
strcpy(s2, s1.c_str());
delete s2;

explanation of the code:

line 1: declare a string and put some sample data in it
line 2: dynamically allocate memory (one element extra because of the NULL-terminator)
line 3: create a copy from the constant string and put it into a non-constant character array
line 4: free up dynamically allocated memory
tux4life 2,072 Postaholic

>Yes, but apply the same rule to 15000, 1500, 150 and 15, what now?
It works fine? As long as you have the right special cases for identifying a teen and ignoring zeros, all is well with _Nestor's logic.

Oh, as I'm not a native speaker I forgot about the English language rules for forming a number, if you write a program which can successfully apply these rules, it's all OK :)

And of course such a converter is better than just storing all text-representations in every way, for example it doesn't take up a lot of memory (this is rather for big numbers), you won't have to put all your effort in manually inputting all the text-representations of each number :)

tux4life 2,072 Postaholic

An example of sprintf:

int i = 52300;
char s[30];
sprintf(s, "%d", i); // s now contains the value 52300
tux4life 2,072 Postaholic

use the following code for random numbers between limits:

Random* r = new Random();

int x=r->Next(10,21);

this will create random numbers between 10 and 20!

Doesn't work :(

tux4life 2,072 Postaholic

yes,i agree thats impractical but i think its better to teach 'em the basics first.

And a simple statement which uses some conditional operators aren't the basics or what?

tux4life 2,072 Postaholic

Also if your looking for large numbers lets say 5400 and you want to write five thousand you could count the digits. So the number 5 is the forth number from the end which makes it easy to determine it represents 5 thousand. The number 4 is three digits in so it represents hundres etc..

Yes, but apply the same rule to 15000, 1500, 150 and 15, what now?