-7

I've been working through the "Programming: Principles and Practice using C++" book, and this example was meant to illustrate how data can be lost in type conversions. But when I tried to execute it, it keeps telling me 'Main' : must return a value. I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".

I've copied the code from the book to Visual Studio and everything else I've copied has worked as expected - would anyone be able to tell me how I could fix this please? I don't know why it's happening with this specific example or how to make it stop asking for a return value.

Sorry if this is dumb, I'm very inexperienced and I've tried googling for an answer, but suggested solutions like "return 0" don't work :)!

#include "std_lib_facilities.h"

int main()
{
    double d = 0;

    while (cin >> d)
    {

        int i = d;
        char c = i;
        int i2 = c;
        cout << "d==" << d
        << " i==" << i
        << " i2==" << i2
        << " char(" << c << ")\n";
    }
}
Dean
  • 3
  • 1
  • 1
    Show the whole program. I'm going to guess that it's the `cout` and that you are neither including `iostream` nor `using namespace std;`. And you still do need to return something from main. – zero298 Jan 29 '16 at 21:13
  • 2
    What part of this simple 5-word error message don't you understand? – user207421 Jan 29 '16 at 21:13
  • 1
    Oh, sorry, I forgot to include that I was using "#include "../../std_lib_facilities.h" which is something given to us to use those! – Dean Jan 29 '16 at 21:14
  • 5
    @EJP It's easy to be snarky, but C++ doesn't require a return from `main`. – erip Jan 29 '16 at 21:16
  • 1
    Are you perhaps building a Win32 application (as opposed to a Console application), which requires `WinMain` instead of `main`? (your specific unresolved external error message would help us determine that) – Benjamin Lindley Jan 29 '16 at 21:18
  • You said the error was "Main must return a value". Are you sure it was Main with a capital M? If so, then that's a different Main from the main you've shown us here. – Klitos Kyriacou Jan 29 '16 at 21:21
  • 1
    ...Okay, I used a capital M. I am so so sorry it took me this long to figure that out. This is embarrassing. I knew that capitalisation and the way you type things had to be very precise but...I just kept missing that. Thank you Klitos (and everyone else!) *Is really embarrassed* – Dean Jan 29 '16 at 21:24
  • @Dean don't be - it's an easy mistake to make, especially for a beginner. – Klitos Kyriacou Jan 29 '16 at 21:25
  • **−1** because this is **not the real code**. – Cheers and hth. - Alf Jan 29 '16 at 22:06

2 Answers2

3

I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".

That's a separate, unrelated problem uncovered by fixing the first one. Add the return, then deal with the unresolved externals error.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 7
    Problem is, it shouldn't be an error. `main` does not require a return statement, even though it does require the return type of `int` (it's unique that way). – Benjamin Lindley Jan 29 '16 at 21:14
  • While I think this is helpful, it's not really an answer. – erip Jan 29 '16 at 21:21
  • 1
    @erip The OP thinks "return 0" doesn't work because another error pops up, and my answer is that it does work and the OP should leave it in. It doesn't answer what an unresolved externals error is, but that's a separate question. – John Kugelman Jan 29 '16 at 22:03
-1

If that's your whole program, you're unresolved external is most likely iostream. You need to include that and use the correct namespace.

Consider the following code:

#include <iostream>

using namespace std;

int main() {
   cout << "Hello World!" << endl;
   return 0;
}

Better yet, forgo the using statement and use std::cout so that you don't have to worry about namespace collision. See this question Why is “using namespace std;” considered bad practice?

#include <iostream>

int main() {
   std::cout << "Hello World!" << std::endl;
   return 0;
}
Community
  • 1
  • 1
zero298
  • 25,467
  • 10
  • 75
  • 100
  • Or `using std::cout; using std::cin;` – erip Jan 29 '16 at 21:20
  • Downvote for even mentioning `using namespace std`. The answer is also incorrect, but that comes second. – SergeyA Jan 29 '16 at 21:20
  • 1
    @SergeyA I'll be sure to downvote every JavaScript question I see that is missing a `"use strict"` statement from now on. – zero298 Jan 29 '16 at 21:25
  • @zero298, this is your life and you alone are responsible for choices you make. – SergeyA Jan 29 '16 at 21:27
  • "Unresolved external" is a linker error. A missing header would cause compilation errors and would not fix linking errors. – John Kugelman Jan 29 '16 at 22:01
  • 1
    @zero298: the header included in the OP's code is something Bjarne cooked up for the book, and as I recall it (in turn) includes ``. And yes, I think there's even a, gasp!, `using namespace std;`. Those are not bad decisions for the specific purpose, but Bjarne's use of an unholy macro for something was a disaster there. – Cheers and hth. - Alf Jan 29 '16 at 22:03
  • 1
    @SergeyA: pretty dumb reason for downvoting. absolute mechanical rules are incompatible with quality. – Cheers and hth. - Alf Jan 29 '16 at 22:05
  • @Cheersandhth.-Alf, since I believe to be `using namespace std` to be an absolute evil, I consistently downvote questions which suggest typing this incantation to fix the problem. Because OP is going to use those words. And than will develop of habit of doing so - it helped last time! And than... I think you know what's going to happen than. – SergeyA Jan 29 '16 at 22:09