0

I'm studying a code that writes text on the heap:

#include <iostream>
using namespace std;

int main() {
    
    char c;
    int count{0},d{0};
    char *x;
    x = new char[1000];
    if(x != NULL){
        cout << "enter the text,ending with by eof marker" << endl;
        for(count=0; count!=cin.eof()&&count<1000;){
            cin >> c;
            if(!cin.eof())
            x[count++] = c;
                
        }
        for(int d=0; d<count;d++)
        cout << *(x+d);
        cout << "end of text" << endl;
    }

I have a problem understanding this snippet:

x[count++] = c;

Can anyone explain it to me?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `x[count++] = c;` is equivalent to `x[count] = c; count = count + 1;`. – 0x5453 Feb 17 '21 at 22:19
  • 5
    I would be wary of wherever you got that code from. This is far from an example of good C++ code. Checking if `new` returns `NULL`, comparing `cin.eof()` with an int, declaring two variables named `d` and only using one---none of this makes any sense. – Brian61354270 Feb 17 '21 at 22:21
  • 1
    Particularly, there is a memory leak (missing `delete[] x`) and `count != cin.eof()` doesn't make any sense. – jtbandes Feb 17 '21 at 22:22
  • thank you guys, this is actually from my professor's solution! – jujubeefei Feb 17 '21 at 22:46
  • 2
    @jujubeefei then your professor is teaching you bad C++. There are a lot of mistakes in this code. – Remy Lebeau Feb 17 '21 at 22:46
  • This may be the *worst* possible way to read a string from the console. Also, fix your indentation. It'll save you headaches in the future. I'd ask your prof why you're using C-style strings in C++, and why you're providing initializers for types that are default initialized to 0. That typically only happens when Java people decided that they're C programmers. Everything about this solution is wrong except that it *happens* to work, despite the author's best efforts. – 3Dave Feb 17 '21 at 22:53
  • The integers would be uninitialized if no initializer were provided: https://stackoverflow.com/questions/6032638/default-variable-value – jtbandes Feb 17 '21 at 23:13
  • This code could be simplified a lot, for example: `cin.read(x, 1000);`. – jtbandes Feb 17 '21 at 23:17

1 Answers1

3

This is doing a post-increment of count. The value of c will be assigned to x[i], where i is the old value of count.

It's equivalent to:

x[count] = c;
count = count + 1;
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • Hi jtbandes, thanks for your help. I agree with you that count != can.eof() doesn't make sense.So, what is the correct way to rewrite this snippet code? – jujubeefei Feb 17 '21 at 22:56
  • Well, `cin.eof()` is a bool so you could just delete the `count !=`. On the other hand, the `if` inside the loop is already checking for eof, so you could add `else { break; }`. – jtbandes Feb 17 '21 at 23:10
  • @jujubeefei An alternative: `while (count < 1000 & cin >> c) { x[count++] = c; }` In English, While we still have space in the array and we successfully read a character from the input stream, store the character in the array and increase the counter by 1. – user4581301 Feb 18 '21 at 00:20
  • @user4581301 I think you meant `&&` :) – jtbandes Feb 18 '21 at 02:38
  • Smurf. I'm writing that off as a typo. – user4581301 Feb 18 '21 at 03:00