   Test Program
  I've been trying to learn c++ for a month now with a decent amount of success and with the help of "C++ Primer Plus" by Stephen Prata. From what I have read, the book as been amazing.Back on topic though. Like most books, at the end of each chapter there are test excercises to complete.This one was to test what you had learned about loops.The point of the program was to use nested loops to ask a user to enter a number, and then to create a box of that number, with that many asteriks in each row, preceded by periods.For example, if you entered 3, you'd get back..*.*****Would this code for doing it be considered efficient?#include <iostream>#include <string>int main(){using namespace std;cout << "Enter the number of rows: ";int num;cin >> num;string * row = new string[num];    for (int x = 0; x < num; x++){for (int y = 0; y < num; y++){row[x][y] = '*';for (int b = x+y+1; b < num; b++)row[x][y] = '.';cout << row[x][y]; }cout << endl;}delete [] row;cin.get();cin.get();return 0;}
Quote:Original post by Jrok138I've been trying to learn c++ for a month now with a decent amount of success and with the help of "C++ Primer Plus" by Stephen Prata. From what I have read, the book as been amazing.Back on topic though. Like most books, at the end of each chapter there are test excercises to complete.This one was to test what you had learned about loops.The point of the program was to use nested loops to ask a user to enter a number, and then to create a box of that number, with that many asteriks in each row, preceded by periods.For example, if you entered 3, you'd get back..*.*****Would this code for doing it be considered efficient?#include <iostream>#include <string>int main(){using namespace std;cout << "Enter the number of rows: ";int num;cin >> num;string * row = new string[num];    for (int x = 0; x < num; x++){for (int y = 0; y < num; y++){row[x][y] = '*';for (int b = x+y+1; b < num; b++)row[x][y] = '.';cout << row[x][y]; }cout << endl;}delete [] row;cin.get();cin.get();return 0;}I'm not sure what kind of results that would produce, but from the looks of it'd you'd crash your program.You see, you're creating a dynamic array of num strings.  The strings in there would all have a size of 0 because of their default constructor.  However, you access characters in those strings from 0 to num-1.  Those characters don't exist yet, so if you want to access them you must first resize the string to num characters.Here is a manual on STL strings.  I believe you may use the void resize(size_type n, charT c = charT()) function.Good luck!;  Yeah, I'd say the idea of that exercise was more to print the values directly to the console from the loop rather than build the box in strings first.   ;  The program runs like a charm,The only problem that I have faced so far is that if num is greater than 20, it crashes.   ;Quote:Original post by Jrok138The program runs like a charm,The only problem that I have faced so far is that if num is greater than 20, it crashes.This is precisely because of what the other posters were saying. Writing to those positions in the string object is undefined behaviour, and depending on how std::string is implemented on any given system, and any number of other factors, a variety of things could happen.As it happens, many common std::string implementations implement a "short string optimimization", where the data structure for the object includes a small character array that is used when the string is short, as well as a char* that points to a dynamic allocation when it is needed (i.e. if you grow the string beyond the buffer size, it will set the pointer to point at some memory that it allocates itself dynamically - typically with 'new[]' - and copies the string into that memory; in the destructor of the std::string, any such dynamic allocation is freed up - typically with 'delete[]'). For short strings, the memory is available; if they grow too big, then you overwrite the data following that buffer, because the operator[] simply returns a char& relative to where the string thinks the string is located.If you resize() the string ahead of time, you can make room for the data.As the other posters suggested, the intent is probably that you just output the characters right away, rather than accumulating the text. The concept is sound, however, and it's good to get experience with the standard library containers early on. (Although, if you're smart enough to use std::string, you should also be smart enough to use std::vector.) But it makes little sense to put the b-loop inside the y-loop (you write *all* the '.'s for *every* '*' that you write), and it definitely makes no sense to output every character *as it's set*: this only serves to highlight the uselessness of the data accumulation. Think: What is the condition that determines whether to write a '*' or a '.' for a particular character? How do you express the idea "write either a '*' or a '.' depending on *this* condition"?   