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

  • Context: C/C++ 
  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around understanding a piece of C++ code that involves reading characters from input, converting them to integers, and storing them in an integer array. Participants explore the use of character arrays, the behavior of the `atoi` function, and the increment operation in array assignments.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why a character array is used if it only holds one character at a time, suggesting a single char variable might suffice.
  • Another participant explains that the character array is necessary for using the `strcmp` function, although they note that the length of the array does not affect functionality.
  • There is a discussion about the line `array[size++] = atoi(buff);`, with participants agreeing that it assigns the integer value from `atoi(buff)` to `array[size]` and then increments `size`, though there is uncertainty about the exact order of operations.
  • One participant reflects on their understanding of the increment operation, suggesting that the assignment occurs before the increment based on their observations when printing the integer array.
  • Another participant clarifies how `cin >> buff;` works, indicating that it reads multiple characters until the enter key is pressed, which leads to confusion about how many characters are actually stored in the array.
  • There is a clarification that when entering a number like 124, the characters are stored in the array such that `buff[0]` holds '1', `buff[1]` holds '2', and `buff[2]` holds '4'.
  • A participant humorously expresses confusion about the acronym ATOI, which stands for "Array To Integer".

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the code and the behavior of the `atoi` function, but there remains uncertainty regarding the exact workings of the character array and the increment operation in the assignment. Some participants express doubt about whether the character array truly holds only one character at a time.

Contextual Notes

There are limitations in understanding how `cin >> buff;` interacts with the character array, particularly regarding the number of characters that can be stored and potential issues with input exceeding the array's capacity.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 65 ·
3
Replies
65
Views
8K
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K