Sign in to follow this  
Shewolf

What am i doing wrong?

Recommended Posts

Trying to make a simple program lol. The main function wont call the PName() It just displays the welcome to thens ays press any key to enter.

// MAIN

#include <iostream>
using namespace std;

int main()
{
    cout << " Welcome to Kesmere\n\n\n ";
    int PName();    
    
    system("pause");
    return 0;
}

int PName()
{
    char name[50];
    cout << " Enter your name\n\n";
}  

Share this post


Link to post
Share on other sites

// MAIN

#include <iostream>
using namespace std;

int main()
{
cout << " Welcome to Kesmere\n\n\n ";
int PName(); // this is a forward declaration of int PName() - it does nothing
PName(); // this is a function call
int x = PName(); // this is function call, where the return value is assigned to x
system("pause");
return 0;
}

int PName()
{
char name[50];
cout << " Enter your name\n\n";
return 0; // if return type is not void - you must return something
}


Share this post


Link to post
Share on other sites
You're declaring the function in the main loop. That's why it will compile, but it won't actually call it.

What you need to do is get rid of the int in int PName(), and also declare the function above, so you can actually call it.


// MAIN

#include <iostream>

using namespace std;

int PName();

int main()
{
cout << " Welcome to Kesmere\n\n\n ";
PName();

system("pause");

return 0;
}

int PName()
{
char name[50];

cout << " Enter your name\n\n";
//Edit And as Paulius Maruska noted, you should put a return here.
//It's not absolutely necessary, as some compilers will let this happen
//(they'll return a value by default), but bad style.
return 0;
}




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this