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

  • C/C++
  • Thread starter Math Is Hard
  • Start date
  • Tags
    Code
In summary: Thanks for the clarification. Are you sure it's only holding one character at a time?Yes, it is only holding one character at a time.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
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
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
  • #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:
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
maybe I don't understand how
cin >> buff;
is working?
 
  • #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.)
 
  • #8
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]?
 
  • #9
Yes.
ATOI --> Array To Integer
 
  • #10
heh. I wondered what the A stood for. Seriously, I didn't know!
 

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

1. What is the purpose of the "using namespace std;" statement in C++?

The "using namespace std;" statement allows you to access all standard library functions and objects in C++ without having to specify the "std::" prefix every time. This can save time and make your code more concise.

2. How do I declare and initialize a variable in C++?

To declare and initialize a variable in C++, you can use the following syntax:
data_type variable_name = value;
For example, to declare and initialize an integer variable named "num" with the value 10, you can write:
int num = 10;

3. What is the difference between = and == in C++?

The = operator is used for assignment, meaning it assigns a value to a variable. For example,
int num = 10;
The == operator is used for comparison, meaning it checks if two values are equal. For example,
if (num == 10) {
    // do something
}

This will only execute the code inside the if statement if the variable "num" is equal to 10.

4. How do I print something to the console in C++?

To print something to the console in C++, you can use the "cout" function from the iostream library. For example, to print the string "Hello World!" to the console, you can write:
cout << "Hello World!";
This will output:
Hello World!
You can also use the "endl" statement to add a line break.

5. How do I use a for loop in C++?

To use a for loop in C++, you can use the following syntax:
for (initialization; condition; increment/decrement) {
    // code to be executed
}

The "initialization" step is where you declare and initialize the variable that will control the loop. The "condition" step is where you specify the condition that must be met for the loop to continue. The "increment/decrement" step is where you specify how the controlling variable will change after each iteration of the loop. For example, to print the numbers 1 to 10 to the console, you can write:
for (int i = 1; i <= 10; i++) {
    cout << i << endl;
}

This will output:
1
2
3
...
10

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
65
Views
5K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
5
Views
921
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top