Home > database >  My program keeps looping and I don't know why (c )
My program keeps looping and I don't know why (c )

Time:10-19

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;

}

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

Simulate the execution step by step as if you were the computer. Do that blindly, as a machine would do, i.e. by strictly fulfilling the effect of the instructions.

You will very soon see where your mistake is.

CodePudding user response:

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

  •  Tags:  
  • c
  • Related