How to add input string at the final output

If my input is 10 4 | -

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>
using namespace 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}

10 4 | -
|----|----|----|----|----|----|----|----|----|----|
Last edited on
My dev C++ can't run your code because of line 6 saying
"extended initializer lists only available with -std=c++11 or -std=gnu++11"
any idea why?
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.
Thanks, I just copied the one line you did with my own code instead and it worked! ty for the answer.
Last edited on
@kazeroth900, use updated dev c++
https://www.embarcadero.com/free-tools/dev-cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

string operator * ( unsigned n, const string &s ){ return n ? ( n - 1 ) * s + s : "" ; }

int main()
{
   string delim, space;
   int n, width;
   cin >> n >> width >> delim >> space;
   cout << delim + n * ( width * space + delim );
}


10 4 | -
|----|----|----|----|----|----|----|----|----|----| 
@The Grey Wolf L6, 7 don't need the {} for initialisation as std::string has a default constructor which is called when z,zz are defined.
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.

_________________________________
*HonestQuestion®
No, it's not 'wrong' - it's just not needed. :)

For your way of initialising, I'd code as:

1
2
auto text {std::string{"Test"}};
auto number {1};


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...)

But again, there's nothing 'wrong' :)
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.
Last edited on
There's always exception(s) to prove the rule. exceptio probat regulam

If you know you don't need an initial value set at definition then you have sufficient understanding to know what you don't need.

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 using namespace std::string_literals; before.
Last edited on
Yeah - using s for string requires using namespace std::string_literals;

Same as sv for string_view requires using namespace std::string_view_literals;
Topic archived. No new replies allowed.