- #1
- 4,652
- 37
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
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:
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:
array[size++] = atoi(buff);
Thanks - here's the code:
Code:
# 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: