简体   繁体   中英

Run C++ in Sublime Text 3 on Mac OSX

I know there is a built in C++ compiler and run plugin for ST3. This builds my program just fine. Running the code, it runs the first part in its console, but stops at the first cin >> . The code runs fine when I run the file compiled in ST3 straight from the terminal.

Source code:

#include <iostream>
using namespace std;

int main()
{
   int begInv;
   int sold;
   int store1, store2, store3;

   // Get the beginning inventory for all the stores.
   cout << "One week ago, 3 new widget stores opened \n";
   cout << "at the same time with the same beginning \n";
   cout << "inventory. What was the beginning inventory?";
   cin >> begInv;

   // Set each store's inventory.
   store1 = begInv;
   store2 = begInv;
   store3 = begInv;

   // Get the number of widgets sold at store 1.
   cout << "How many widgets has store 1 sold? ";
   cin >> sold;
   store1 -= sold; // Adjust store 1's inventory.

   // Get the number of widgets sold at store 2.
   cout << "How many widgets has store 2 sold? ";
   cin >> sold;
   store2 -= sold; // Adjust store 2's inventory.

   // Get the number of widgets sold at store 3.
   cout << "How many widgets has store 3 sold? ";
   cin >> sold;
   store3 -= sold; // Adjust store 3's inventory.

   // Display each store's current inventory.
   cout << "\nThe current inventory of each store:\n";
   cout << "Store 1:"  << store1 << "\n";
   cout << "Store 2:"  << store2 << "\n";
   cout << "Store 3:"  << store3 << "\n";
   return 0;
}

Screenshot of the result in ST3.

If anyone knows why it's only running halfway, and could help me figure it out, that would be amazing. Thanks!

I would guess that the issue arises not because you compile with sublime, but rather that you are trying to run the application from inside the sublime, because it cannot handle interactive programs well.

If you compile your application from the sublime and run it from the console, everything should work just fine.

You could use the tricks specified in this answer to run the application in an external cmd terminal.

Try this in Mac :

 {
    "cmd": ["bash", "-c", "/usr/bin/g++ '${file}' -std=c++11 -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM