-3

I'm a beginner in programming and I am currently trying to code things myself and I encountered a question that tells me to let a user choose from number 1-12 and it will display the month it represents (ex. 1=January) using while and switch loops. I'm now so brain dead need help.

edit: it loops if I entered a wrong input like putting a or 99.

#include <iostream>

using namespace std;

int main() 
{
   
    int months = 0;
    cout << "Enter a number from 1-12 (or Q to quit):   " << endl;
    cin >> months;
while (months != 'Q')
{
switch (months){
                case 1:
                    cout << "January" << endl;
                    break;

                case 2:
                    cout << "February"<< endl;
                    break;

                case 3:
                    cout << "March" << endl;
                    break;

                case 4:
                    cout << "April" << endl;
                    break;

                case 5:
                    cout << "May" << endl;
                    break;

                case 6:
                    cout << "June" << endl;
                    break;

                case 7:
                    cout << "July" << endl;
                    break;

                case 8:
                    cout << "August" << endl;
                    break;

                case 9:
                    cout << "September" << endl;
                    break;

                case 10:
                    cout << "October" << endl;
                    break;

                case 11:
                    cout << "November" << endl;
                    break;

                case 12:
                    cout << "December" << endl;
                    break;
                default:
                    
                     {
                           cout <<"You've entered an invalid response. Please only select from numbers 1- 12.\n"; 
                           break;
                     }
}
                        }

//I hve no idea what to do here

return 0;

}
rioV8
  • 24,506
  • 3
  • 32
  • 49
R-301
  • 9
  • 1
  • 1
    you seem to want `months` to be a `char` and an `int`. You need to decide for one – 463035818_is_not_an_ai Oct 18 '22 at 11:52
  • 2
    You should also refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which are also available as PDFs for free. – Jason Oct 18 '22 at 11:53
  • Please [edit] your question to indent the code consistently. That will make it easier to read and understand it. – Some programmer dude Oct 18 '22 at 11:53
  • 1
    `while (months != 'Q')` - Nothing in the loop ever *modifies* `months`. So if it doesn't equal `'Q'` right away then it will *never* equal `'Q'`. – David Oct 18 '22 at 12:04
  • All possible inputs need to either be of type `int` (the easier approach for OP), or of type `std::string`. This is because we are dealing with possible double-digit numbers and *some* symbol that should signify that we want to quit. – sweenish Oct 18 '22 at 12:45
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 18 '22 at 13:32
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 18 '22 at 13:41
  • And if you enter a valid moth, it does not loop? – U. Windl Oct 23 '22 at 21:18

4 Answers4

4

Two issues in your code:

First you read user input once outside of the loop. When the loop is executed once then it is executed forever, because the condition never changes. Move reading input inside the loop.

Second, you mixed up characters, char, and integers ,int. 'Q' is a character literal, its a char. 1,2, etc are integer literals, they are ints.

char is in fact also a number type, but specifically with input and output char is treated differently, because the "numbers" are treated as representations of characters. A common encoding is ASCII.

Pop quiz: What do you have to enter to make this code print foo on the console:

#include <iostream>
using namespace std;


int main()
{
    int x;
    std::cin >> x;
    if (x == 'Q') std::cout << "foo\n";
}

Answer: It depends. With the usual ascii encoding 'Q' == 81 so you need to enter 81 to make x equal to 'Q'.

std::cin >> x reads an integer. If the user enters Q then reading an int will fail. std::cin will be in an error state and x will get 0 assigned.

Decide for one: Use an int then you cannot read user input Q. Or use char then you need to compare the input against the characters '1','2' etc.

Alternatively read a std::string and parse the input to see if it is a character or a number.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • @R-301 if it is confusion you need to ask or tell me what specifically is confusing, only then I can clarify – 463035818_is_not_an_ai Oct 18 '22 at 12:30
  • Why do we need to change it from int to char? I mean we ask the user a "number" and not a char right? – R-301 Oct 18 '22 at 12:35
  • @R-301 i did not say that you *need to* change it from int to char. I only said if you want to read a `Q` from user input then that wont work with `int`, because `Q` is not an integer – 463035818_is_not_an_ai Oct 18 '22 at 12:40
  • @R-301 in your code `cin >> months;` tries to read an integer from stdin. If the user enters something else than an integer then reading an integer fails – 463035818_is_not_an_ai Oct 18 '22 at 12:41
  • ohh now I got it. I have edited my code now I don't get why my code loops if the user inputs wrongly. Is there a fix? – R-301 Oct 18 '22 at 12:47
1

You need to take the input inside the while loop not outside it ! because the while loop depends on what the user will write so you just need to correct it to this form



while (months != 'Q')
{
    cout << "Enter a number from 1-12 (or Q to quit):   " << endl;
    cin >> months;

}

Update : if the user writes an invalid input your program will go inside an infinite loop again so you should think about how to terminate it immediately after an invalid input

  • Good sir! Your "update" is what making me go mad. It keeps looping. I thought break would suffice or is it not? – R-301 Oct 18 '22 at 12:27
1

The problem is that you used while (months != 'Q'), not if (months != 'Q'). The compiler will always loop the while loop since months is always not equal to Q. Also, months is an int, i.e. inputting Q (this is not int) will crash your code, so you might want to modify it. You can either change Q into an int, or use char month and compare with '1', '2' etc.

#include <iostream>
using namespace std;
int main() {
    int months;
    cin >> months;
    while (months != 'Q') {
        cout << "Enter a number from 1-12 (or Q to quit):   " << endl;
        switch (months){
            case 1:
                cout << "January" << endl;
                break;

            case 2:
                cout << "February"<< endl;
                break;

            case 3:
                cout << "March" << endl;
                break;
            //etc. (i'll shorten the code here)

            default: 
                cout << "Your input is invalid. Please only input numbers 1-12.\n"; 
                break;
        }
    }
    return 0;
}

PS: Please indent your code properly. You do not want your code to be a mess.

  • Noted! But how will you code it in for? Is while bad in this situation? – R-301 Oct 18 '22 at 12:28
  • If you are just receiving 1 input, `if` would be enough. However, if you have multiple inputs, add `cin >> month` at the end of the `while` loop. – ducksaysquack Oct 18 '22 at 12:38
  • @R-301 i see that you stated if you entered a wrong input it will loop infinitely. Is it only if you enter a wrong input will it loop infinitely or does it happen to each case? – ducksaysquack Oct 18 '22 at 12:54
  • only when the user inputs the wrong input then it will loop indifenitely. If I type the right things it will print just what I wanted but it will terminate the program – R-301 Oct 18 '22 at 12:59
  • Did you edit your code? I see that it has the same problem of looping infinitely no matter what input in your code. – ducksaysquack Oct 18 '22 at 13:24
1

Let's rename the variable and use a lookup table (array).

static const char *    month_names[] = 
{
    "No Month as 0",
    "January", "February", "March", "April",
    "May", "June", "July", "August",
    "September", "October", "November", "December"
};
int month_index = 0;
std::cout << "Enter month number: ";
std::cin >> month_index;
if ((month_index > 0) && (month_index < 12))
{
    std::cout << "Month name is: " << month_names[month_index] << "\n";
}
else
{
    std::cerr << "Invalid month number\n";
}

In the case of mapping a number to a name, arrays work very well. You can also search the array for the month name and the index will be the month number.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154