1. Int Arguments

This is so great! This is a great board, a lot of helpful people here and I look forward to learning a lot here. I just finished "Teach Yourself C++ in 21 Days", so I'm not a complete newb, but I definately need to learn a lot more. So everyone prepare for my MANY MANY questions!

Heres a question to get things rolling

Code:
//  Triangle computation program

#include <iostream>
using namespace std;

int main(int argc, int *argv[])
{
int a, b, c;
//cout << argch;
cout << "\n";
cout << argv;
cout << argv;
cout << argv;

if (argc > 4 || (argc < 4 && argc > 1))
{
cout << "Invalid number of sides.\n";
cout << "Implementation: <filename> sideA sideB sideC\n";
}

if (argc == 4)
{
a = argv;
b = argv;
c = argv;
cout << "Calculating based on given arguments.\n";
}

if(argc == 1)
{
cout << "Enter side 1: ";
cin >> a;
cout << "Enter side 2: ";
cin >> b;
cout << "Enter side 3: ";
cin >> c;
cout << "Calculating...\n";
}

if ( a == b && a == c && b == c )
cout << "This is an equilateral Triangle.\n\b";

if ( (a == b && a !=c) ||
(a == c && a !=b) ||
(b == c && b !=a) )
cout << "This is an isocles Triangle.\n\b";
if ( ((a*a)+(b*b)== (c*c)) ||
((b*b)+(c*c)== (a*a)) ||
((c*c)+(a*a)== (b*b)) )
cout << "This is a right triangle.\n\b";
else
cout << "Please check the numbers and try again!\n";

return 0;
}
The only thing I couldn't get to work is for this program to use integers it receives from the command line. How can I get this to work? Thanks.
-Peace 2. Re: Int Arguments

Originally posted by Scytz0
Code:
int main(int argc, int *argv[])
this should be:
Code:
int main( int argc, char *argv[] )
{
//and now you need to parse argv for the arguments...remember
//that arguments are usualy seperated by a flag ( - )
//...so if running a prog with arguments you would have somthin
//like this:
//user~>main -nrows -ncols
....
axon 3. Thanks, I'll try that.

// ** EDIT **

Does this look right?

Code:
//  Triangle computation program

#include <iostream>
using namespace std;

int main(int argc, char argv[])
{
int a, b, c;
//cout << argch;
cout << "\n";
cout << argv;
cout << argv;
cout << argv;

if (argc > 4 || (argc < 4 && argc > 1))
{
cout << "Invalid number of sides.\n";
cout << "Implementation: <filename> sideA sideB sideC\n";
}

if (argc == 4)
{
a = argv;
b = argv;
c = argv;
cout << "Calculating based on given arguments.\n";
cout << a << "\n" << b << "\n" << c << endl;
}

if(argc == 1)
{
cout << "Enter side 1: ";
cin >> a;
cout << "Enter side 2: ";
cin >> b;
cout << "Enter side 3: ";
cin >> c;
cout << "Calculating...\n";
}

if ( a == b && a == c && b == c )
cout << "This is an equilateral Triangle.\n\b";

if ( (a == b && a !=c) ||
(a == c && a !=b) ||
(b == c && b !=a) )
cout << "This is an isocles Triangle.\n\b";
if ( ((a*a)+(b*b)== (c*c)) ||
((b*b)+(c*c)== (a*a)) ||
((c*c)+(a*a)== (b*b)) )
cout << "This is a right triangle.\n\b";
else
cout << "Please check the numbers and try again!\n";

return 0;
}
This is what I type at the command line

C:\Documents and Settings\admin\Desktop>tri2 -3 -4 -5

and I get this:

8" Calculating based on given arguments.
56
34
0
Please check the numbers and try again!

What am I doing wrong? 4. axon's advice was correct, but you didn't quite follow it exactly. He said
int main( int argc, char *argv[] )
but you used
int main( int argc, char argv[] )
Once you fix that your code won't work still because you are setting a char array to an int. Since argv is an array of char* objects, that means it is 0 or more char* strings. So argv is a null terminated string, as is argv, argv and argv (assuming your example arguments).

I'd suggest using the standard string class to hold those strings, but whichever way you do it you'll have to read up a bit on it to properly save the values as ints. 5. Could you nudge me in the right direction or give me a hint as to how I would use a string to accomplish this? Thanks.
-Peace 6. jlou is right..

>>C:\Documents and Settings\admin\Desktop>tri2 -3 -4 -5

if these are your arguments, trace back what happens to them.
argc is set to 4. So the first element of argv is tri2. Anyways, below I'm attaching a prog that parses a command line which I wrote not so long ago...here is what it supposed to do...it does have one bug when your command starts out with an alpha char.
Code:
//argument:    main 234aa34  34b  23  3b4
//display:
234
34
34
23
3
4
as you can see the prog seperates the integesr from the characters. each time an char or white space is encoutered go to new line.
Code:
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
int a=0; //counter for amount of letters
char c;

if ( argc == 1 ) {	//check for amount of arguments
cout << "\nThere is nothing to parse!\n";
cout << endl;
} else {
for ( int i = 1; i < argc; i++ ) {
for ( int j = 0; j < strlen(argv[i]); j++ ){
c = argv[i][j];	//step through each string and
if ( isdigit(c) ) { //check if value is a digit
if ( a > 0 ) {
cout << endl;
a=0;
}
cout << argv[i][j];// << endl;
}else { a++; } //if value is not digit increment a
}
cout << endl;
}
}
return 0;
}
axon 7. Originally posted by Scytz0
Could you nudge me in the right direction or give me a hint as to how I would use a string to accomplish this? Thanks.
-Peace
one word atoi()

do a little search for it. It should also be in your book. Popular pages Recent additions 