GeneralThis forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 2,612
Rep:
i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
argv[0] (equivalent to *argv) contains the command used to execute the program.
argv[1] contains the first argument.
Duh! I knew it was probably a problem with the for loop!
Well, I changed the *argv to *(argv+1), and that makes the first question work, but I'm still trying to figure out why the others won't...
EDIT: Removing the else break; clause did it.
The revised code:
Code:
#include <stdio.h>
char* questions[3] = {"What are you doing now?",
"Who are you?",
"How are you feeling?"};
char* answers[3] = {"None of your beeswax...\n",
"Who are YOU?\n",
"Why do you care?\n"};
int main(int argc,char** argv)
{
if(argc < 2)
{
printf("Usage: ./askme <phrase>\n");
printf("(where <phrase> is a question)\n");
return 1;
}
else
{
int i;
for(i = 0; i <= 2; i++)
{
if(strcmp(*(argv+1),questions[i]) == 0)
printf("%s",answers[i]);
}
}
return 0;
}
I still don't understand why you wrote *(argv+1) instead of the equivalent, but shorter and easier to understand argv[1].
It helps to reinforce the concept of how pointers really work, in my mind. It reminds me that the "pointer" is really just a counter that keeps track of a memory address, and that the "array indices" are simply relative address points to the initial address that the "pointer" counter keeps track of, incrementing/decrementing by one or more units of the variable data type size (1 byte for char, 4 bytes for int, etc.).
Wow, even reading just part of that asm tutorial really solidified these concepts for me quite well!
i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
neat, I've got something like that too
in one I display a few characters randomly of an MMO I play in the respective forum, in the other I just have a mockup of Schroedinger's cat, randomly one of the two given possibilities ^^
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 2,612
Rep:
a bit of nostalgia
Code:
CLS
INPUT "number of columns"; cols
CLS
FOR x = 1 TO cols
FOR z = 1 TO (cols - x)
st$ = st$ + " "
NEXT
FOR y = 1 TO x
st$ = st$ + "* "
NEXT
PRINT st$
st$ = ""
NEXT
any guess as to the programming language?
same program in c++
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
if (argv[1] == NULL) {
cout << "usage: pyramid number of rows\n";
return 1;
}
int cols=atoi(argv[1]);
for (int n=0; n<=cols; n++) {
for (int z=0;z<=(cols-n);z++) {
cout << " ";
}
for (int y=0;y<=n;y++) {
cout << "* ";
}
cout << "\n";
}
return 1;
}
and in c++ as a cgi program
accessable when compiled and placed in the cgi-bin folder by
Code:
http://{host}/cgi-bin/pyramid.cgi?{number of colums}
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
cout<<"Content-type: text/html"<<endl<<endl;
if (argv[1] == NULL) {
cout << "usage: pyramid number of rows\n";
return 1;
}
int cols=atoi(argv[1]);
for (int n=0; n<=cols; n++) {
for (int z=0;z<=(cols-n);z++) {
cout << " ";
}
for (int y=0;y<=n;y++) {
cout << "* ";
}
cout << "<br />";
}
return 1;
}
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 2,612
Rep:
its extracts php code from a file ($_GET['file']) into memory from a tarball (on the fly) and executes it with eval()
(requires PEAR and the PEAR archive module)
would be invoked by
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.