0

I am making a program in C++, in which a web link is there so that when i type an input or simply press enter it will redirect me to that link which i have entered. For example if i have 3 options and i choose option A, the program will redirect me to that link which is in the option A.

Here's a sample:-

#include<iostream>
#include<stdlib.h>

using namespace std;

int main()
{
 system("COLOR B1");
 int choice;
 cout << "Menu" << endl;
 cout << "1. Pasta" << "\n";
 cout << "2. Cold Drink" << "\n";
 cout << "Your choice (1-2)" << "\n";
 cin >> choice;
 if(choice == 1)
 {
    cout << "Thanks" << "\n"; //Here i want a url and when i choose 1 it 
                              //will direct me to that url
 }
 else if(choice == 2)
 {
    cout << "Thanks" << "\n";  // And here also...
 }
 else
 {
    return 0;
 }
}

Please help. Thanks

  • 1
    There is no standard way to do that. How to do it will depend on your OS – litelite Aug 18 '17 at 15:04
  • I am working on windows 8 but you can also tell me how to do it on Linux also –  Aug 18 '17 at 15:06
  • "Open URL in default browser" is enough? – KonstantinL Aug 18 '17 at 15:06
  • I want to open it in any browser. –  Aug 18 '17 at 15:07
  • 1
    Take a week to read about [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol), [HTML5](https://en.wikipedia.org/wiki/HTML5), [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)), ... Consider using some HTTP server library like [libonion](https://www.coralbits.com/libonion/) – Basile Starynkevitch Aug 18 '17 at 15:23
  • Possible duplicate of [Launch web page from my application](https://stackoverflow.com/questions/153046/launch-web-page-from-my-application) – timday Aug 18 '17 at 22:25
  • No, this is not working. All people please refer this link and my sample program also. –  Aug 19 '17 at 08:49
  • https://stackoverflow.com/questions/17347950/open-url-from-c-code –  Aug 19 '17 at 08:50

1 Answers1

2

In Windows desktop programs you can use the ShellExecute API with the open operation to open a URL with the default application (generally a web browser).

ShellExecute(NULL, L"open", L"https://example.com", nullptr, nullptr, SW_SHOWNORMAL);

Store apps can not use ShellExecute at all, but you can use the UWP LaunchUriAsync.

task = Windows::System::Launcher::LaunchUriAsync(
    ref new Windows::Foundation::Uri("https://example.com"));

The various other platforms often have their own API's. In general you want to find and use them and not to assume any particular browser executable is on the path preventing users from using another or installing to a non-default location (or getting broken by updates, etc.).

Fire Lancer
  • 29,364
  • 31
  • 116
  • 182