• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

basic for loop syntax question

   Thread *th1;
    th1 = new Thread("Counter1");
    th1 = new Thread("Counter2");
    th1 = new Thread("Counter3");
    th1 = new Thread("Counter4");
    th1 = new Thread("Counter5");



Hi, how to use a loop for(int i=1;i<=5;i++) to generate above 5 Thread?
I am using g++ in linux
0
ddlam
Asked:
ddlam
  • 5
  • 5
  • 2
1 Solution
 
Infinity08Commented:
>> Hi, how to use a loop for(int i=1;i<=5;i++) to generate above 5 Thread?

You would just place it in a loop :

    Thread *th[5] = { 0 };
    std::string cntr = "Counter";
    for (int i = 0; i < 5; ++i) {
        th[i] = new Thread(cntr + ((char) ('1' + i)));
    }

Note that I put the created threads in an array instead of overwriting the same value.
0
 
itsmeandnobodyelseCommented:
>>>> for(int i=1;i<=5;i++)

better: for(int i=0;i<5; ++i)

C/C++ makes a 0-based indexing. You make life easier if you don't change that.

To add the counter 'i' to a string variable use string class:

      ...
      string counter = "Counter";
      counter += '1' + (char)i;  


the last statement appends one character at end of string "Counter". The character was calculated by 'adding' the loop variable i to the character '1' (ASCII 49) what gives the required. Finally pass counter.c_str() to the Thread constructor.

Note, you better would define a

  Thread* th[5];

array so that you have the handles for all threads and not only for the last.

 
0
 
itsmeandnobodyelseCommented:
Seems as I was too talkative as nearly the same could be said with a fraction of my text ;-)

Hope, it is no homework as with at least one of these solutions the work already was done !
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Infinity08Commented:
I didn't even consider that this could be homework, because it doesn't look like any of the usual assignments I'm aware of. If I'm mistaken, then I apologize.

And I'm sure your explanation will be appreciated, Alex.
0
 
itsmeandnobodyelseCommented:
>>>> And I'm sure your explanation will be appreciated, Alex.
Thanks.

>>>>     new Thread(cntr + ((char) ('1' + i)));

That only compiles if the constructor of Thread takes a string. If it takes a 'const char*' you better use a temporary string variable as I did in my code (counter) and turn it to a const char* by calling the string::c_str() member function.
0
 
Infinity08Commented:
>> That only compiles if the constructor of Thread takes a string.

Correct ... Good point :)
0
 
ddlamAuthor Commented:
Oh yeah, well, it's homework! cheer!
0
 
Infinity08Commented:
>> Oh yeah, well, it's homework! cheer!

Then why didn't you say so in your first post ? You've misled me and tricked me into solving your homework this way, and I'm not too happy about that :(

If somebody solves your homework for you, you've missed the whole point of homework : to learn something. You do realize that, don't you ? Wouldn't it be better to try and solve the homework yourself, and only come here in case of specific questions and/or problems ? You are following this course to learn, I hope ?
0
 
itsmeandnobodyelseCommented:
>> Oh yeah, well, it's homework! cheer!

Maybe he was joking ....

ddlam is a master in Macromedia Flash what doesn't exclude that he is a C++ beginner as well but makes it less likely. Nevertheless, if the issue can not be cleared (maybe by a 'pardon me') I won't answer to further questions of ddlam.
0
 
ddlamAuthor Commented:
It is a "part" of homework. I am writing a server so that 'For loop' is very small part of the homework but why do you guy care about homework problem? Is it wrong if I learn it in a faster way. I don't think spending a night for some syntax problem is good for programming. There are many program languages that you can spend your whole life for them or just skip something you don't enjoy.
0
 
Infinity08Commented:
>> why do you guy care about homework problem?

First of all, because it's in the rules that we can't solve homework :

        http://www.experts-exchange.com/help.jsp#hi105

Second, because doing the work for you would mean cheating you out of the experience of finding the solution yourself.


>> I don't think spending a night for some syntax problem is good for programming.

It is : that's what programming is all about. The stuff you already know, you can apply without a problem. It's the stuff you don't already know that makes programming interesting, because you have to spend (a lot of) time to find the answer, and all that time, you're learning new things. New things that you'll be able to apply later, because then you'll already know them.

The best way of learning something is really by figuring things out yourself. That way you're sure you understand the problem and the solution, and that's pretty much a guarantee that you won't forget it.
If you just take a solution from somebody else, you haven't gone through the process of figuring out the solution, so you're missing the experience gained from that. You'll also most likely not fully understand the solution, so you're a lot less likely to be able to remember it.


>> or just skip something you don't enjoy.

If that's your attitude, then you should realize that skipping it now means that you'll have to keep skipping it again and again and again. This takes a lot more effort than just taking some time to figure it out once, and then never have to worry about it again.
0
 
itsmeandnobodyelseCommented:
>>>> why do you guy care about homework problem?
It is simply the rules here in EE. Experts do not homework. They only help with homework. Period.

Infinity already told that the question didn't much look like typical homework. If you say it is only a little part of a greater homework, it shows that his judgement has been right. Then it is not a issue helping you solving this technical problem. The only issue left is that you haven't been honest from the start and that you mean to be right in making fun of the issue.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now