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

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
The discussion revolves around understanding a C++ code snippet that reads characters into a char array and converts them into integers stored in an int array. Participants clarify that the char array is necessary for using string functions like strcmp, despite only holding one character at a time. The increment operation in the line "array[size++] = atoi(buff);" is confirmed to occur after the assignment, which explains why the integer array starts indexing from zero. Additionally, it is noted that the char array can hold multiple characters until the user presses enter, allowing for the conversion of multi-digit numbers. Overall, the conversation emphasizes the importance of understanding how input handling and array indexing work in C++.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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:
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:
# 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:
Technology news on Phys.org
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:
My first question is: if the character array only holds one character at a time

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


The second question is about this line

You have that right.
 
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.
 
Hurkyl said:
Are you sure it's only holding one character at a time?
I'm not actually - I thought it was, but I'm not certain.
 
maybe I don't understand how
cin >> buff;
is working?
 
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.)
 
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]?
 
Yes.
ATOI --> Array To Integer
 
  • #10
heh. I wondered what the A stood for. Seriously, I didn't know!
 

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
Replies
12
Views
2K
Replies
20
Views
2K
Replies
5
Views
2K
Replies
12
Views
3K
  • · Replies 65 ·
3
Replies
65
Views
7K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K