I apologize immediately for such a cumbersome code, it’s me so that you understand more clearly. I want every time I enter an element, the counter counts the number of characters in the element and when outputting elements it also displays the number of characters (TOTAL) in the elements below it. Not in one, but in all elements (5, 10, 15, etc.). It is not for you to decide for me, I just coped with one of the elements, but with a few not, please help with this.

#include <iostream> #include <conio.h> #include <string.h> using namespace std; struct element { string data; element *adress; }; class List { private: element * StartAdress, *FinishAdress; public: List() { StartAdress = NULL; FinishAdress = NULL; } void ElAdd(string a) { element *e; e = new element; e->data = a; /*e -> adress = StartAdress; StartAdress = e; */ if (StartAdress == NULL) { StartAdress = e; } else { FinishAdress->adress = e; } FinishAdress = e; FinishAdress->adress = NULL; } void print() { element *e; for (e = StartAdress; e != NULL; e = e->adress) cout << e->data << " " << endl; } }; int main() { List L; string x; cout << "Bufer is Empty! nn"; cout << "For the Add Elements Press 'A':n"; { char v = getch(); if (v == 'a') { cout << endl; cout << "Enter Elements: n"; start: getline(cin, x); L.ElAdd(x); cout << "--------n"; } cout << "Elements of Your List:nn"; L.print(); cout << "--------nn"; cout << "For the Continue of Adding Press 'P':n"; cout << "For the Clear Buffer Press 'C':n"; v = getch(); if (v == 'p') { cout << "Enter the Next Element: n"; goto start; } } system("PAUSE"); return EXIT_SUCCESS; } 

what you advised me displays errors, which I described in the 7th comment of the 1st answer. Please help me with errors.

  • 2
    Without thinking about the rest of the code: Your code does not correspond to modern programming concepts, which means it is initially incorrect. goto start; PS all the more so that the start label is inside if , and it’s generally a horror. - ReinRaus
  • and what can be added there instead? so that the same effect was? And at the expense of PS I need him to start from there, that's why navi1893
  • one
    @ nav1893 try to rewrite yourself without goto , if it doesn't work out, ask a new question, and in the future forget the word goto forever. This is just a little advice. - ReinRaus
  • thank! I will try. And on account of the length of the lines can not add anything? - navi1893
  • @ navi1893, you, I remember, had a similar question about adding / deleting with the list. You were advised to use the switch statement inside while. IMHO is the same here. - avp 7:44 pm

2 answers 2

It is necessary in the List class to add a private variable of type int and when adding an element add the value of the length of the element to this variable

 totalSymbols += a.lenght(); 

and when output, also output the value of the variable totalSymbols .

  • why it gives the error: expected `; ' before '+ =' token, on the same line with your code ??? - navi1893
  • and variable in class List with type int and name totalSymbols declared? - CyberCoder
  • yes, it gives an error there too! = / I did this: private: element * StartAdress, * FinishAdress; int TotalSymbols + = x.length (); Here is an error! And I wrote x.length (), right? I'm entering the 'x' - navi1893
  • one
    no, first, in the List class, add private int totalSymbols = 0; then in the ElAdd function, do totalSymbols + = a.lenght () ;. a because the parameter a is passed to the function, in the declaration void ElAdd (string a) - CyberCoder
  • one
    No @ navi1893. At this point, you simply declare an int TotalSymbols. In the constructor, reset it, similar to StartAdress and FinishAdress. And to increase in the ElAdd () method TotalSymbols + = a.length (); When you have a method that removes the list item, do not forget to reduce TotalSymbols. - avp

I am not a sishnik, to be honest, I write exaggerated and maybe not correctly. I will accept corrections to errors with joy, I will increase my knowledge of C #.

 int total=0; getline(cin, x); while (x=='a'){ getline(cin, x); L.ElAdd(x); total+=x.length(); cout<<total; getline(cin, x); // если введут А то заново }; 
  • In while operation == for comparison, string with char (symbol) is not defined. But it can be compared to "a", because getline for string discards '\ n' - avp
  • That is, 'a' and "a" are different things? .. - ReinRaus
  • For sure. 'a' is a character, char, one byte. "a" is a string, an array of char, is terminated by a zero byte (the length of the string is not taken into account) is often written, for example, char * str ie pointer, address. In this case, the string (its bytes with a zero at the end) is located at the address stored in the variable str. - avp