Jossie Calderon
Jossie Calderon

Reputation: 1425

What's wrong with the following C++ code involving input and string?

#include <iostream>

typedef int temperature;
temperature celsiustemperature[4];

int main()
{
    using namespace std;
    cout << "Enter a start temperature in celsius: ";
    cin >> celsiustemperature[0];
    cout << "Enter an end temperature in celsius: ";
    cin >> celsiustemperature[1];
    cout << "You printed " << celsiustemperature[0] << " and " << celsiustemperature[1] << "." << " Is this correct?" << endl;
    char szYesOrNo;
    cin >> szYesOrNo;
    switch (szYesOrNo)
    {
        case "yes":
               cout << "win";
               break;
        case "no":
              cout << "winner";
              break;
     }
     return 0;
}

I cannot figure out what is wrong with the following code. Sorry for not adding comments; I want it to print win if the user inputs yes and winner if user inputs no.

Upvotes: 1

Views: 126

Answers (6)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

char szYesOrNo;

This is a single character.

switch (szYesOrNo)
{
    case "yes":

This will compare the value of the single character with the address of the string "yes", which isn't what you want.

Either input to a string, or compare chars.

To compare chars:

switch (szYesOrNo)
{
    case 'y':

To compare strings, you can't use a switch statement. You could use nested if/else:

string szYesOrNo;

if(szYesOrNo == "yes") {
    cout << "win";
} else if(szYesOrNo == "no") {
    cout << "winner";
}

You can use == when comparing a string to a char[] (which is what "yes" is). string makes sure the contents are compared rather than just the addresses.

Upvotes: 0

WassiM ZgheiB
WassiM ZgheiB

Reputation: 119

szYesOrNo is a character, so you cannot switch it in a case of "yes" which is a string ( 3 characters )

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

First of all, szYesOrNo is a single char and so can only contain one character. When you do cin >> szYesOrNo;, you are only reading y or n.

Second, you're trying to use a switch statement to compare this single char to the string literals "yes" and "no". This comparison doesn't make sense. The string literals are of type "array of N const char".

Instead, use std::string like so:

string szYesOrNo;
cin >> szYesOrNo;
if (szYesOrNo == "yes") {
  cout << "win";
} else if (szYesOrNo == "no") {
  cout << "winner";
}

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

The main problem with your code is that you declare a variable as char szYesOrNo; which can only hold ONE symbol, such as a letter, but then expect the character to enter a whole word. You should use a string to do this instead. When you fix that, you will need to use if statements instead of a switch statement to make decisions.

Upvotes: 0

levis501
levis501

Reputation: 4207

You can't switch on a string. Only on an integer-like type (ints, enums, chars, longs).

Upvotes: 0

Andy Prowl
Andy Prowl

Reputation: 126432

This won't compile, because szYesOrNo is a char, and you are comparing it with string literals in your switch statement. String literals are of type const char[], which cannot be directly compared to char.

Use std::string instead of a char:

std::string szYesOrNo;

This will also force you to remove the switch, because switch cannot operate on a string value (also notice, that your switch does not have a default case, so it won't handle incorrect input). Just do it this way:

if (szYesOrNo == "yes")
{
    cout << "win";
}
else if (szYesOrNo == "no")
{
     cout << "winner";
}
else
{
    // Handle wrong input...
}

Upvotes: 4

Related Questions