Using a main function to print sum of two values?

The comments in the code indicates what I am trying to do, however this is all I am able to come up with. But from here I am stuck. Is there a way to make this simpler and modify it to do what I would like it to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Write a main function that takes two values
// as arguments and prints their sum

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char **argv)
{
    string str;
    for(int i = 1; i != argc; ++i)
        str += string(argv[i]) + " ";

    cout << str << endl;
    return 0;
}


You can use a string constructor to make a complete string fromt he char array:
1
2
string string_one(argv[1]);
string string_two(argv[2]);


You can convert string to int: http://www.cplusplus.com/reference/string/stoi/

Create two strings, then create two int, then add the int together.
Hello CodingIsHard17,

Based on the comment in the code:

// Write a main function that takes two values
// as arguments and prints their sum


One would think that your two values would be numbers. Yet you are dealing with them as "strings".


Is there a way to make this simpler and modify it to do what I would like it to do?



I see no way to make this any simpler right now. It contains all the cede necessary to get the job done. Beyond that I have no idea what you want it to do.

An example of what is on the command line and what you are wanting for output would help.

Andy
takes two values
for(int i = 1; i != argc; ++i)

Two. Exactly two.

You loop does not take exactly two values. It could take 0, 1, 2, 3, ...

If argc<3, then you don't have at least two arguments.
So how do I not deal with them as strings and how can I modify that code to only take 2 values?
The above posters are trying to help you, and Handy Andy in particular has requested something to help him, help you.
Andy wrote:
An example of what is on the command line and what you are wanting for output would help.


argc is the number of arguments passed into the program, including the name of the program itself.

e.g. calling
my_program arg1 arg2 would make argc == 3. (2 "real" arguments).

If you want to avoid processing anything when argc does not equal 3, then add that restriction in your program.
Something like:
1
2
3
4
5
6
7
8
if (argc == 3)
{
    // process argv[1] and argv[2]
}
else
{
    cout << "Invalid arguments\n";
}


Please read Repeater's response and follow the links that show you how to convert a string into an integer.
Last edited on
This is all that I am able to come up with but now there is the issue of not having it in a main function.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std; 
int main() {
   int n1, n2;
   cout<<"Enter first number: ";
   cin>>n1;
   cout<<"Enter second number: ";
   cin>>n2;
   cout<<"Sum = "<<(n1+n2)<<endl;
    return 0;
}
Hello CodingIsHard17,

This is all that I am able to come up with but now there is the issue of not having it in a main function.
Not having what (it) in the "main" function?
I asked you for an example of what is on the command line. And what your expected output should be.
I am still waiting.
Your last bit of code is adding two numbers.
Should I take this as being what you need?
I came up with this. It will work with "strings" or numbers.
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
// Write a main function that takes two values
// as arguments and prints their sum

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

using namespace std;

int main(int argc, char **argv)
{
	string str;
	int num1{}, num2{};

	if (argc < 3)
	{
		std::cout << "\n Must have at least two strings or numbers.\n" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread". Optional along with the header files.
		return 1;
	}

	try
	{
		num1 = stoi(argv[1]);
		num2 = stoi(argv[2]);

		std::cout << "\n num1 + num2 = " << num1 + num2 << std::endl;
	}
	catch (const std::exception& e)
	{
		std::cout << "\n " << e.what() << '\n' << std::endl;
			
		str += ' ' + string(argv[1]) + " ";
		str += string(argv[2]);
	}

	//cout << argv[i] << ' ';
	cout << str << endl;

	return 0;
}

Note: a few well placed blank lines makes it much easier to read.

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.