It should print out |----|----|----|----|----|----|----|----|----|----|
But my code only prints out |----|----|----|----|----|----|----|----|----|----
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
usingnamespace std;
int main() {
string z,zz ="";
int x,y;
cin >> x >> y >> z >> zz;
for (int a = 1; a <= x; ++a) {
cout << z;
for (int b = 1; b <= y; ++b) {
cout << zz;
}
}
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string z{};
std::string zz{};
int x{};
int y{};
std::cin >> x >> y >> z >> zz;
for (int a = 0; a < x; ++a)
{
std::cout << z;
for (int b = 0; b < y; ++b)
{
std::cout << zz;
}
}
std::cout << z << '\n';
return 0;
}
You will need to find out where you can put -std=c++11 in your build settings...or just initialise the variables how you did in your code. It would probably be worth updating your compiler/IDE as you will likely get code that meets newer standards here.
seeplus, I know I don't need the {} there but is it wrong?* For me consistency is important, I'm dyslexic and find the form of code helps me understand it quicker, seeing the {} means I know its been initialised. I'm moving to code more like :
1 2
auto text = std::string {"Test"};
auto number = int {1};
search: “auto to stick”
I admit that while I'm in the process of retraining my finger, I can Frankenstein a few things.
1 as a number has a type int. (But note the meaning of this syntax has changed over versions of C++. At one point this meant an initializer_list of one entry...)
It's debatable whether any of the variables should be initialised. My compiler will tell me if I use a variable (of built-in type) without initialising it, but it won't say anything if some default value is unusable but I forgot to set it to something sensible.
I don't see the point of initialising variables to something that is unworkable in the problem (x or y being zero) just because of the oft-repeated, but seldom-justified, "Thou must initialise all variables". For large arrays that might actually have cost implications.
There is also the idea of telling one thing to beginners, and something else to those who have more experience. As in, the information one is told when learning to drive is rather different to what they say at Rally School ...........
seeplus, I do use 'auto to track' when I want the benefits of type deduction but I would still use the equal sign...mainly because so much other stuff in C++ goes along the lines of auto name = ...
1 2 3
auto text = {std::string{"Test"}};
auto text2 = "Test"s; // Should be deduced as a std::string?
auto number = {1};
So it is more to do with if I want to commit to a type or be more easily flexible with the type.
edit: I'm not sure about line 2 in the code above, I think I read it somewhere but didn't get it to work, so put it out of my mind until now. edit:probable missed usingnamespace std::string_literals; before.