Jump to content
  • Advertisement
Sign in to follow this  
Shewolf

What am i doing wrong?

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

// 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!