error: expected unqualified-id before ‘while’|

Your code is missing a int main(void) function in which all of your code should be inside. By definition, a C++ program is required to have a int main(void) function. You need to put all your code inside the int main(void) function.

Also, your cin << and cout << statements are missing the namespace tag std::. Therefore, you need to add the std:: at every instance you use cin or cout See the code below:

#include <iostream>

int main(void) // The main() function is required in a C++ program
{
    int num = 1;
    int number;
    int total = 0;
    while (num <= 5)
    {
        std::cin >> number; // You need to add the reference to std:: as "cin <<" is a part of the std namespace
        total += number;
        num++;
        std::cout << total << std::endl // You need to add the reference to std:: as "cout <<" and "endl <<" is a part of the std namespace
    }

    return 0;
}

Leave a Comment