Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Dyndrilliac on January 08, 2004, 02:10 PM

Title: Help Fnding Mistake
Post by: Dyndrilliac on January 08, 2004, 02:10 PM
I just recently started learning c++, and I was making a very small simple Win32 Console App to Say the following:

Quote
Hi!
Beep!

And the "Beep!" would be followed by a beeping sound, than it would wait for you to press a key. I came up with the following errors:

Quote--------------------Configuration: Practice - Win32 Debug--------------------
Compiling...
Practice.cpp
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Practice.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Practice.exe - 2 error(s), 0 warning(s)

My code is below.

//Libraries
#include <iostream.h>
#include <conio.h>

//Functions
void main()
{ // Start Code Body For Function "main"
   cout<<"Hi."<<endl;
   cout<<"Beep!\a"<<endl;
   getch();
} // End Code body For Function "main"


As far as I could tell,a ccording to the tutorials I found my code is correct. if it matters I'm using MSVC++ 6.0. Any help finding my mistakes would be appreciated.
Title: Re:Help Fnding Mistake
Post by: Siege on January 08, 2004, 02:43 PM
 int getch(void);  (http://www.delorie.com/djgpp/doc/libc/libc_358.html)
And from what I have heard using void is bad practice ;)
Title: Re:Help Fnding Mistake
Post by: Kp on January 08, 2004, 02:44 PM
Your compiler has decided that you're designing a GUI program.  GUIs have their entry point at WinMain, while text interface programs use the entry point 'main'.  So, aside from having a horribly incorrect prototype for main, your C code is fine.  Your project files will need to be modified to reflect a console application; never having tried, I'm not even sure if you can modify them.  You might have to create a new empty project (Select 'Win32 console application') and then add your existing source to that.

[Edit: Siege posted while I was typing.  I'd be curious to see what he thinks is wrong, because the supplied information seems totally irrelevant.]
Title: Re:Help Fnding Mistake
Post by: Adron on January 08, 2004, 03:02 PM
Quote from: Kp on January 08, 2004, 02:44 PM
I'm not even sure if you can modify them.

You can.
Title: Re:Help Fnding Mistake
Post by: MoNksBaNe_Agahnim on January 08, 2004, 03:28 PM
not sure what \a does but ya int main() is always better than void.

And not sure if you have practiced loops at all but i have always liked and used while/do while loops instead of getch()... such as...


#include <iostream.h>

int main()
{
   int num;

   do{
   cout << "Hi." << endl;
   cout << "Beep!" << endl << endl;

   cout << "Input 1 to quit, any other number to "
           << "re-run program";
   cin >> num;
   }while(num != 1); /* runs program until num is equal to one*/

    return 0;
}


For a program this small a do while loop is really just extra work, but when you get to bigger stuff it will be nice to already have experience with it
Title: Re:Help Fnding Mistake
Post by: Siege on January 08, 2004, 03:56 PM
Isn't how he uses getch() like that wrong?

\a makes it beep.
Title: Re:Help Fnding Mistake
Post by: MoNksBaNe_Agahnim on January 08, 2004, 04:04 PM
nah the way he used getch(); is fine, all it is doing it pausing the program until you hit any key which closes it
Title: Re:Help Fnding Mistake
Post by: iago on January 08, 2004, 05:33 PM
Yeah, there's nothing wrong with getch().  But main() shouldn't be void.
Title: Re:Help Fnding Mistake
Post by: Skywing on January 09, 2004, 12:05 AM
Change /subsystem:windows to /subsystem:console in the linker command-line.