How to Create arguments? (Beginners trying to learn C++ here, sorry for the long paragraph)

I have only started to use the language c++ with microsoft visual studio a little over a week, and I have run into some problems that is given by my professor that I can't get my head around. So basically the problem gives a simple explanation or what argc variable is and how it works, and then it wants me to create 1, 2 or 3 arguments that will display some numbers when it is passed the program via command line. And the value of argc should be 2, 3 or 4 when I passed 1, 2 or 3 arguments to the program respectively.
This is what I have gotten so far:

// week3lab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, char*argv[])
{
int a = 2, b = 3, c = 4;
cout << a << endl << b << endl << c << endl;
cout << "\n";
return 0;
}

I am wondering if this is correct? I will attached the original problem below.

Problem :All user input for the Laboratory Tests are passed to your program via command line arguments, which
you can access using the ‘argc’ and ‘argv’ variables. Both of these variables are defined as part of the
main() function in your program. The argc variable is an integer and is assigned a value that
corresponds to the number of arguments that are being passed to the program via the command line at
EEET2246 – Engineering Computing 1 – Week 3 Laboratory Tasks
13/07/18 2
runtime. Therefore, as a programmer you can determine the number of arguments being passed to
your program by reading the value of the argc variable.
Write a program using a ‘cout’ statement that displays the value of argc when you run your program
with more than 1 command line argument. Demonstrate that when you pass 1, 2 or 3 arguments to
your program via the command line, that your program displays the correct number of arguments to
the console. The value of argc should be 2, 3 or 4 when 1, 2 or 3 arguments are passed to your
program, respectively.

Last edited on
I am wondering if this is correct?

No, sorry. Notice that you're never actually using argc or argv, despite the directions talking about it.

argc is the number of arguments, including the name of the program itself
argv contains the array of the arguments as text.

For example,
command-line: week3lab a b c
Will produce:
argc == 4
argv[0] == "week3lab"
argv[1] == "a"
argv[2] == "b"
argv[3] == "c"

Hint: The following will loop through every command-line argument.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main(int argc, char*argv[])
{
    for (int i = 0; i < argc; i++)
    {
        std::cout << argv[i] << "\n";
    }
}


Last edited on
Oh I see now, thank you so much for your help. Very much appreciated. :D
Topic archived. No new replies allowed.