My C++ Code doesn't want to run?

Reem Elhady 1 Reputation point
2022-11-10T02:09:38.117+00:00

So I have this code below when I first ran it. It worked? But then after that for some reason, every time I run this code or any other code, I get this pop-up. I don't understand it, still new on the whole programming. And, just need help to run c++ without any problems it, why this happened, and how to avoid it in the future?
Also, I get no issues, but sometimes I get errors. But when I come to fix them still nothing.

Here is the code I used to run it:

include<iostream>

using namespace std;

int main()
{
int num1;
cout << "Enter the first number.";
cin >> num1;

int num2;  
cout << "Enter the second number.";  
cin >> num2;  

int sum = num1 + num2;  
cout << "The sum is. . . " << sum;  
end1;  

}

258877-image.png

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,894 questions
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,171 Reputation points Microsoft Vendor
    2022-11-10T05:39:42.833+00:00

    Hi @Reem Elhady ,

    There are some problems with this code, and main() is an integer, so it needs a return value.

    cout << "The sum is. . . " << sum;  
    end1;  
    

    I suggest you to refer to the following code:

    #include<iostream>   
    using namespace std;  
      
    int main() {  
      int num1;  
      cout << "Enter the first number.";  
      cin >> num1;  
      int num2;  
      cout << "Enter the second number.";  
      cin >> num2;  
      int sum = num1 + num2;  
      cout << "The sum is. . . " << sum << endl;  
      return 0;  
    }  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. WayneAKing 4,266 Reputation points
    2022-11-11T03:22:54.247+00:00

    The output you show in the image file is NOT from the C++ code
    that you posted. There can be various reasons why this happens.
    Two of the most common are:

    (1) The code you are TRYING to build and run is NOT in a source
    file that is part of a Project in the Solution which is open
    in the IDE. You can't just open a C++ source file in the IDE and
    think that it will get built and executed. It won't, unless
    it is specified in the Solution/Project settings.

    (2) When you have changed the code in a .cpp file that is
    part of an open Project and there are errors in the Build
    that prevent a successful build (compile and link), the IDE
    may give you the choice to execute the last successfully
    built .exe for that Project. If you choose to run that
    then the output will reflect what was in the old version
    of the .cpp source code.

    As a general rule - especially for beginners - I recommend
    that you do NOT use Run and depend on an implicit Build
    whenever you change code. That has a tendency to "hide" the
    results of the Build process as the Output Window will
    show the result of the failed attempt to execute the
    program.

    I recommend that you always do an explicit Build or Rebuild
    first. Only after there are no errors from the Build should
    you try to Run the program.

    To avoid seeing results from an earlier version of the
    code, it's a good practice to do an explicit Clean of
    the Solution before doing a Build/Rebuild/Run. Normally
    that isn't a necessary explicit step, but when you are
    seeing "leftover" results from earlier builds it can
    help eliminate the clutter and reduce confusion.

    • Wayne
    0 comments No comments