1

This is the first code I have tried to run I have everything working but can't seem to get my "else" statement to display when I type in random characters after "are you satisfied with this output"

int main ()
{
  int Fahrenheit, Celsius, y, Y;
  cout << "Please enter a tempautre in Fahrenheit: ";
  cin >> Fahrenheit;
  cout << "The tempature you entered is: " << Fahrenheit << ".\n";
  Celsius = (Fahrenheit-32) / 1.8; // tempature conversion formula
  cout << "The tempature in Celsius is: "<< (Celsius) << ".\n";
  cout << "Are you satisified with output: " ;
  cin >> y, Y;
  if ((y, Y == y)  ||  (y, Y ==Y))
  {
    cout << "Thank You";
  }
  else
  {
    cout << "Thanks for your input";
  }  
  return 0;
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • `cin >> y, Y;`, `(y, Y == y)`, and `(y, Y ==Y)` do not do what you seem to be trying to do. – Retired Ninja Jan 20 '20 at 04:59
  • I am required to have the if/else statements in my code I have tried many variations of cin >> y, Y;, (y, Y == y), and (y, Y ==Y) with varying results. – Programming Newb Jan 20 '20 at 05:06
  • You haven't seen any example of code that looks like `cin >> y, Y;` or `(y, Y == y)` anywhere. I think you're confusing identifiers (variable names) with values (e.g. characters like `'y'` and strings like `"y"`). Also, you need to pay attention to types. There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 20 '20 at 09:27

3 Answers3

3

Firstly, it's not correct to write it:

    (y,Y == y)

A boolean expression may only have one variable on each side, like this:

    (y == Y)// or
    (Y == Y)

You can't put two variable separated by a comma (,) in the same side.

Finally, the inclusive or operator (||) means that if one of these two boolean expressions is true,

if ((y, Y == y) || (y, Y ==Y)) // these ones here

the final result is also true. And since Y is always equals to Y

    (y,Y==Y)

your final result will be always true.

Also, I don't understant why y and Y are integers. I think you meant something like that, didn't you?

    char y;
    cout << "Are you satisified with output (y/n): ";
    cin >> y;
    if(y == 'y')
    {
        cout << "Thank you" << endl;
    }
    if(y == 'n')
    {
        cout << "Thanks for input" << endl;
    }
isocppforbids
  • 389
  • 1
  • 12
  • On the last part, I think the intention is to take both capital and non-capital `y` as confirmation: `if (y == 'y' || y == 'Y')`. Also, small bug: You require the answer before asking the question (`cin >> y` before `cout << ...`). :) – Frodyne Jan 20 '20 at 08:29
  • Oh, and there is a build in comma operator, so `(y, Y == y)` does technically make sense in C++. But you are correct that it is used in a nonsensical way here. For reference: https://en.cppreference.com/w/cpp/language/operator_other (Look for *Built-in comma operator*). – Frodyne Jan 20 '20 at 08:35
2

Input stream objects can read and interpret input from sequences of characters. But one line each time.

You can't do cin>> y, Y; as std::cin takes only a line, instead you can do it separately.

And also this part is wrong:

if ((y, Y == y)  ||  (y, Y ==Y)){}

instead you should write:

if (input /*which should be char type*/ == 'Y' || input == 'y')

This part is also wrong:

Celsius = (Fahrenheit-32) / 1.8; // temperature conversion formula

Your decimal part is lost, as Celsius is of type int

0

This part of your code doesn't look good :

cin >> y, Y;
  if ((y, Y == y)  ||  (y, Y ==Y)) {cout << "Thank You";
  }
  else
  {
    cout << "Thanks for your input";
  }  

To check the user input you can write something like:

char response;
cin >> response;
if( ('y' == response) || ('Y' == response) )
{
    cout << "Thank You" << endl;
}
else
{
    cout << "Thanks for your input" << endl;
}

Also, you must understand the difference between a char and a string(vector of chars). By typing a random string in console, the cin will get only the first character because your input variable is of type char.

Theodor Badea
  • 465
  • 2
  • 9