0

I want the program to output the "year" after user types that year; however, all that seems to happen is the default statement occurs. I used similar code from a basic calculator which worked fine with char and float but, with this, I cannot work out where I am wrong.

// Simple program to produce a description of an event that happened within the chosen year.

#include <iostream>

using namespace std;

int main() 
{
    int nYear;

    cout << "Enter a year between 1980 and 1982: ";
    cin >> nYear;

    switch(nYear)
    {
    case '1980':
        cout << "1980"; // I will replace "year" with fact once code works.
        break;

    case '1981':
        cout << "1981";
        break;

    case '1982':
        cout << "1982";
        break;

    default:
        cout << "Error try again with specified year";
        break;
    }
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

3 Answers3

1

Your switch-block uses multi-character literals, e.g. '1980' to compare them with int nYears - this can't work (see here about single quote multi-characters). You never want to use those. Instead, use plain integral literals, because an int is what you are reading in from standard input.

switch(nYear)
 {
   case 1980:
   cout << "1980"; // I will replace "year" with fact once code works.
   break;

 // ...
}
lubgr
  • 37,368
  • 3
  • 66
  • 117
0

I'm not familiar with C++, but declaring int nYear and then trying to compare it to a string (case '1980') gives me the idea that that will be your issue.

Lieven Luyckx
  • 132
  • 2
  • 12
  • `'1980'` is not a string. `"1980"` would be, but single quotes are for a **character**. `'1980'` is a bit of a quirk; it's a multi-character literal; its type is `int`; its value is implementation-defined. – Pete Becker Jul 31 '20 at 12:59
0

You're trying to compare an integer to a string with two single quotes '', which is impossible. Single quotes can only hold single characters, double quotes can do multiple. But still, it's an error.

A better way to write the code with safety (notice the comments):

#include <iostream>

// Avoiding to use 'using namespace std' to prevent
// the naming ambiguity (good practice)

int main(void) {
    int nYear;

    std::cout << "Enter the year: ";
    std::cin >> nYear;

    // Verifying if the input was an integer
    if (!std::cin.good()) {
        std::cout << "Incorrect input!";
        return 1;
    }

    switch (nYear) {
        case 1980:
        case 1981:
        case 1982:
            // Converting the integer to an std::string
            std::cout << std::to_string(nYear);
            break;

        default:
            std::cout << "Out of bounds.";
            break;
    }
    std::cout << std::endl;
    
    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • 1
    Thank you for your input you have helped me regarding using namespace std; . I was not intending to compare a int and a string, i was looking for the user to input a int, then output would be a string. My mistake was using '1980': and not just 1980:. sorry for any confusion and thank you again. – IsolatedSpace Jul 31 '20 at 13:30