Jump to content

simple c++ loop question

MisterWhite
Go to solution Solved by Sauron,

in pseudocode (won't write it for you ;))

print "enter your name"input -> nameprint "enter height"input -> heightprint "enter width"input -> widthfor(i from 0 to (width-1))   print name;print "\n"for(i from 0 to (height-3))   print name + (width-2)*" " + name + "\n"for(i from 0 to (width-1))   print name;print "\n"

So i have this exercise:

Write a C++ program to ask the user for his/her name and a size, and display a hollow rectangle with it:

Enter your name: Yo
Enter height: 4

Enter width:4
YoYoYoYo
Yo____Yo
Yo____Yo
YoYoYoYo
(note: the underscores _ should not be displayed on screen; you program should display blank spaces inside the rectangle)

 

And i can't come up with the solution for this, any help?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is your question about the underscores being changed to blanks or the programm as a whole?

Oh, and don't forget to follow your own topics. ;)

I once had one of these, now I've got this.

Link to comment
Share on other sites

Link to post
Share on other sites

Is your question about the underscores being changed to blanks or the programm as a whole?

Oh, and don't forget to follow your own topics. ;)

Just how to make those 2 loops (i think there should be 2), so that there would be "spaces" in the middle

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

in pseudocode (won't write it for you ;))

print "enter your name"input -> nameprint "enter height"input -> heightprint "enter width"input -> widthfor(i from 0 to (width-1))   print name;print "\n"for(i from 0 to (height-3))   print name + (width-2)*" " + name + "\n"for(i from 0 to (width-1))   print name;print "\n"

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

 

in pseudocode (won't write it for you ;))

print "enter your name"input -> nameprint "enter height"input -> heightprint "enter width"input -> widthfor(i from 0 to (width-1))   print name;print "\n"for(i from 0 to (height-3))   print name + (width-2)*" " + name + "\n"for(i from 0 to (width-1))   print name;print "\n"

What will happen when you do this  ' (width-2)*" "  '  i mean multiply a number by space?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

What will happen when you do this  ' (width-2)*" "  '  i mean multiply a number by space?

 

it means, print (width-2) spaces

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

it means, print (width-2) spaces

It doesn't work that way :D. error

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

It doesn't work that way :D. error

 

because it's pseudocode, you need to figure out how to express it in c++

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

because it's pseudocode, you need to figure out how to express it in c++

Just out of curiosity, you know how to do it?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just out of curiosity, you know how to do it?

 

Of course

 

-edit-

 

by the way if you want you can nest another for loop to print the spaces

for(i from 0 to (height-3))   print name   for(a from 0 to (width-3))      print " "   print name   print "\n"

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Shall I just give you the whole code? But that way I'm not really doing you a favor.

From salty to bath salty in 2.9 seconds

 

Link to comment
Share on other sites

Link to post
Share on other sites

Shall I just give you the whole code? But that way I'm not really doing you a favor.

If i will fail, i will "summon" you :D

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites


#include <iostream>

#include <string>

int main(int argc, const char * argv[]) {

std::string name;

int height, width;

std::cout << "Name: ";

std::cin >> name;

std::cout << "Height: ";

std::cin >> height;

std::cout << "widht: ";

std::cin >> width;

std::string space((name.length()*width) - (name.length()*2), ' ');

std::string midRows = name + space + name;

for(int w = 0; w < width; w++)

std::cout << name;

std::cout << std::endl;

for(int h = 0; h < (height - 2); h++)

std::cout << midRows << std::endl;

for(int w = 0; w < width; w++)

std::cout << name;

return 0;

}

Link to comment
Share on other sites

Link to post
Share on other sites

 

Of course

 

-edit-

 

by the way if you want you can nest another for loop to print the spaces

for(i from 0 to (height-3))   print name   for(a from 0 to (width-3))      print " "   print name   print "\n"

HEYYYY i did it :P, wanna see the code?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

HEYYYY i did it :P, wanna see the code?

 

sure - did I help?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

sure - did I help?

Yes. i just googled how to print a specific amount of spaces and it was just a simple loop of "couts"

Well this is in c# , not c++; but i think you should get the idea:

 

using System;

public class Rectangle

{

    public static void Main()

    {

        string n;

        int width, height;  

        Console.Write ("Enter a word: ");

        n =  (Console.ReadLine());

        Console.Write ("Enter width: ");

        width = Convert.ToInt32 (Console.ReadLine());

        Console.Write ("Enter heigth: ");

        height = Convert.ToInt32 (Console.ReadLine());

        int many = n.Length * (width-2);

        for (int i = 0; i < width; i++) 

        {

            Console.Write (n);

        }

        Console.WriteLine ();

        for (int i = 0; i < height-2 ; i++)

        {

            Console.Write (n);

            for(int j=0; j< many; j++)

            {

                Console.Write (" ");

            }

            Console.Write (n);

            Console.WriteLine ();

        }

        for (int i = 0; i < width; i++) 

        {

            Console.Write (n);

        }

        Console.ReadKey ();

    }

}

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yes. i just googled how to print a specific amount of spaces and it was just a simple loop of "couts"

Well this is in c# , not c++; but i think you should get the idea:

 

yeah, that's what this was

 

for(i from 0 to (height-3))   print name   for(a from 0 to (width-3))      print " "   print name   print "\n"

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×