10 Replies - 389 Views - Last Post: 09 April 2012 - 07:40 AM Rate Topic: -----

#1 Kev920  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-April 12

How would you convert a program from using int main() to void main()

Posted 09 April 2012 - 05:31 AM

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
Is This A Good Question/Topic? 0
  • +

Replies To: How would you convert a program from using int main() to void main()

#2 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: How would you convert a program from using int main() to void main()

Posted 09 April 2012 - 05:53 AM

View PostKev920, 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.
Was This Post Helpful? -1
  • +
  • -

#3 Kev920  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-April 12

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?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3043
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#5 Kev920  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-April 12

Re: How would you convert a program from using int main() to void main()

Posted 09 April 2012 - 06:31 AM

cool thanks man
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4880
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

#7 Kev920  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-April 12

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.

Was This Post Helpful? 0
  • +
  • -

#8 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

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.


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.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon

Reputation: 3043
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

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

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
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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.

This post has been edited by r.stiltskin: 09 April 2012 - 07:08 AM

Was This Post Helpful? 1
  • +
  • -

#11 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: How would you convert a program from using int main() to void main()

Posted 09 April 2012 - 07:40 AM

View Postjimblumberg, on 09 April 2012 - 06:57 AM, said:

Did you try to compile your example? I get several errors starting off with

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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1