Hi, I've been working alot on a program lately, and I'm actually veeery close to done.
Atleast until I come up with something more to add.

The most useful function of this program, is the ability to create Shortcuts, which at the same time creates a command to start it.
Everything is happening in a DOS window, so It's nothing visual.

Right now, it works fine. It creates logs, shortcuts, commands.
I can type for example; chrome
And it simply launches chrome :icon_cheesygrin:
Now to what I need help with.
I've been trying to search, find own ways or whatever.
But I simply can't figure out how to run several programs by commands on one single line.
Ex. chrome mirc msn wmp devcpp
And it runs them all.
So does anyone know how to make use of getline() for this?
Or maybe another way?

I know how to make it search the line until a space ' ' comes, and place it into a variable.
But how do I continue afterwards?:-/

cin >> var1 >> var2 >> var3 >> var4;

cin will ignore the whitespaces and newlines and input to multiple variables

*EDIT: I noticed I didn't really offer a true solution because you dont know how many commands the user will enter so you could do this:

const int NUM_PROG = 10;   // Number of programs that could be run in single command
const int NAME_LEN = 80;  // Max program name size
char prog[NUM_PROG][NAME_LEN];
int index = 0;

while (index < NUM_PROG && cin >> prog[index]) index++;

This would input to variables until there is no more input and you can use the index to tell you how many programs the user entered.

Your right on the first code, it wouldn't be so functional.
But the second, doesn't work. I have to type something 10 times :|
But thanks

I ran the code to test it and just to let you know I forgot to add that the loop should terminate when a sentinel value is entered, that will work..... cept I cant edit my post anymore :( For example:

// Here, I use -1 as the sentinel value, so if the user enters -1, the loop will terminate
while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);
const int NUM_PROG = 10;   // Number of programs that could be run in single command
             const int NAME_LEN = 80;  // Max program name size
             char prog[NUM_PROG][NAME_LEN];
             int index = 0;
             cout << "Program(s) to launch: ";
             while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);

= same
:-/
I still have to type something 10 times.
Am I doing something wrong?

Hmmm... thats exactly what I have and mine works lol..... you included the cstring library right? (im assuming you did or u should get a compiler error but thats really the only thing I could think of lol) If you type three inputs then -1, each one seperated by a space, then press enter it still waits for more input??

Hmmm... thats exactly what I have and mine works lol..... you included the cstring library right? (im assuming you did or u should get a compiler error but thats really the only thing I could think of lol) If you type three inputs then -1, each one seperated by a space, then press enter it still waits for more input??

I'm not at home right now, so I can't test with that include.
Yeah, you heard. I don't have that include but I don't get any errors lol.

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <shellapi.h>
#include <time.h>
#include <io.h>
#include <sys/types.h>
#include <dirent.h>

Alot I know.

This is very weird.... what compiler are you using? It shouldn't make any diff in this case but im running out of ideas

just for giggles can you try

while (index < NUM_PROG && cin >> prog[index])
{
       if(strcmp(prog[index], "-1")
              break;
       index++;
}

Hey Nate, did you try running my code above to see if it worked for you? It works fine for me but its not working for iou.... see if you can catch something cuz im out of ideas

This

int index = 0;
             while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);

looks really suspicious. At the first iteration you access prog[-1] , which invokes an UB. Not sure if it is a "real" reason.

This

int index = 0;
             while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);

looks really suspicious. At the first iteration you access prog[-1] , which invokes an UB. Not sure if it is a "real" reason.

The reason being is that you want strcmp to check prog at the last loop iteration before it cin's to prog[index++] to see if user added the sentinel value

I understand the reason. Still it doesn't justify violating the rules.

I understand the reason. Still it doesn't justify violating the rules.

As im def no master of C++ yet =) just wondering what issues you would run into with the loop? I know strcmp during the first iteration tests a value outside the bounds of the array but it is a read only operation... could this still cause some kind of problem?

A segfault (or access violation) to begin with.
What is important is that if the program invokes an undefined behaviour, you may turn the logical thinking off. Everything becomes possible.

> could this still cause some kind of problem?
Any of
- nothing at all, the loop just exits
- what you wanted to happen
- segmentation fault
- machine reset

It's undefined behaviour - period.
You're sitting on the branch of the tree and you're cutting the wrong side!

The fact that it "works for you" just makes you lucky, that is all.
Tomorrow, you might be less lucky.

I haven't been able to test anything yet, but I could tell you what I need in more detail.

I need 10 variables, to call each program to.
So for example; chrome myprog dropbox
Here, chrome should be the Prog1, myprog Prog2 etc...
But if I write this code myself, I have to type them all in.
Which is not what I want, the user should be able to just type in the programs he wants to start, while maximum is still 10 at a time.

I can solve this myself, but that would take a whole lot of lines and the user would first have to type in a number of how many programs he wants to start, and for each I have to have the same code lol.
Pretty stupid, but if theres no other way, then I'll just have to go with it.
Thanks for helping:icon_wink:

So does anyone know how to make use of getline() for this?

You were on the right track (i.e. getline()) in the beginning, so you might get the input doing e.g. the following:

#include <iostream>
#include <string>

using std::string;
using std::getline;
using std::cin;
using std::cout;

int main()
{
    string input;
	
    // Let the user type in the whole line ...
    getline(cin, input);
	
    // What did we get?
    cout << "[" << input << "]";
	
    // Todo: iterate over the input extracting the individual 
    // words, using e.g. a stringstream

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.