Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:07:29+00:00 2026-06-12T11:07:29+00:00

I have an assignment to learn how to use boost::variant. I’m trying to create

  • 0

I have an assignment to learn how to use boost::variant. I’m trying to create a function that asks the user for a shape type to create. Then create the requested shape and assign it to the variant and return it. I’m using a switch to accomplish this, but I’m getting a runtime error with the default statement.

I also get a warning from the compiler: “warning C4715: ‘ShapeVariant’ : not all control paths return a value”

How do I just print a string if the user enters an invalid selection?

Thanks!

#include "boost/variant.hpp"

typedef boost::variant<Point,Line,Circle> ShapeType;

ShapeType ShapeVariant()
{
    cout << "Please select a Shape Type\n1: Point\n2: Line\n3: Circle\n\nSelection: ";

    int choice;

    cin >> choice;

    switch(choice)
    {
    case 1: return Point(); break;
    case 2: return Line(); break;
    case 3: return Circle(); break;
    default: cout << "Invalid selection." << endl; break;
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:07:31+00:00Added an answer on June 12, 2026 at 11:07 am

    Instead of printing a string from the function, you could instead throw an exception that the caller catches and prints the reason why the exception was thrown.

    switch(choice)
    {
      case 1: return Point(); break;
      case 2: return Line(); break;
      case 3: return Circle(); break;
      default: throw std::runtime_error( "Invalid selection." );
    } 
    

    In the caller:

    try {
      auto result = ShapeVariant();
    } catch( std::exception const& e ) {
      std::cout << e.what() << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have assignment to work on producer and consumer problem by use thread and
I have an assignment in which I have to allow a user to plot
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have an assignment about fftw and I was trying to write a small
I have an assignment to create a secure communication between 2 people with a
I have an assignment that wants me to write an ternary search algorithm and
I have an assignment that simulates a dice game. As part of the program,
I'm trying to learn about new feature of C++ namely move constructor and assignment
I am new to programming and I have been trying to create a program
So, given that I have some type of data structure like this within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.