C++ newb :D

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

dez_666

Thread Starter
Joined
May 29, 2007
Messages
842
Hi im like just learning C++ and i decided to take a big leap by going from knowing how to use cout to making command line parameters. :D

So i tried this..

Code:
#include <cstdlib>
#include <iostream>

/*  int Add (char x,char y)
	{
	 		return(x+y);
}

*/

int main(int argc, int argv[])

{
 	
 	using std::cout;
 	using std::endl;
 	
    cout << argv[0]+argv[1] << endl;
}
And yes, the commented part was going to be a function but i couldnt get it to work so i just commented it for later experimentation. So when i run this by typing in "adder.exe 1 2"
i get an output of "8013972". can someone tell me what im doing wrong? :rolleyes:
its probably something stupid that i missed.
 
Joined
Apr 30, 2001
Messages
2,636
Can't test this at the moment, but try:

Code:
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;

int cstringToInt(const char* s) {
    istringstream str(s);
    int i;
    str >> i;
    return i;
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        cout << "Usage: this.exe int1 int2" << endl;
        return EXIT_FAILURE;
    }
    cout << cstringToInt(argv[1]) + cstringToInt(argv[2]) << endl;
}
 

dez_666

Thread Starter
Joined
May 29, 2007
Messages
842
:D it works.

but can you explain to me how to turn cmd line parameters into integers (so they can be added n stuff)?
 
Joined
Apr 30, 2001
Messages
2,636
char* argv[] represents an array of C-style strings

If you run:
test.exe 1 2

argv[0] will be "test.exe".
argv[1] will be "1".
argv[2] will be "2".

argc represents the size of the array. In this case, it's 3.

So, to add 1 + 2, you need to convert "1" to 1 and "2" to 2. cstringToInt is one way to do that.

cstringToInt uses istringstream to do the conversion. You start off by contructing the stream to have a value of the C-style string you're passing to the cstringToInt (argv[1] and argv[2] in this case). Then, you create an int and do str >> i to grab the input from the string and dump it to the int. When you do this, it knows how to handle the conversion (in basically the same way cin >> i does).

However, instead of cstringToInt, you can use atoi(argv[1]) and atoi(argv[2]) if you want. They do about the same thing. See http://www.cppreference.com/stdstring/atoi.html
 

dez_666

Thread Starter
Joined
May 29, 2007
Messages
842
hmm. would you rather have it be text color RGB 0,0,0 and background color Hex 000000?
 
Joined
Apr 11, 2006
Messages
7,954
dez_666 said:
hmm. would you rather have it be text color RGB 0,0,0 and background color Hex 000000?
You don't have to do that, you could make the text #FFFFFF. ;)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top