0

I am learning programming and I made a code to avarage as much numbers as I want. The code runs perfectly in Visual Studio, but when I try to run it with the exe file it dies after I type in "end". I use "end" to break the loop (23rd line).

#include <iostream>
#include <string>
using namespace std;

int main()
{
string n = "";
double a = 0;
double counter = 0;
string list = "0123456789.";
int b = 0;
char betu = '\0';
int num = 0;
double value = 0;
while (true) {
    betu = '\0';
    n = "";
    a = 0;
    b = 0;
    cout << "Send in a number:" << endl;
    cin >> n;
    b = n.length();
    if (n == "end") {
        break;
    }
    for (int i = 0; i < b; i++) {
        if (list.find(n[i]) == string::npos) {
            cout << "The number is not correct.";
            exit(EXIT_FAILURE);
        }
    }
    a = stod(n);
    counter += a;
    num++;
}
if (num == 0) {
    cout << "not enough numbers to count with." << endl;
}
else {
    value = counter / num;
    cout << "The avarage of your results is: " << endl;
    cout << value << endl;
}
return 0;
}

I want "end" to quit the loop and continue the program after the loop.

  • 1
    Clearer: It _is_ running the code after the loop. – Mooing Duck Mar 15 '21 at 18:49
  • 2
    Run the application from the command line. – Quimby Mar 15 '21 at 18:55
  • @Yksisarvinen I tried the metods writen in that post, for example system("pause"); or getchar(); in the end of the main function, but neither of them solved my problem. I tried running it with ctrl + f5 (run without debugging) and it worked, but when i opened the exe it still did not work. So it seems like it only works inside Visual Studio. – Varga Ágoston Mar 15 '21 at 19:01
  • 2
    @VargaÁgoston exactly where you put the system("pause")? – robbinc91 Mar 15 '21 at 19:13

2 Answers2

0

So after I replaced the return 0; with system('pause"); it worked. I left return 0; in the code thats why it didn't worked for the first time.

  • You must have added `system("pause");` after the `return 0;` instead of before it. if so remember that once the code hits a return of a function, the function ends. No other code for that function will execute after the return. – drescherjm Mar 15 '21 at 20:06
0

Excuse me if I have misinterpreted your question but I believe you are asking how you can continue inputing numbers after you have entered "end" to produce your result.

When a break statement is found in any loop, it immediately terminates the enclosing loop, even if it is contained within a nested conditional statement. If you would like to exit your conditional statement without exiting your while loop you would want to use the "continue" statement. Below I give an example of how you might achieve a solution to your problem. I also removed "using namespace std" as it is generally not considered best practice and removed some redundant assignments, but maybe not all as I am in a bit of a rush.

It is also best to end the while loop when the defined condition is met by returning false, and I have exemplarised how this can be done within a new conditional where the input "terminate" will end the program.

#include <iostream>
#include <string>

int main()
{
    std::string n;
    double a = 0;
    double counter = 0;
    std::string list = "0123456789.";
    int b = 0;
    char betu = '\0';
    int num = 0;
    double value;

    while (true) {
        std::cout << "Send in a number:" << std::endl;
        std::cin >> n;

        b = n.length();

        if (n == "end") 
        {
            if (num == 0)
            {
                std::cout << "not enough numbers to count with." << std::endl;
            }
            else 
            {
                value = counter / num;
                std::cout << "The avarage of your results is: " << std::endl;
                std::cout << value << std::endl;
            }
            continue;
        }

        else if (n == "terminate")
        {
            return false;
        }

        for (int i = 0; i < b; i++) {
            if (list.find(n[i]) == std::string::npos) {
                std::cout << "The number is not correct.";
                exit(EXIT_FAILURE);
            }
        }

        a = std::stod(n);
        counter += a;
        num++;
    }
    return 0;
}
helpful_IO
  • 11
  • 1