|
-
Jan 25th, 2003, 03:22 PM
#1
Thread Starter
Frenzied Member
Console (command line)
ok i know some c++ cuz of you people out her thanks now i want to know about the command line arguments . Do they have to do with
VB Code:
int main (int nArgs, char* pszArgs[])
if i do this
VB Code:
#include <stdio.h>
#include <iostream.h>
int main (int nVal, int nVal2, char* pszArgs[])
{
int nVal3 = nVal + nVal2;
cout << "The total is: " << nVal3;
return 0;
}
Will it work? Like how would i enter the command line arg?
like
Add.exe 3, 4 is incorrect but whats the right way
Last edited by JasonLpz; Jan 25th, 2003 at 03:25 PM.
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 05:04 PM
#2
No, the command line arguments don't work like that. The arguments are passed as an array of char*s, the first argument of main is the number of arguments in the array, the second is the array itself:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
if (argc!=3) {
cerr << "You must pass two arguments" << endl;
}
int val1 = atoi(argv[1]);
int val2 = atoi(argv[1]);
cout << val1 << " + " << val2 << " = " << (val1+val2) << endl;
return 0;
}
Note that the first argument of argv is always the name of the program as it is called (therefore argc will be one more then the actual number of arguments)
-
Jan 25th, 2003, 05:18 PM
#3
Thread Starter
Frenzied Member
how do i enter the 2 args now?
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 05:22 PM
#4
Thread Starter
Frenzied Member
Oops theres your problem thanks its great now
VB Code:
int val1 = atoi(argv[1]);
int val2 = atoi(argv[2]);
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 06:18 PM
#5
Thread Starter
Frenzied Member
ok great so far so good. Now i have 1 problem i made this program with the info u gave me. Now i get this error not when i compile but when i run this without any arguments. how can i catch this error and stop it like if they dont type any arguments then show the
VB Code:
if (sVal == 0) {
cout << "You must select a program to run. Press number to run program.\n";
cout << "\n1 = Adding Application";
cout << "\n2 = Subtract Application";
cout << "\n3 = Multiply Application";
cout << "\n4 = Divide Application\n\n\n";
}
VB Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int sVal = atoi(argv[1]);
if (argc!=3) {
cerr << "For A list or programs press 0.\n\n"<< endl;
}
if (sVal == 0) {
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n";
cout << "||You must select a program to run. Press number to run program.||\n";
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
cout << "\n||1 = Adding Application ||";
cout << "\n||2 = Subtract Application ||";
cout << "\n||3 = Multiply Application ||";
cout << "\n||4 = Divide Application ||\n";
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n\n\n"
}
if (sVal == 1)
{
int a1;
int a2;
int a3;
cout << "Enter numbers below to add\n";
cout << "1:";
cin >> a1;
cout << "2: ";
cin >> a2;
a3 = a1 + a2;
cout << "\n\n The Sum is: " << a1 << " + " << a2 << " = " << a3;
}
if (sVal == 2)
{
int a1;
int a2;
int a3;
cout << "Enter numbers below to subtract\n";
cout << "1:";
cin >> a1;
cout << "2: ";
cin >> a2;
a3 = a1 - a2;
cout << "\n\n The Subtracted sum is: " << a1 << " - " << a2 << " = " << a3;
}
if (sVal == 3)
{
int a1;
int a2;
int a3;
cout << "Enter numbers below to multiply\n";
cout << "1:";
cin >> a1;
cout << "2: ";
cin >> a2;
a3 = a1 * a2;
cout << "\n\n The Multiplied sum is: " << a1 << " x " << a2 << " = " << a3;
}
if (sVal == 4)
{
int a1;
int a2;
int a3;
cout << "Enter numbers below to divide\n";
cout << "1:";
cin >> a1;
cout << "2: ";
cin >> a2;
a3 = a1 / a2;
cout << "\n\n The Divided sum is: " << a1 << " / " << a2 << " = " << a3;
}
return 0;
}
Last edited by JasonLpz; Jan 25th, 2003 at 06:34 PM.
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 06:20 PM
#6
Thread Starter
Frenzied Member
oh yeah in VB there is a easy to use SELECT CASE is there something like this in c++ (there must be ) Can someone tell me something about that plz thank you all
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 07:29 PM
#7
There's a switch in C++ that does the same as select in VB. It can't handle strings though.
As for this:
int sVal = atoi(argv[1]);
Don't access any command line arguments without having checked that there really is something! If the user starts your app without arguments then you'll probably end up with an access violation.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 25th, 2003, 07:55 PM
#8
Thread Starter
Frenzied Member
ok thanks got it
VB Code:
int main(int argc, char** argv)
{
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n";
cout << "||You must select a program to run. Press number to run program.||\n";
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
cout << "\n||0 = This list ||";
cout << "\n||1 = Adding Application ||";
cout << "\n||2 = Subtract Application ||";
cout << "\n||3 = Multiply Application ||";
cout << "\n||4 = Divide Application ||\n";
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n\n\n";
int sVal;
if (argc!=3) {
cerr << ""<< endl;
}
if (argc!=1){
sVal = atoi(argv[1]);
}
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 25th, 2003, 08:00 PM
#9
Thread Starter
Frenzied Member
another question. I noticed my programs wil close if i just double click them is there a way to change this?
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 26th, 2003, 05:26 AM
#10
I guess they open, notice that there are insufficient arguments and thus end. The console window is not kep open.
To make up for that you can either start the app from a console window or add a line of code that keeps the window open (like cin.ignore(1); )
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|