1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ newb :D

Discussion in 'Software Development' started by dez_666, Jul 5, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. dez_666

    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.
     
  2. dez_666

    dez_666 Thread Starter

    Joined:
    May 29, 2007
    Messages:
    842
    anyone wanna help me :3
     
  3. Shadow2531

    Shadow2531

    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;
    }
    
     
  4. dez_666

    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)?
     
  5. Shadow2531

    Shadow2531

    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
     
  6. WARnux

    WARnux

    Joined:
    Jun 29, 2007
    Messages:
    78
    Your avatar is quite distracting. Can you decrease the flashing?
     
  7. dez_666

    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?
     
  8. ferrija1

    ferrija1

    Joined:
    Apr 11, 2006
    Messages:
    7,954
    You don't have to do that, you could make the text #FFFFFF. ;)
     
  9. WARnux

    WARnux

    Joined:
    Jun 29, 2007
    Messages:
    78
    Good one! No, 666 does not scare me because I know where it comes from.
     
  10. dez_666

    dez_666 Thread Starter

    Joined:
    May 29, 2007
    Messages:
    842
    i didnt ask if it scared you and im sorry but i dont care :eek:
     
  11. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/592139

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice