-4

So I'm fairly new to c++ and programming in general, so just for learning purposes, I am trying to extend the use of functions, but I'm not sure what do create for the arguments of my yesorno(). I created this and thought it would work, but as I understand, it always prints the return of '0' (which is also unwanted, but I understand why it does it currently) but does nothing with the variables and ends the program. Does anyone have anything that would spare me a remote crash course on everything I'm doing wrong?

#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;
}
  • 1
    _"Does anyone have anything that would spare me a remote crash course on everything I'm doing wrong?"_ Stack Overflow is a Q&A (more like a forum), not a place for crash courses/tutorials/mentoring (like a forum or chat room). – Lightness Races in Orbit Jul 07 '14 at 11:55
  • 1
    [This](http://www.amazon.com/C-Programming-Language-4th/dp/0321563840/ref=sr_1_1?s=books&ie=UTF8&qid=1404734203&sr=1-1&keywords=stroustrup) comes to mind... Or any one of myriad others... – twalberg Jul 07 '14 at 11:58
  • Try `int yesorno_input = yesorno();` to capture the result of the function call in a variable. Then review the early chapters of your [introductory book](http://stackoverflow.com/questions/388242) to remind yourself how variables work. – Mike Seymour Jul 07 '14 at 11:58

3 Answers3

2

The thing is you want to get the result back from the yesorno() right.? And in your program you have made some mistakes like as noted in below code. And as you want to have a parametrized function yesorno() try following:

#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;
}

Hope this will help you

A B
  • 1,461
  • 2
  • 19
  • 54
1

Corrected version of your code. Learn to DEBUG your code. This way you will learn the fun of programming. You should know the significance of each line in your code.

#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;
}
gmfreak
  • 399
  • 4
  • 12
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;

}

In your code, at the end of yerorno() function you use return 0; statement. This causes whatever the input is, return 0 to called line.

In order to correct this: You have to use your return value of the yesorno function in order to use the choice. You can do this with yesorno_input = yesorno();

sertsedat
  • 3,490
  • 1
  • 25
  • 45