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 7598571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:26:19+00:00 2026-05-30T22:26:19+00:00

How do you stop the code from running in C++? I have the code

  • 0

How do you stop the code from running in C++? I have the code

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int total, sub, subc;
    cout << "What number would you like to start with? ";
    cin >> total;
        cout << "Enter in a number, 1 or 2, to subtract";
    cin >> sub;
    if (sub == 1) {
        total--;
        subc++;
        cout << "You subtracted one";
    }
    else {
        total = total - 2;
        subc++;
    }
    if (sub <= 0)
        cout << "YAY!";
}

and i want to insert a thing that just stops the code and exits right after cout << "YAY!"
how do i do that???

  • 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-05-30T22:26:20+00:00Added an answer on May 30, 2026 at 10:26 pm

    A return statement will end the main function and therefore the program:

    return 0;
    

    ETA: Though as @Mysticial notes, this program will indeed end right after the cout << "YAY!" line.

    ETA: If you are in fact working within a while loop, the best way to leave the loop would be to use a break statement:

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main() {
        int total, sub, subc;
        cout << "What number would you like to start with? ";
        cin >> total;
        while (1) {
                cout << "Enter in a number, 1 or 2, to subtract";
            cin >> sub;
            if (sub == 1) {
                total--;
                subc++;
                cout << "You subtracted one";
            }
            else {
                total = total - 2;
                subc++;
            }
            if (sub <= 0) {
                cout << "YAY!";
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running emacs 23.4 on OSX, obtained from emacsformacosx.com. I'm writing matlab code, using
I have extracted the following code from selenium IDE.(c# remote control) using System; using
How do you stop the designer from auto generating code that sets the value
I have code to retrieve data from a database into a form but it
I have some code that can spawn background worker threads from anywhere within it.
It is a widely known fact that one shall not stop running processes using
I have a simple pub-sub setup on a mid-sized network, using ZMQ 2.1. Although
I have a problem with running a Java app (B) from my app (A).
i am running a command line program from inside my c# code behind page.
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):

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.