Having trouble converting a variable

This is a discussion on Having trouble converting a variable within the C++ Programming forums, part of the General Programming Boards category; I'm having a terrible time trying to convert a variable. When I convert the value of argv[1] from char* to ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Having trouble converting a variable

    I'm having a terrible time trying to convert a variable. When I convert the value of argv[1] from char* to int using atoi, it just doesn't seem to come out right. Anytime I use a two-digit number as my argument, it comes out as 15, regardless of what number it was I typed. With three digit numbers, it's 16. What exactly am I doing wrong here? It works fine when I use the prompt rather than specify an argument.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main ( int argc, char *argv[] )
    {
        int a;
        int b;
        int c;
        int count;
        int endset;
        unsigned keepLooping;
    
        if (argc != 1) {endset = atoi(argv[1]);
                 goto noprompt; }
    
        cout << "\nFibonacci's Number Sequence\n";
        cout << "Input how many numbers to go through: ";
        cin >> endset;
        
        noprompt:
        count = 0;
        endset -= 1;
        if (endset == 0) {
                   cout << "\n1\n";
                   return 0; }
        if (endset < 0) {
                   cerr << "\nInvalid number specified\n";
                   return 1; }
        a = 0;
        b = 1;
        keepLooping = 1;
        while (keepLooping)
        {
              count += 1;
              c = a + b;
              if (count == 2) {
                        c = 1;
                        a = 1;
                        b = 1; }
              cout << "\n" << c;
              a = b;
              b = c;
              if (count > endset)
              {
                        keepLooping = 0;
                        cout << "\n";
                        return 0;
              }
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (1) Just from a theoretical perspective, why use goto when you can use ... else? Also from a theoretical perspective, your return 0 is probably not where you want it to be.
    (2) I don't see anything wrong with atoi here, unfortunately; does argv[1] contain what you want when you start? (I.e., print it out to check.)

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Both 'return 0' lines are fine. The first one is for when you specify 1, so it will just print that one number and not go through the rest of the program. The second one is after everything has finished.

    And yeah, I've had the contents of the different variables printed out at multiple locations in the program. argv[1] always has whatever argument I specify at the command-line.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then it works I guess. I just compiled your code, ran it as "temp 24" and got 24 lines of output, with the last one being 46368.

    And of course, my statement about your return 0 was theoretical: you get the right answer where it is, of course, just like you get the right answer with your goto. I'd take points off for either one all the same. (I would recommend the final return 0 to go at the end, not hidden inside an if statement or a while loop. What if I made you output something else afterwards, or do this interactively in a loop?)

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    The code I posted was slightly different from what I was using. I moved the variable declarations around to prevent having to place them in multiple areas of the file (once for argument specification, once for prompted input). I'm really not sure why that caused the error, but the code is working fine now.

    Thanks for the help. I will relocate the "return 0" command for the reason you specified, but I like my "goto". =P

    Thanks again.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    but I like my "goto".
    I don't think this is just a matter of taste.

    There are other things that might be simplified.

    Why not?
    Code:
    while (count < endset)
    ...
    This would do away with the manipulation of endset before the loop and the conditional check at the end of the loop.

    I also believe you should output the value of b, not c. This would do away with the special case if count == 2 and endset being 1:

    1) 0 + 1 = 1
    2) 1 + 1 = 2
    3) 1 + 2 = 3
    4) 2 + 3 = 5
    5) 3 + 5 = 8
    etc.
    The bold items should nicely output the sequence. The red items don't (hence your special case when count == 2?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by KasMage View Post
    but I like my "goto". =P
    I'm sure your teacher won't like it though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. converting variable to bits?
    By draggy in forum C Programming
    Replies: 20
    Last Post: 07-13-2005, 08:42 AM
  3. variable type char to variable type int
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-15-2002, 08:52 AM
  4. Converting CString to int ??
    By eko-eko in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2002, 07:45 PM
  5. Replies: 8
    Last Post: 04-22-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21