Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with C++ program

  • 08-05-2009 9:34am
    #1
    Registered Users, Registered Users 2 Posts: 2,318 ✭✭✭


    Can anyone help me and point out where i'm going wrong with the below code?
    The error i get is:
    1>c:\users\mark\documents\visual studio 2008\projects\c++ projects\random\user defined functions with cin\user defined functions with cin\user defined functions with cin.cpp(26) : error C2562: 'myfun' : 'void' function returning a value
    1> c:\users\mark\documents\visual studio 2008\projects\c++ projects\random\user defined functions with cin\user defined functions with cin\user defined functions with cin.cpp(3) : see declaration of 'myfun'
    #include <iostream>
    #include <cmath>
    void myfun(int);				// Creates a function prototype. Just to say will make a function.
    
    
    //Below would be a function
    int main()
    {
    	using namespace std;		// Can be put inside a function as its allows other functions to be different.
    	int yournum;
    	cin >> yournum;
    	myfun(yournum);				// This is whats called calling a function. 
    								// This will give x in the function below its value of 45.
    
    	system("pause");
    	return 0;
    }
    
    void myfun(int x)				// (int x) says that there will be one integer variable named x.
    {
    	using namespace std;
    	
    	cout << "My favourite number is: " << x << endl;
    
    	system("pause");
    	return 0;
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 368 ✭✭backboiler


    The first error says it all. You can't return a value from a function declared void. Take out the "return 0" at the end of myfun().


  • Closed Accounts Posts: 2,219 ✭✭✭Lab_Mouse


    You should also declare
    using namespace std;
    
    once,just after your include's,that way the namespace std is visible to all parts of your programme


  • Registered Users, Registered Users 2 Posts: 2,318 ✭✭✭deceit


    Thanks backboiler
    Lab_Mouse wrote: »
    You should also declare
    using namespace std;
    
    once,just after your include's,that way the namespace std is visible to all parts of your programme
    I have been declaring using namespace std; at the start for all programs but in this one I was putting it like that just so I would remember when I looked over my work that it can be done indiviually also. But thanks for pointing it out.


  • Closed Accounts Posts: 2,219 ✭✭✭Lab_Mouse


    No worries


  • Registered Users, Registered Users 2 Posts: 368 ✭✭backboiler


    By the way, when you get into more complicated programs you may have dozens of compiler error messages. Always start with the first error reported. It's the important one.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,318 ✭✭✭deceit


    backboiler wrote: »
    By the way, when you get into more complicated programs you may have dozens of compiler error messages. Always start with the first error reported. It's the important one.
    Thats really good to know, think I would always just go bottom up as was first would see. Will start with the top one from now on. Seems simple after it being pointed out what I missed and how it states it in the error message.


  • Closed Accounts Posts: 2,219 ✭✭✭Lab_Mouse


    Like backboiler said above,if the error is say a missing '}' it could throw up 20 errors but fixing it will get rid of them all.Also check the line above where the error is aswell

    sometimes the error message can be a bit strange,you might be only be missing a semi colon.

    Just copy and paste the error number into google and you will get a detailed explanation of the fault(presuming your using microsoft visual)


  • Registered Users, Registered Users 2 Posts: 2,318 ✭✭✭deceit


    Lab_Mouse wrote: »
    Like backboiler said above,if the error is say a missing '}' it could throw up 20 errors but fixing it will get rid of them all.Also check the line above where the error is aswell

    sometimes the error message can be a bit strange,you might be only be missing a semi colon.

    Just copy and paste the error number into google and you will get a detailed explanation of the fault(presuming your using microsoft visual)

    Yea i'm on visual studio 2008. Never taught to try google the fault. I'm sure 99% of the time will bring up the right answer :). Thanks


Advertisement