Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

(simple)C++ questions about a piece of code

  1. Feb 21, 2005 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I am looking at a piece of code from my teacher's program and I am just a little unsure of about how a couple of things work. A char array is declared, and an int array is declared. It looks like a character is read from the keyboard and stored in the char array, then the character is transformed into the integer value it represents, and put into an int array. This process repeats until a Q or q is entered.

    My first question is: if the character array only holds one character at a time, why is an array used? Couldn't a char type variable hold it temporarily then be re-used? But I probably just don't understand what's actually going on here.

    The second question is about this line
    Code (Text):
    array[size++] = atoi(buff);
    what I think is happening is that array[size] gets assigned the value from atoi(buff) and then the variable called size is incremented. Is that how it works? I'm just a little unused to seeing the increment happen there. If he had written array[++size] would there be an increment and then an assignment at the new index position?

    Thanks - here's the code:
    Code (Text):

    # include <iostream>
    using namespace std;

    int main()
    {
        char buff[80];
        int array[1000];
        int size = 0;

        cout << "Type in some numbers. Type Q to quit."<< endl;
       
        do
        {
            cin >> buff;
            if (strcmp(buff,"Q")&& strcmp(buff, "q"))
                array[size++] = atoi(buff);

        } while(strcmp(buff,"Q")&& strcmp(buff, "q"));

       
        for (int i = 0; i< size; ++i)  
            cout << array[i];
       
        return 0;
    }
     
     
    Last edited: Feb 21, 2005
  2. jcsd
  3. Feb 21, 2005 #2
    The array does only use one character at a time, however, to use the string function strcmp you need to have a string, basically an array of chars is necessary. The length of the array doesn't matter, if you were to make it an array buff[1] it would work the same way.

    array[size++] = atoi(buff);

    For this line, what is being done is exactly what you said, the character buff is being changed into an integer and then is being added to the int array "array". For the life of me at this point I can't remember if it increments before or after it is assigned the value, I think it is after, but I could be wrong.


    ~Lyuokdea
     
    Last edited: Feb 21, 2005
  4. Feb 21, 2005 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Are you sure it's only holding one character at a time?


    You have that right.
     
  5. Feb 21, 2005 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks, Lyuokdea. That makes sense! I did not consider what was necessary to use strcmp().
    I believe the assignment must be happening before the increment since when I print the int array in the for loop I start at 0 rather than 1.
    Also, I just went back and changed that line to
    array[size++] = atoi(buff);
    and it spit out a lot of garbage, so I'm sure now that's the case.
    Thanks for your help.
     
  6. Feb 21, 2005 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I'm not actually - I thought it was, but I'm not certain.
     
  7. Feb 21, 2005 #6

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    maybe I don't understand how
    cin >> buff;
    is working?
     
  8. Feb 21, 2005 #7
    cin >> buff; keeps adding the characters that are typed until you press <enter>. When you press <enter> the characters are converted to an integer and added to the integer array. Many integers are more characters like 124 (three characters but just one integer), that is why an array of characters is used. The program will run into problems when someone tries to enter a number that has more than 80 digits...

    (this was already clear but: ++A means first increment A then use it and A++ means first use A and then increment it.)
     
  9. Feb 21, 2005 #8

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks, gerben. So if I enter 124, and press enter, buff[0] gets 1, buff[1] gets 2 and buff[2] gets 4? Then everything in buff gets converted to the integer it represents and the integer 124 gets assigned to array[0]?
     
  10. Feb 21, 2005 #9
    Yes.
    ATOI --> Array To Integer
     
  11. Feb 22, 2005 #10

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    heh. I wondered what the A stood for. Seriously, I didn't know!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: (simple)C++ questions about a piece of code
  1. C question (Replies: 19)

  2. C++ question (Replies: 8)

  3. C question (Replies: 6)

Loading...