What am i doing wrong?

Started by
2 comments, last by Shewolf 17 years, 8 months ago
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";
}  
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}  
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;}  
Thank you! I should have known that LOL!

This topic is closed to new replies.

Advertisement