C++ functions with loops and switches

Hi dear C++ learners if you are wondering to find some helpful problems and their solution for your practice so you are reading the right post. In this post I have some problems which will enable you to implement the concepts of loops, switches, functions in C++.

Problem no.1:

Write a C++ program that allows the user to draw geometric shapes. The program should display a menu with the following options:

  1. Draw a right-angled triangle.
  2. Draw a rectangle.
  3. Draw a shallow right-angled triangle.
  4. Draw a shallow rectangle.

The program should prompt the user to enter their choice (1, 2, 3, or 4) and then follow these instructions based on their choice:

Option 1: If the user selects “1,” prompt them to enter the number of rows (a positive integer). Then, draw a right-angled triangle with the specified number of rows using asterisks (*) as shown below:

Option 2: If the user selects “2,” prompt them to enter the number of rows (a positive integer) and the number of columns (a positive integer). Then, draw a rectangle with the specified number of rows and columns using asterisks as shown below:

Option 3: If the user selects “3,” prompt them to enter the number of rows (a positive integer). Then, draw a shallow right-angled triangle with the specified number of rows using asterisks as shown below:

Option 4: If the user selects “4,” prompt them to enter the number of rows (a positive integer) and the number of columns (a positive integer). Then, draw a shallow rectangle with the specified number of rows and columns using asterisks as shown below:

  • If the user enters an invalid choice or invalid input (non-positive integers), display an error message and allow them to try again.

Your program should continue to display the menu and execute the chosen option until the user decides to exit.

Solution:

#include<iostream>

using namespace std;

void menu() {
cout << “Enter 1 to draw a right-angled triangle” << endl;
cout << “Enter 2 to draw a rectangle” << endl;
cout << “Enter 3 to draw a shallow right-angled triangle” << endl;
cout << “Enter 4 to draw a shallow rectangle” << endl;
cout << “Enter 0 to exit” << endl;
cout << “Enter your choice: “;
}

void drawRightAngledTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << “* “;
}
cout << endl;
}
}

void drawRectangle(int rows, int cols) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
cout << “* “;
}
cout << endl;
}
}

void drawShallowRightAngledTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || i == rows || i == j) {
cout << “* “;
} else {
cout << ” “;
}
}
cout << endl;
}
}

void drawShallowRectangle(int rows, int cols) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == 1 || i == rows || j == 1 || j == cols) {
cout << “* “;
} else {
cout << ” “;
}
}
cout << endl;
}
}

int main() {
int choice;
while (true) {
menu();
cin >> choice;

    if (choice == 0) {
        break; // Exit the program
    }

    int rows, cols;
    switch (choice) {
        case 1:
            cout << "Enter the number of rows: ";
            cin >> rows;
            drawRightAngledTriangle(rows);
            break;
        case 2:
            cout << "Enter the number of rows: ";
            cin >> rows;
            cout << "Enter the number of columns: ";
            cin >> cols;
            drawRectangle(rows, cols);
            break;
        case 3:
            cout << "Enter the number of rows: ";
            cin >> rows;
            drawShallowRightAngledTriangle(rows);
            break;
        case 4:
            cout << "Enter the number of rows: ";
            cin >> rows;
            cout << "Enter the number of columns: ";
            cin >> cols;
            drawShallowRectangle(rows, cols);
            break;
        default:
            cout << "Invalid Option. Please enter 1, 2, 3, 4, or 0 to exit." << endl;
            break;
    }
}

return 0;

}

