我今天制作了一个程序,当我最终完成它并完成重新编译时,它最终编译完成!我知道的人的名字出现了,然后我必须输入我希望拥有该号码的人的姓名。问题是,无论我输入什么名字,程序都只输出" 15"我真的不知道为什么!
#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
}
答案 0 :(得分:2)
编辑:我最初只有long
,但请参阅下面的评论主题,了解为什么我将其编辑为long long
。
我做了一些改变:
link
函数并不总是返回值。我在底部添加了一个return 0;
,其中没有一个名称匹配。int
。我转而到处使用long long
。请注意,我的编译器为前两个问题生成了警告。如果您没有,请考虑更改您的设置,以确保您看到有价值的警告。
以下代码对我有效:
#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);
}