1

Keep Getting This Error On Line 12

#include <iostream>

int num = 1;
int number;
int total = 0;
while (num<=5){
    cin >> number;
    total +=number;
    num++;
    cout<< total<<endl
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Elite Deity
  • 31
  • 1
  • 1
  • 4

1 Answers1

1

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;
}
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • int main(void) function does not return a value. It will work, but it's not the C++ standard. http://en.cppreference.com/w/cpp/language/main_function – Santiago Varela Mar 21 '17 at 23:44
  • 2
    @SantiagoVarela, It is standard C++ that `main` (nothing else, only `main`) returns 0 when reaching the end of the function. – chris Mar 21 '17 at 23:47
  • How does that conflict with my comment @chris? I am referring to the main function and quoting a source... – Santiago Varela Mar 21 '17 at 23:53
  • 1
    @SantiagoVarela, You said it wasn't standard C++ to fall off the end of `main`. I'm saying it is, and your reference agrees: *4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.* – chris Mar 21 '17 at 23:58
  • I realized I was missing the main comment,silly me.now I'm getting a different error when I try to run the code.it says ||=== Build: Debug in First C++ App (compiler: GNU GCC Compiler) ===| ||error: can't create obj\Debug\main.o: Permission denied| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| any help on this? – Elite Deity Mar 22 '17 at 00:07
  • @EliteDeity how are you building it? Use **sudo** before your build command. – Santiago Varela Mar 22 '17 at 00:16
  • this is my first attempt at c++ so idk all the terms.whats sudo? – Elite Deity Mar 22 '17 at 00:18
  • @chris You're right. It's not standard, but I guess it's a opinion of taste. If my main function is returning an int, I like reading it's returning a successful exit, with the code **0**. In the C Programming language, an exit status of 0 (zero) stands for successful execution, while 1 (one) (usually) or any other number stands for an error. I guess this "legacy" has been carried over to C++. – Santiago Varela Mar 22 '17 at 00:19
  • @ABusyProgrammer the feeling is mutual. I also got down voted, with no explanation. – Santiago Varela Mar 22 '17 at 00:21
  • @EliteDeity That is probably because the console application is running in the background. The result is a conflict of multiple tasks running. Try cancelling/stopping all other build/run tasks, and then try again. – BusyProgrammer Mar 22 '17 at 00:28
  • @SantiagoVarela Sorry about that, your comment coincided with the downvote, so I thought that was your downvote. – BusyProgrammer Mar 22 '17 at 00:31
  • I tried closing everything via task manager and restarting,still get error.and ok sorry.ill click it – Elite Deity Mar 22 '17 at 00:42
  • @EliteDeity Which IDE are you using? – BusyProgrammer Mar 22 '17 at 00:43
  • @ABusyProgrammerIm Using CodeBlocks – Elite Deity Mar 22 '17 at 01:00
  • @EliteDeity Try the following links: http://stackoverflow.com/questions/22921297/codeblocks-c-build-permission-denied-collect2-exe ; http://www.metalshaperman.com/?p=180 ; The first one is on StackOverflow, and the second one is a separate blog. They both address the error you got in your program – BusyProgrammer Mar 22 '17 at 01:19