67 thoughts on “C++ functions with loops and switches”

  1. Certainly, a great deal remains unknown regarding this topic. Your arguments are well-reasoned and persuasive. Your sharing of the content was really appreciated; each quote was fantastic. Persist in sharing and never cease inspiring others.

    Reply
  2. Your article writing style is really impressive. Great stuff! I bookmarked it and will be sure to check it out later. Nowadays, I find that reading extremely interesting articles or reviews on the internet teaches me something new and difficult.

    Reply
  3. You are a really accomplished blogger; this is fascinating. I have subscribed to your feed and can’t wait to read more of your outstanding content. Furthermore, I have shared your website on my social media accounts!

    Reply
  4. I have been browsing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion, if all website owners and
    bloggers made good content as you did, the web will be much more useful than ever before.

    Reply
  5. You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

    Reply
  6. Your article is something that I truly like, and I blog very frequently. In point of fact, the piece has already piqued my attention. I am going to save your website to my bookmarks and continue to check it for brand spanking new information.

    Reply
  7. Accept to our blog, where we delve into the fascinating territory of software development and technology. Here, we inspect the latest trends, breakthroughs, and innovations that are shaping the future of digital landscapes. From cutting-edge programming languages to transformative software applications, we blind a blue spectrum of topics designed to educate both tech enthusiasts and dynamism professionals. Join us as we sail with the aid the complexities of software engineering, unravel the secrets of loaded software deployment, and discuss the impact of technology on our every day lives. Whether you’re a acclimatized developer, a tech-savvy individual, or simply kinky about the ever-evolving cosmos of software, our blog is your go-to source fit insightful, appealing, and revealing content. Interrupt tuned for regular updates and sound deep into the planet of software with us!

    https://paymarket.site/
    https://metka-cc.site/
    https://nordchange.site/
    https://bitbits.site/
    https://fastchange.site/
    https://tiptopobmen.site/
    https://el-change.site/
    https://netex24.site/
    https://btchange.site/
    https://vvobmen.site/

    Reply
  8. Welcome to our blog, where we delve into the fascinating area of software phenomenon and technology. Here, we enquire into the latest trends, breakthroughs, and innovations that are shaping the tomorrow’s of digital landscapes. From cutting-edge programming languages to transformative software applications, we account for a generalized spectrum of topics designed to educate both tech enthusiasts and energy professionals. Tie us as we navigate with the aid the complexities of software engineering, unravel the secrets of successful software deployment, and talk over the effect of technology on our daily lives. Whether you’re a acclimatized developer, a tech-savvy living soul, or entirely odd about the ever-evolving time of software, our blog is your go-to commencement quest of insightful, appealing, and informative content. Interrupt tuned in the interest regular updates and sound extensive into the planet of software with us!

    https://envelopez.ru/
    https://altinbit.site/
    https://blablamoney.site/
    https://coinblinker.site/
    https://e-change.site/
    https://n1ex.store/
    https://global-ex.site/
    https://bitcashcc.shop/
    https://aple-system.ru/
    https://myxa-cc.site/

    Reply
  9. Simply wish to say your article is as amazing The clearness in your post is just nice and i could assume youre an expert on this subject Well with your permission let me to grab your feed to keep updated with forthcoming post Thanks a million and please carry on the gratifying work

    Reply
  10. Accept to our blog, where we delve into the fascinating territory of software development and technology. Here, we traverse the latest trends, breakthroughs, and innovations that are shaping the prospective of digital landscapes. From cutting-edge programming languages to transformative software applications, we account for a unsubtle spectrum of topics designed to enlighten both tech enthusiasts and energy professionals. Meet us as we captain during the complexities of software engineering, unravel the secrets of in the money software deployment, and review the effect of technology on our circadian lives. Whether you’re a inured developer, a tech-savvy individual, or simply extraordinary about the ever-evolving time of software, our blog is your go-to commencement for insightful, friendly, and revealing content. Stay tuned for semi-monthly updates and dip unfathomable into the planet of software with us!

    https://nicechange.shop/
    https://eobmen.site/
    https://itez.store/
    https://altinbit.site/
    https://excoin.site/
    https://altcoincc.site/
    https://prostocash.store/
    https://coin-bank.site/
    https://galaxyflowers.ru/
    https://darken-biz.site/

    Reply
  11. Accept to our blog, where we delve into the fascinating kingdom of software situation and technology. Here, we traverse the latest trends, breakthroughs, and innovations that are shaping the future of digital landscapes. From cutting-edge programming languages to transformative software applications, we be enough a blue spectrum of topics designed to educate both tech enthusiasts and bustle professionals. Meet us as we navigate during the complexities of software engineering, unravel the secrets of in the money software deployment, and examine the repercussions of technology on our everyday lives. Whether you’re a acclimatized developer, a tech-savvy living soul, or entirely curious about the ever-evolving cosmos of software, our blog is your go-to commencement fit insightful, delightful, and edifying content. Stay tuned in the interest uniform updates and dip deep into the planet of software with us!

    https://altcoincc.site/
    https://grandchange.store/
    https://paaywallet.store/
    https://btcboss.site/
    https://fehupay.site/
    https://envelopez.ru/
    https://rocketchange.site/
    https://topcash.site/
    https://ychanger.site/
    https://crybex.site/

    Reply
  12. Its like you read my mind You appear to know so much about this like you wrote the book in it or something I think that you can do with a few pics to drive the message home a little bit but instead of that this is excellent blog A fantastic read Ill certainly be back

    Reply
  13. консультации юриста бесплатно для всех вопросов о юридических вопросах|юридическое обслуживание бесплатно на любые темы
    бесплатная юридическая поддержка для физических лиц и компаний по разнообразным вопросам Получи бесплатную юридическую консультацию от знающих специалистов|Получи бесплатную консультирование от лучших юристов по любым проблемам
    Бесплатная юридическая помощь: консультации специалистов и решение вопросов
    бесплатные юридические консультации по телефону konsultaciya-yurista-499.ru.

    Reply
  14. I just could not leave your web site before suggesting that I really enjoyed the standard information a person supply to your visitors Is gonna be again steadily in order to check up on new posts

    Reply
  15. Accept to our blog, where we delve into the fascinating kingdom of software phenomenon and technology. Here, we explore the latest trends, breakthroughs, and innovations that are shaping the tomorrow’s of digital landscapes. From cutting-edge programming languages to transformative software applications, we be enough a broad spectrum of topics designed to teach both tech enthusiasts and bustle professionals. Tie us as we journey through the complexities of software engineering, unravel the secrets of in the money software deployment, and talk over the impact of technology on our every day lives. Whether you’re a seasoned developer, a tech-savvy solitary, or simply odd roughly the ever-evolving world of software, our blog is your go-to commencement in compensation insightful, engaging, and illuminating content. Stop tuned for uniform updates and jump extensive into the world of software with us!

    https://kassacc.site/
    https://ria888.ru/
    https://abcobmen.site/
    https://korablik-zabeg.ru/
    https://fastexchange.site/
    https://coinstartcc.site/
    https://z-exchange.site/
    https://sovagg.site/
    https://exline-pro.site/
    https://transfer24.store/

    Reply
  16. I just could not leave your web site before suggesting that I really enjoyed the standard information a person supply to your visitors Is gonna be again steadily in order to check up on new posts

    Reply
  17. obviously like your website but you need to test the spelling on quite a few of your posts Several of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand Ill certainly come back again

    Reply
  18. I liked it as much as you did. Even though the picture and writing are good, you’re looking forward to what comes next. If you defend this walk, it will be pretty much the same every time.

    Reply
  19. In addition, I had a wonderful time with that. In spite of the fact that both the narration and the images are of a very high level, you realise that you are anxiously expecting what will happen next. Regardless of whether you choose to defend this stroll or not, it will be essentially the same every time.

    Reply
  20. Undoubtedly, there is a great deal to learn on this topic. I agree with everything you said. It was all very nicely said. I loved every quote, and I appreciate you sharing the information. Continue inspiring and sharing with others.

    Reply
  21. You’re a highly talented blogger, and this is incredibly interesting. I’ve subscribed to your feed and am eager to read more of your amazing posts. I’ve also shared your website on my social media accounts!

    Reply
  22. I loved you better than you would ever be able to express here. The picture is beautiful, and your wording is elegant; nonetheless, you read it in a short amount of time. I believe that you ought to give it another shot in the near future. If you make sure that this trek is safe, I will most likely try to do that again and again.

    Reply
  23. I do agree with all the ideas you have introduced on your post They are very convincing and will definitely work Still the posts are very short for newbies May just you please prolong them a little from subsequent time Thank you for the post

    Reply
  24. What i do not understood is in truth how you are not actually a lot more smartlyliked than you may be now You are very intelligent You realize therefore significantly in the case of this topic produced me individually imagine it from numerous numerous angles Its like men and women dont seem to be fascinated until it is one thing to do with Woman gaga Your own stuffs nice All the time care for it up

    Reply
  25. Its like you read my mind You appear to know a lot about this like you wrote the book in it or something I think that you could do with some pics to drive the message home a little bit but instead of that this is fantastic blog An excellent read I will certainly be back

    Reply
  26. I have been surfing online more than 3 hours today yet I never found any interesting article like yours It is pretty worth enough for me In my opinion if all web owners and bloggers made good content as you did the web will be much more useful than ever before

    Reply
  27. What i do not realize is in fact how you are no longer actually much more wellfavored than you might be right now Youre very intelligent You recognize thus considerably in relation to this topic made me in my view believe it from numerous numerous angles Its like men and women are not fascinated until it is one thing to do with Lady gaga Your own stuffs excellent All the time handle it up

    Reply
  28. Fantastic beat I would like to apprentice while you amend your web site how could i subscribe for a blog site The account helped me a acceptable deal I had been a little bit acquainted of this your broadcast offered bright clear concept

    Reply
  29. Ive read several just right stuff here Certainly price bookmarking for revisiting I wonder how a lot effort you place to create this kind of great informative website

    Reply

Leave a Comment