 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
marlen at yahoo dot it Guest
|
Posted: Tue Apr 20, 2004 10:02 am Post subject: i have a little problem....with int main (....) |
|
|
hi,
I've created a function int main(int argc, char* argv[])...and when I
run this programm I will inserted some parameter(especially integer)
whose I need in another function (for example: ./programm 300
400)....in my case I will, when run "./programm 300 400", show "my
number is 300 and 300 + 100 = 400".
How can I do ?
thank u!!!
bye
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Apr 20, 2004 10:25 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
marlen at yahoo dot it wrote:
| Quote: | I've created a function int main(int argc, char* argv[])...and when I
run this programm I will inserted some parameter(especially integer)
whose I need in another function (for example: ./programm 300
400)....in my case I will, when run "./programm 300 400", show "my
number is 300 and 300 + 100 = 400".
|
The typical behaviour is that the parameters to main() resemble the
commandline. In your case, calling the program with "./programm 300 400"
results in these parameters:
argc = 3
argv[0] = "./programm"
argv[1] = "300"
argv[2] = "400"
You can parse these strings, add the resulting numbers and then simply write
the result to std::cout.
ciao
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gareth Stockwell Guest
|
Posted: Tue Apr 20, 2004 10:32 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
This should do it:
#include <iostream>
int main(int argc, char** argv) {
// Check enough arguments were supplied
if(argc < 3) {
std::cerr << "Usage: " << argv[0] << " x z" << std::endl;
exit(1);
}
// Process the command line
const int x = atoi(argv[1]), z = atoi(argv[2]);
// Compute and output the difference
const int y = z - x;
std::cout << x << " + " << y << " = " << z << std::endl;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Niels Dybdahl Guest
|
Posted: Tue Apr 20, 2004 11:30 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
| Quote: | I've created a function int main(int argc, char* argv[])...and when I
run this programm I will inserted some parameter(especially integer)
whose I need in another function (for example: ./programm 300
400)....in my case I will, when run "./programm 300 400", show "my
number is 300 and 300 + 100 = 400".
How can I do ?
|
You might use atoi for converting the parameters to int and use printf to
write the result.
Niels Dybdahl
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Tue Apr 20, 2004 11:33 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
marlen at yahoo dot it wrote:
| Quote: | hi,
I've created a function int main(int argc, char* argv[])...and when I
run this programm I will inserted some parameter(especially integer)
whose I need in another function (for example: ./programm 300
400)....in my case I will, when run "./programm 300 400", show "my
number is 300 and 300 + 100 = 400".
How can I do ?
|
Horner's algorithm is a good place to start.
It may help to look at <ctype> and isdigit() if you want to validate
the inputs.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Apr 23, 2004 11:11 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
I think you're all interpreting the OP's original request incorrectly.
[email]bise79 (AT) hotmail (DOT) com[/email] (marlen at yahoo dot it) wrote
| Quote: | I've created a function int main(int argc, char* argv[])...and when I
run this programm I will inserted some parameter(especially integer)
whose I need in another function (for example: ./programm 300
400)....in my case I will, when run "./programm 300 400", show "my
number is 300 and 300 + 100 = 400".
How can I do ?
|
Not the best English in the world. But "I will inserted some parameter"
seems a strange way to say "I will calculate some value." Even more
suspicious is "I need in another function."
I suspect that Marlen wants to type in
./programm 300 400
and then invoke
./anotherprog
which would read the input "my number is 300 and 300 + 100 = 400"
If I'm right, Marlen wants to understand how Unix-style pipes work...
./programm 300 400 | ./anotherprog
The "|" character is the "Pipe" character; on most keyboards, it is
shift + "". The output of one program is fed as input to the next
program. Besides Unix, this works on all versions of Microsoft Windows
and even MS-DOS.
Then again, Marlen might want programm to invoke
./anotherprog my number is 300 and 300 + 100 = 400
which of course doesn't have a simple answer, but he might try
redirecting output into a command file and then calling it.
./programm 300 400 >cmdfile
call cmdfile
None of that is exactly on-topic in this newsgroup, but if Marlen is
having trouble phrasing the request, it's natural to assume that he
doesn't know which newsgroup is correct. Perhaps one of the Unix groups?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Apr 26, 2004 10:23 am Post subject: Re: i have a little problem....with int main (....) |
|
|
[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote in message
news:<7f2735a5.0404221605.50a80b2e (AT) posting (DOT) google.com>...
| Quote: | I think you're all interpreting the OP's original request incorrectly.
[email]bise79 (AT) hotmail (DOT) com[/email] (marlen at yahoo dot it) wrote
I've created a function int main(int argc, char* argv[])...and when
I run this programm I will inserted some parameter(especially
integer) whose I need in another function (for example: ./programm
300 400)....in my case I will, when run "./programm 300 400", show
"my number is 300 and 300 + 100 = 400". How can I do ?
Not the best English in the world. But "I will inserted some
parameter" seems a strange way to say "I will calculate some value."
|
True, but it doesn't surprise me too much for "I will pass a parameter."
(On the other hand, the "dot it" at the end of the address suggests
Italian as a native language. The expression would be "passare un
parametro", and I can't quite imagine any context where I would
translate "passare" by "insert".) He does seem to distinguish between
functions and programs, too. What I would imagine is that he is trying
to find out how to do something like:
someFunction( argc, argv ) ;
where the signature of someFunction is something like:
void someFunction( int size, int array[] ) ;
| Quote: | Even more suspicious is "I need in another function."
I suspect that Marlen wants to type in
./programm 300 400
and then invoke
./anotherprog
which would read the input "my number is 300 and 300 + 100 = 400"
|
Or type in "programme 300 400" and call a function which takes int's?
| Quote: | If I'm right, Marlen wants to understand how Unix-style pipes work...
./programm 300 400 | ./anotherprog
The "|" character is the "Pipe" character; on most keyboards, it is
shift + "".
|
If the poster is having problems with English, I'd be very surprised
that his keyboard has the '|' character as shift+''. You can special
order US keyboards (often at extra price), but with most local
keyboards, you need some sort of contortions using Alt Gr to get a '|'.
(It's obvious that the inventors of C and Unix never had to deal with
European keyboards. If you're concerned about internationalization,
make sure that your user can input everything necessary using just the
invariable characters in ISO 646.)
If you are programming in C++, and you don't want carpien tunnel
syndrome, you're best bet is load the US keyboard driver, and learn to
type blind with it, so you can work whatever happens to be engraved on
the keys.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Apr 28, 2004 7:50 pm Post subject: Re: i have a little problem....with int main (....) |
|
|
| Quote: | bise79 (AT) hotmail (DOT) com (marlen at yahoo dot it) wrote
I've created a function int main(int argc, char* argv[])...and when
I run this programm I will inserted some parameter(especially
integer) whose I need in another function (for example: ./programm
300 400)....in my case I will, when run "./programm 300 400", show
"my number is 300 and 300 + 100 = 400". How can I do ?
[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote
I think you're all interpreting the OP's original request incorrectly.
Not the best English in the world. But "I will inserted some
parameter" seems a strange way to say "I will calculate some value."
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote
| Quote: | True, but it doesn't surprise me too much for "I will pass a parameter."
|
Maybe. It's impossible to say for sure unless Marlen replies.
| Quote: | (On the other hand, the "dot it" at the end of the address suggests
Italian as a native language.
|
Yes.
| Quote: | The expression would be "passare un
parametro", and I can't quite imagine any context where I would
translate "passare" by "insert".)
|
I wouldn't know.
| Quote: | He does seem to distinguish between
functions and programs, too.
|
Distinguish in the sense of using both words.
I've created a function...
When I run this program I will inserted some parameter...
whose I need in another function...
I'm not (yet) convinced that he understands the difference.
| Quote: | What I would imagine is that he is trying
to find out how to do something like:
someFunction( argc, argv ) ;
where the signature of someFunction is something like:
void someFunction( int size, int array[] ) ;
|
That does seem to be the group consensus.
| Quote: | I suspect that Marlen wants to type in
./programm 300 400
and then invoke
./anotherprog
which would read the input "my number is 300 and 300 + 100 = 400"
Or type in "programme 300 400" and call a function which takes int's?
|
Maybe.
| Quote: | If I'm right, Marlen wants to understand how Unix-style pipes work...
./programm 300 400 | ./anotherprog
The "|" character is the "Pipe" character; on most keyboards, it is
shift + "".
|
[Comments about Italian keyboards]
Perhaps I should have said "on most U.S. keyboards."
I was trying to be explicit about which character he should use, because
some (unusual in the US) keyboards have different keys for two different
vertical bars. To complicate the issue, if you use the ALT-number method
to enter characters, at least in my Windows locale, ALT+124 comes out as
the solid bar but ALT+127 comes out as the broken bar (which is character
code 166) and ALT+166 comes out with another character altogether (170).
| Quote: | If you are programming in C++, and you don't want carpien tunnel
syndrome, you're best bet is load the US keyboard driver, and learn to
type blind with it, so you can work whatever happens to be engraved on
the keys.
|
Could set me off on a whole new direction, only marginally on-topic to a
C++ newsgroup...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|