Thread: Quick Loop Question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    16

    Question Quick Loop Question

    How would I go about looping this?
    Code:
        cout << "Please enter the number of prompts you desire: ";
        getline (cin, NumPrompts); "\n";
        
        
    
           std::string command("C:\\windows\\system32\\ping.exe ");
           command.append(" ").append(Address);
           command.append(" ").append(Parameter);
           command.append(" ").append(Bytes);
           system(command.c_str());
    I know about the for loop, about the while loop etc. But my interest is in doing a loop with a variable. Like, you enter 5 for NumPrompts and it makes 5 prompts. Or you enter 10 and it makes 10, etc. Before you redirect me to MSDN, or elsewhere: Please know I already looked. I would greatly appreciate any help.

    -Nightwarrior

    Edit: Oh yeah, whenever I try to use for or while, my compiler (dev c++) gives me an error that command is undeclared, but it runs fine without the loop.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't loop though a bunch of named variables. That's what arrays are for. With an array, it's easy.

    [edit]
    Code:
    for(int x = 0; x < 3; x ++) {
        command.append(" ").append(array[x]);
    }
    [/edit]

    [edit=2] I think that I've misunderstood what you're trying to do. Are you just trying to do this?
    Code:
    for(int x = 0; x < NumPrompts; x ++) {
        system(command.c_str());
    }
    BTW, this "\n" does nothing:
    Code:
    getline (cin, NumPrompts); "\n";
    I think you put the ) in the wrong place.
    [/edit]
    Last edited by dwks; 10-17-2007 at 11:52 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Do you need something like this?
    Code:
        int NumPrompts;
        cout << "Please enter the number of prompts you desire: ";
        cin >> NumPrompts;
    
        int i=0;
        while (i++ < NumPrompts)
        {
            std::string command("C:\\windows\\system32\\ping.exe ");
            command.append(" ").append(Address);
            command.append(" ").append(Parameter);
            command.append(" ").append(Bytes);
            system(command.c_str());
        }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My second edit does that in a much more efficient way, noodles.

    Code:
    for(int i=NumPrompts;i;--i)
    Eh. The standard idiom is to loop from 0 to NumPrompts-1 inclusive, or [0,NumPrompts). That's just backwards.

    Besides, if you're doing it that way, you don't even need a loop variable.
    Code:
    do {
        // code
    } while(--NumPrompts);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ...

    You need to declare the array. Something like this, perhaps.
    Code:
    std::string parameter[3];
    parameter[0] = "one";  // whatever was in the variable Address
    parameter[1] = "two";  // whatever was in Parameter
    parameter[2] = "three";  // whatever was in Bytes
    
    std::string command("C:\\windows\\system32\\ping.exe ");
    for(int x = 0; x < 3; x ++) {
        command.append(" ").append(parameter[x]);
    }
    std::system(command.c_str());
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Alright, I'll mess around with that a little. My goal is though, to start as many ping.exe's as the NumPrompts variable.

    Here's the full code (might help explain it more):

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        string Address;
        string Parameter;
        string Bytes;
        int NumPrompts;
        
        cout << "Please Enter The I.P. Address: ";
        getline (cin, Address); "\n";
        cout << "Please Enter any Parameters you wish to include (example: -t -l): ";
        getline (cin, Parameter); "\n";
        cout << "Please enter the byte number you wish to send (-l # (# = 0-65500)): ";
        getline (cin, Bytes); "\n";
        cout << "Please enter the number of prompts you desire: ";
        cin >> NumPrompts; "\n";
        
        
    
            std::string command("C:\\windows\\system32\\ping.exe ");
            command.append(" ").append(Address);
            command.append(" ").append(Parameter);
            command.append(" ").append(Bytes);
            system(command.c_str());
    
    
           
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by Nightwarrior; 10-17-2007 at 12:01 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       int i=0;
        while (i++ < NumPrompts)
    Why not use a for-loop - that's what they are for (pun intended ).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    None of the loops are actually starting more than 1 command prompt w/ ping in it. They all only start one.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, system() is defined to start a new task/process, and wait for it to finish. So by definition, you'll get four (for example) pings running one after another - not in parallel.

    If you want to run multiple instances, then you need to use something other than system - perhaps spawn(), CreateProcess(), ShellExecute() or some other similar function.

    I doubt that your loop in itself is wrong - and if you want to count from one number to another, a for-loop is really the most obvious and natural mechanism to use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Quote Originally Posted by matsp View Post
    Well, system() is defined to start a new task/process, and wait for it to finish. So by definition, you'll get four (for example) pings running one after another - not in parallel.

    If you want to run multiple instances, then you need to use something other than system - perhaps spawn(), CreateProcess(), ShellExecute() or some other similar function.

    I doubt that your loop in itself is wrong - and if you want to count from one number to another, a for-loop is really the most obvious and natural mechanism to use.

    --
    Mats
    Thanks for the reply. I was using ShellExecute before, but I couldn't get it to work; so I swapped over to this (since this is the help someone else offered me). I have no idea how to go about all of this in c++, due to being a newb. Maybe I should just stick to VB. =/

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A simple way to do it is to use the program start, which is Windows-specific, but then so is ShellExecute(). Just use
    Code:
    std::string command("start C:\\windows\\system32\\ping.exe ");
    // ...
    and you'll get the ping in a new window. You may have to wrap double quotes around your command, I can't remember exactly how start works, and this is a Linux system.

    The XP version of start has some interesting features. For example, you can start a program minimized or maximized.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Quote Originally Posted by dwks View Post
    A simple way to do it is to use the program start, which is Windows-specific, but then so is ShellExecute(). Just use
    Code:
    std::string command("start C:\\windows\\system32\\ping.exe ");
    // ...
    and you'll get the ping in a new window. You may have to wrap double quotes around your command, I can't remember exactly how start works, and this is a Linux system.

    The XP version of start has some interesting features. For example, you can start a program minimized or maximized.
    That works, thank you very much! And much thanks to the other people as well! I really learned a lot from this thread / little program. I will try to study up on how to use ShellExecute(), as well.

    Again thanks and best regards,

    Nightwarrior.

    Edit: I forgot to mention, just adding start works, no need for double quotation marks.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Food for thought: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    [edit] I hope you never have to stick to VB. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  5. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM