IM programming a random number guessing game and I have to input allegro into my program but you need to use void main for it and i have been using int main. Is there any way of been able to convert int main to void main and be still return answers. I know this probably is an easy question but i have only been programming a couple of weeks now. thanks
10 Replies - 389 Views - Last Post: 09 April 2012 - 07:40 AM
#1
How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 05:31 AM
Replies To: How would you convert a program from using int main() to void main()
#2
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 05:53 AM
Kev920, on 09 April 2012 - 05:31 AM, said:
IM programming a random number guessing game and I have to input allegro into my program but you need to use void main for it and i have been using int main. Is there any way of been able to convert int main to void main and be still return answers. I know this probably is an easy question but i have only been programming a couple of weeks now. thanks
Im not sure exactly what your doing in your program but you should be able to change the int in int main to void as in:
void main()
{
// do stuff
}
and then no more return 0; at the end of main or anywhere in it. I wouldn't recommend using void main. In a simple program it shouldn't matter but in more advanced ones the operating system might need that value main returns or a process that called your program might need it to know the state of the program when it terminated. Hope that helps.
#3
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:03 AM
I know that but in allegro which is in Object oriented programming you have to use void man, but i want to use allegro and also return values but void main doesnt let you do it so im kind of looking to see if anyone knows how to use void main and return values or will i have to use classes for it?
#4
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:20 AM
You don't. The function main() should (must in C++) be defined as returning an int and you should return an int from this function. Since the Allegro libraries are C/C++ libraries they do use the standard conforming int main(). See this page for an example.
Jim
Jim
#5
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:31 AM
cool thanks man
#6
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:35 AM
Also, main is your entry point. You return a value for the OS executing your program. Your program itself should NEVER call main.
#7
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:42 AM
Im writing a program and im using allegro but when i try to compile it a few errors come up mostly about WinMain is overloaded but when i take out allegro it works fine..... is there anyway of been able to get past it?
This post has been edited by r.stiltskin: 09 April 2012 - 07:06 AM
Reason for edit:: Merged duplicate topics: please don't create multiple topics for the same program.
#8
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:48 AM
Im not familiar with allegro but i think i have an idea for you.
pass a variable by refrence to the function change it in the function exit the function and your variable will be changed too.
When you pass the variable by refrence your passing the address of the variable not the value of the variable. That way when your changing the value of n in plus1 its changing the value of the variable whos address n is holding which is i.
pass a variable by refrence to the function change it in the function exit the function and your variable will be changed too.
void main()
{
int i = 1;
plus1(&i);
}
void plus1(int &n)
{
n++
}
When you pass the variable by refrence your passing the address of the variable not the value of the variable. That way when your changing the value of n in plus1 its changing the value of the variable whos address n is holding which is i.
#9
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:57 AM
Did you try to compile your example? I get several errors starting off with
There are numerous other errors as well. For one more your function call is incorrect, I'll let you figure out why.
Jim
Quote
main.cpp|1|error: ‘::main’ must return ‘int’|
There are numerous other errors as well. For one more your function call is incorrect, I'll let you figure out why.
Jim
#10
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 06:57 AM
(Answering question in Post #7):
If you have set your project up as a Windows application, that is probably the problem. When you use Allegro, Allegro takes care of interfacing with Windows so you should set up the program as a console application with a main() function, you shouldn't have a WinMain and you shouldn't #include Windows.h.
Please keep all questions related to this program under one topic.
If you have set your project up as a Windows application, that is probably the problem. When you use Allegro, Allegro takes care of interfacing with Windows so you should set up the program as a console application with a main() function, you shouldn't have a WinMain and you shouldn't #include Windows.h.
Please keep all questions related to this program under one topic.
This post has been edited by r.stiltskin: 09 April 2012 - 07:08 AM
#11
Re: How would you convert a program from using int main() to void main()
Posted 09 April 2012 - 07:40 AM
jimblumberg, on 09 April 2012 - 06:57 AM, said:
Did you try to compile your example? I get several errors starting off with
There are numerous other errors as well. For one more your function call is incorrect, I'll let you figure out why.
Jim
Quote
main.cpp|1|error: ‘::main’ must return ‘int’|
There are numerous other errors as well. For one more your function call is incorrect, I'll let you figure out why.
Jim
Yep there were some errors and no i didn't compile it it wasn't meant to be complete. Also i went and checked and with all the compiler extra stuff off it still wouldnt do void main(). I already mentioned that i dont suggest using it and i dont use it. It was only there because the the topic starter mentioned he was trying to turn int main() into void main(). Its been awhile since i used it and ive used it before but that was years ago so i thought it was still possible. And heres the re-written complete code for your veiwing enjoyment. Note the int main() weve all learned something about that today.
#include<iostream>
using namespace std;
void plus1(int & n);
int main()
{
int i = 1;
cout<<i<<endl<<endl;
plus1(i);
cout<<i<<endl<<endl;
return 0;
}
void plus1(int & n)
{
n+=1;
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|