iiZ
iiZ

Reputation: 59

C++ function errors

I'm fairly new to C++ and I've been trying to use functions and for a while. I was using them just fine but when I wrote this recent code my compiler (Visual Studio) keeps giving me errors, I can't tell if it is just something small I did that I'm not noticing or how I'm using the function.

I've been looking at it for a while and I'm just getting frustrated so I went and made a simpler code and the same thing happened.

#include <iostream>

using namespace std;

int swordString();

int main()
{
    int age;
    int yearsleft;

    cout << "How old are you?" << endl;
    cin >> age;

    yearsleft = swordString(age);

    cout << "You have " << yearsleft << " years left to live!" << endl;

    return 0;
}

int swordString(int a)
{
    int r;
    r = 100 - a;
    return r;
}

Upvotes: 0

Views: 88

Answers (2)

Prototyping is the same as declaring a function before it is used and this enables the complier to provide stronger type checking.

so you are telling your compiler...

Hey I will need this

int swordString();

but then later in the code you are telling:

hey resolve this:

int a = swordString(age);

so what are you expecting from the compiler to do?

it will complain....

you must carefully implement the method according to what you prototyped...

Upvotes: 2

Daniel A. Thompson
Daniel A. Thompson

Reputation: 1924

Change your function prototype from:

int swordString();

to

int swordString(int a);

Upvotes: 3

Related Questions