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

Program ouput "15" for no reason?

I made a program today and when I finally completed it and finished debbuging it, it finally compiles! The name of the people I know appears, and then I have to enter the name of the person I wish to have the number. the thing is, whatever name I input, the programs simply output "15" And I really don,t get why!

#include "stdafx.h"
#include <iostream>
#include <string>
int number;
std::string name1;

//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; commented this out because it
//never worked, I simply put it in main()
//std::cout << "Nathalie" << std::endl;
//std::cout << "Stéphanie" << std::endl;
//std::cout << "Oliver" << std::endl;
//}


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


int link(std::string name1)
{
if (name1 == "Alex")
return 9856452535;
if (name1 == "Andre")
return 5641598453;
if (name1 == "Guy")
return 8459563258;
if (name1 == "Grand-ma")
return 8457892653;
if (name1 == "Grand-pa")
return 5689852565;
if (name1 == "Jérémy")
return 7894521564;
if (name1 == "Manon")
return 8453262145;
if (name1 == "Nathalie")
return 6589253678;
if (name1 == "Stéphanie")
return 5689433645;
if (name1 == "Olivier")
return 5695658321;
}


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


int main()
{

//Make all the names appear
//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 << "Olivier" << std::endl;

//Get User's input
std::string name1 = enterName();

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

//Print the desired number
printNumber(number);
std::cin.clear(); // reset any error flags
std::cin.ignore(32767, '\n');
std::cin.get(); // get one more char from the user

}

Answer

EDIT: I originally had just long, but see comment thread below for why I've edited this to be long long instead.


I made a couple changes:

  1. Your link function doesn't always return a value. I added a return 0; at the bottom for the case where none of the names match.
  2. Your numbers are too big to fit in an int. I switched to using long longs everywhere.
  3. I did a little minor cleanup (deleted unused code and global variables).

Note that my compiler generated warnings for the first two issues. If yours didn't, consider changing your settings to make sure you see valuable warnings.

The below code works as expected for me:

#include <iostream>
#include <string>

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


long long link(std::string name1)
{
    if (name1 == "Alex")
        return 9856452535;
    if (name1 == "Andre")
        return 5641598453;
    if (name1 == "Guy")
        return 8459563258;
    if (name1 == "Grand-ma")
        return 8457892653;
    if (name1 == "Grand-pa")
        return 5689852565;
    if (name1 == "Jérémy")
        return 7894521564;
    if (name1 == "Manon")
        return 8453262145;
    if (name1 == "Nathalie")
        return 6589253678;
    if (name1 == "Stéphanie")
        return 5689433645;
    if (name1 == "Olivier")
        return 5695658321;

    return 0; // if none of the names match
}


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


int main()
{
    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 << "Olivier" << std::endl;

    //Get user's input
    std::string name1 = enterName();

    //Link name to number
    long long number = link(name1);

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