是或否函数,参数和返回void到cout

时间:2014-07-07 11:53:47

标签: c++ function

所以我对c ++和编程很新,所以仅仅为了学习目的,我试图扩展函数的使用,但是我不确定为我的参数创建了什么是还是不是()。我创造了这个,并认为它会起作用,但据我所知,它总是打印出' 0' (这也是不需要的,但我理解它为什么会这样做)但对变量没有任何作用并结束程序。对于我做错的一切,有没有人能够为我提供远程速成课程?

#include <iostream>

int main()
{
    int yesorno_input = NULL;
    std::cout << yesorno();
    if (yesorno_input == 1)
    {
        std::cout << "yes";
    }
    else
    {
        std::cout << "no";
    }
    std::cin.get();
}

int yesorno()
{
    int yesorno_input = NULL;
    char choice;
    std::cout << "Y\\N";
    std::cin >> choice;
    if (choice == 'y')
        int yesorno_input = 1;
    else if (choice == 'n')
        int yesorno_input = 0;
    else
        std::cout << "...";
    std::cin.get();
    return 0;
}

3 个答案:

答案 0 :(得分:2)

问题是你希望从yesorno()权利中取回结果。? 在你的程序中,你犯了一些错误,如下面的代码所示。 如您想要参数化函数yesorno(),请尝试以下操作:

#include <iostream>
int yesorno(char); // declaration of function yesorno

int main()
{
    int yesorno_input = NULL;
    char choice;

    // here you want to get input from user right? 
    std::cout << "Y\\N";
    std::cin >> choice;

    // here you want to get the value by passing char as a parameter right?
    // so pass the char as a parameter to function yesorno

    yesorno_input  = yesorno(choice);

    // print the answer
    std::cout << yesorno_input;

    // Check for the return value by using switch statement 
    // we can use if...else ladder too but it is convenient to use switch statement
    switch(yesorno_input)
    {
      case 0: 
        std::cout << "No";
        break;
      case 1: 
        std::cout << "yes";
        break;
      default: 
        std::cout << "Invalid Input";          
    }
    std::cin.get();
}

int yesorno(char choice)
{
    // you dnt need these things as you are passing value to the function
    //int yesorno_input = NULL;
    //char choice;
    //std::cout << "Y\\N";
    //std::cin >> choice;

    // just check here for the values and enjoy the output
    if (choice == 'y')
        return 1;
    else if (choice == 'n')
        return 0;
    else            
        return -1;
}

希望这会对你有所帮助

答案 1 :(得分:1)

更正了您的代码版本。学会调试你的代码。通过这种方式,您将学习编程的乐趣。您应该知道代码中每一行的重要性。

#include <iostream>

int yesorno(){
  int yesorno_input = NULL;
  char choice;
  std::cout << "Y\\N";
  std::cin >> choice;
  if (choice == 'y')
    yesorno_input = 1;
  else if (choice == 'n')
    yesorno_input = 0;
  else
    std::cout << "...";
  std::cin.get();
  return yesorno_input;
}


int main()
{
  int yesorno_input = NULL;
  yesorno_input = yesorno();
  if (yesorno_input == 1)
  {
    std::cout << "yes";
  }
  else
  {
    std::cout << "no";
  }
  std::cin.get();
  return 0;
}

答案 2 :(得分:0)

int main()
{
    int yesorno_input = yesorno();
    if (yesorno_input == 1)
    {
       std::cout << "yes";
    }
else
{
    std::cout << "no";
}
}

int yesorno()
{

std::cout << "Y\\N";
std::cin >> choice;
if (choice == 'y' || choice == 'Y')
    return 1;
else if (choice == 'n' || choice == 'N')
   return 0;

}

在您的代码中,yerorno() function末尾使用return 0;语句。这会导致输入无关,将0返回到被调用的行。

为了纠正这个问题: 您必须使用yesorno函数的返回值才能使用该选项。您可以使用yesorno_input = yesorno();

执行此操作