Birdy McBirdFace
Birdy McBirdFace

Reputation: 35

"return value type does not match the function type" and other VS compile errors

I started to learn C++ a few days ago and now I'm trying to make my first program ever, a "phone-book" app. The name of the people I know will appear, I will enter the name of the person I need the number and their number will appear.

But now I am debugging for a while and I still don't get what's wrong with my code! I'm pretty sure it's obvious though, I'm just too new to get it.

    #include <stdafx.h> // Visual Studio users need to uncomment this line
    #include <iostream>

    int nameAppears()
 {
    std::cout << "Alex" <<    std::endl;
    std::cout << "Andre" << std::endl;
    std::cout << "Guy" <<   std::endl;
    std::cout << "Grand-ma" <<        std::endl;
    std::cout << "Grand-pa" <<        std::endl;
    std::cout << "Jérémy" <<  std::endl;
    std::cout << "Manon" <<   std::endl;
    std::cout << "Nathalie" <<  std::endl;
    std::cout << "Stéphanie" << std::endl;
    std::cout << "Oliver" << std::endl;
}


int enterName()
{
    std::cout << "Enter the name you wish to obtain the number:";
    int name;
    std::cin >> name;
    return name;
}


int link(name)
{
    if (name == "Alex")
        return "586 6532";
    if (name == "Andre")
        return "569 8522";
    if (name == "Guy")
        return "850 6589";
    if (name == "Grand-ma")
        return "482 4875";
    if (name == "Grand-pa")
        return "453 9963";
    if (name == "Jérémy")
        return "654 3828";
    if (name == "Manon")
        return "965 4541";
    if (name == "Nathalie")
        return "770 6916";
    if (name == "Stéphanie")
        return "546 5482 ";
    if (name == "Oliver")
        return "246 5554";
}


int printNumber (int number)
{
    std::cout << "The number is: " << number << std::endl;
}


int main()
{
    //Make all the names appear
    nameAppears();

    //Get User's input
    int name = enterName();

    //Link Name to number
    int number = link(name);

    //Print the desired number
    printNumber(number);
}

Here is a list of all the errors I got while compiling this on Visual Studio 2013:

Upvotes: 0

Views: 31412

Answers (3)

LOLOL
LOLOL

Reputation: 1

Use using namespace std and string name you are using an int. Integers are only for numbers not strings. string name; cin >> name

Upvotes: 0

BiagioF
BiagioF

Reputation: 9705

Where to start?

1. Why a name should be a number?

In your function:

int enterName()
{
    std::cout << "Enter the name you wish to obtain the number:";
    int name;
    std::cin >> name;
    return name;
}

The user is supposed to insert a name but the type of a name variable has been declared as int (which is a type for a integer number). My question now is: why the name of a person should be codified as a number?

How to solve this?

Simply using a string type.

#include <string>  // You have to include this header to use string object

// ...

std::string enterName()
{
    std::cout << "Enter the name you wish to obtain the number:";
    std::string name;
    std::cin >> name;
    return name;
}

Note: there are many considerations about how to get a string from the standard input, but I'm not your c++ teacher, and in your case I think that argument is very far from your skills now.

2. C++ is statically type language (more or less).

Another problem is here:

int link(name)
{
  // do something ...
}

In your declaration function name has not type. This is an error! A variable must to have a type as argument.

Moreover your body function returns a "string" type:

return "965 4541";  // return a const char[]

So why you've declared your function returns a int type?

Correct form:

std::string link(const std::string& name)  // declaration signature

3. And again...

Even in this function the type are wrong:

int printNumber (int number)
{
    std::cout << "The number is: " << number << std::endl;
}

It should be:

void printNumber (const std::string& number)
{
    std::cout << "The number is: " << number << std::endl;
}

void as return type because your function does return nothing.

Final Conclusions

I see a lack in your baseline skills about the language. I suggest you to study a good C++ book, and start coding from that.

Upvotes: 1

user3183671
user3183671

Reputation:

  1. "identifier "name" is undefined" you are not declaring the variable name inside the function link, the correct is int main (string name), you need that string.

  2. "return value type does not match the function type" using as example the function int link(string name), if you want to return the phone number or you return it like 8506589 (no spaces) or you change the return type to string so it consider a string and accept things like - or space.

3- No need for stdafx header, erase this line and google it for understanding more about it.

4- You declared name as an int, you should have declared as a string.

Dude, your code have so many problems, you should consider going to some youtube videos is you are trying to learn by yourself and watch some videos for c programming first, try thenewboston videos... I helped with just few of your mistakes.

Upvotes: 0

Related Questions