Birdy McBirdFace Birdy McBirdFace - 3 months ago 16
C++ Question

"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:


  • "cannot open source file "stdafx.h""

  • "identifier "name" is undefined" (2 times in a row)

  • "return value type does not match the function type" (10 times in a row)

  • ""name": undeclared identifier"

  • ""link: function-style initializer appears to be a function definition"

  • "term does not evaluate to a function taking 1 arguments"


Answer

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.