Advise:???

Hello everyone,

I am working on a dimond shape. I have the code to print the dimond.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void draw_dimond(int x, int y, int len)
{
// check if input is an odd number, still have to do that.
for( x = 0; x < len; x++ )
{
	for( y = 0; y < len; y++ )
	{
		if( x < len/2 ) //top half of diamond
		{
		if( y == len/2 - x || y == len/2 + x )
			cout << "*";
		else
			cout << " ";
		}
		else    //bottom half of diamond
		{
		if( y == x - len/2  || y == len -(x len/2) -1  )
			cout << "*";
		else
			cout << " ";
		}
	}
cout << endl;
}


when i call this in main it prints it on screen

But the help I need with the argument passed to the function.

I want the arguments to be x and y cordinates for the console window.
So the image can be printed in the middle, end or so on...

I am new to C++, I was hoping someone knows how to solve this.

Thanks in advance for the help.

Do this for your main-function:

1
2
3
4
int main(int argc, char *argv[]) {
	draw_dimond(atoi(argv[1][0]), atoi(argv[2][0]), length);
	...


Where length is some variable. This is pretty crude, but should work:)
I do have more call functions on main()
I am not familiar with:
1
2
3
4
5
int main(int argc, char *argv[]) // what is going on here????!!!! 
{
	draw_dimond(atoi(argv[1][0]), atoi(argv[2][0]), length);
	...
}


Will this method effect my other functions?

Programs can be started with different parameters. When opening your program with these parameters, argc and argv are used.

argc means argumentcount (or, the number of arguments passed). If your program was called bob and you called your program like this: bob.exe lol lol
There would be 3 arguments. There is always at least one argument, which is the exact location of your program, and the program name. The next ones are all parameters.

argv is, as you can see, an array of char*. This means that it's an array of C-strings. These are the actual arguments themselves. In the previous example, this is what argv looks like:
1
2
3
4
5
{
 "bob.exe\0",
 "lol\0",
 "lol\0"
}

So argv[0] would return "bob.exe\0", argv[1] would return "lol" and for example argv[2][0] would return "l" (since that's the first character of the second parameter.

So, it won't affect any other functions, it just adds the possibility to open your program with parameters, to control it at the moment you call it.
Last edited on
The [0] is wrong in atoi(argv[1][0]) -- argv[1], argv[2] are char*, so you're trying to pass the first char of the arg to atol, which expects a (const) char*.

(the [0] is generally needed only when you're checking if the arg is a switch -- if(argv[i][0] == '-') ... /* then switch on argv[i][1] */

And I also suggest adding a couple of checks to main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
bool is_numeric(const char* value); // use isdigit() to check all chars are digits, or .

void usage(); // display usage message

int main(int argc, char *argv[]) {
    int ret_code = 0;

    if(3 != argc)
    {
        usage(); // tell user to provide x and y
    }
    else
    {
        int x = 0;
        int y = 0;
        int len = 10; // or whatever

        if(is_numeric(argv[1]))
        {
            x = atoi(argv[1]);
        }
        else
        {
            ret_code = 1;
            cerr << "error : invalid value for x" << endl;
        }

        if(is_numeric(argv[2]))
        {
            y = atoi(argv[2]);
        }
        else
        {
            ret_code = 1;
            cerr << "error : invalid value for y" << endl;
        }
            
        if(ret_code == 0)
            draw_dimond(x, y, length);
        else
            usage();
    }

    return 0;
}


Andy

P.S. The check could be factored out/done more slickly. But this is the first solution that occured to me.
Last edited on
thanks everyone
Topic archived. No new replies allowed.