If statement with multiple criteria

I am not sure how to ask this. I have been coding since my first post on this forum at 2, working on some school work and some fun work. This is a school project and I am trying to do WAY more than is required by the project. More for my own self-teaching than the class grade.

I am trying to take a numerical value of a month and error back invalid input. It needs to error back on any input not a number and on any number larger than 12. I am doing this with month, day, and year, but will only post month since the solution should work with all three.

First attempted solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

using namespace std;

int main()
{
   unsigned int month;
   cout << "Input the month (numerically: ";
   while (!(cin >> month) || month > 12)
   {
      cin.clear();
      cout << Invalid Input! Input the month (numerically): "
      cin.ignore(100,'\n');
   }
   cout << month << endl;
   return(0);
} 


That wouldn't work for some reason. My current code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;

int main()
{
   unsigned int month;
   cout << "Input the month (numerically): ";
   if (!(cin >> month))
   {
       cin.clear();
       cin.ignore(100,'\n');
       month = 13;
       while (month > 12)
       {
           cin.clear();
           cout << "Invalid Input! Input the month (numerically): ";
           cin.ignore(100,'\n');
       }
    }
    cout << month << endl;
    return(0);
}


I can't help feeling I am going WAY off the deep end of absurd. I even started doing an "if else" to double check and an "else" to continue the code... Any guidance would be much appreciated!

Thanks in advance,
Arcie
1
2
3
4
5
6
7
 month = 13;
       while (month > 12)
       {
           cin.clear();
           cout << "Invalid Input! Input the month (numerically): ";
           cin.ignore(100,'\n');
       }


I am tired so forgive me if I am wrong, but, if you assign month to = 13 then make a loop statement that says while > then 12 do this, but never give the opportunity to change month in that loop isn't that a problem? Aside from this if you got specific errors in your compiler please post.
Sorry for the delay between posts. The issue isn't the compiler. It compiles just fine, just doesn't do what I want it to do. I want the program to spit an error if the user types a character or string instead of a number AND to if the user inputs a value above the possible. Simply put, error on the month if it exceeds 12, error on the day if it exceeds 31 (thinking about using a switch statement to set the max value of the day based on the month chosen, but still new to that area), and the year if it exceeds 99.

I hope that clears up my dilemma. It just seems like there is an easy way of doing this that I am missing.
The first you attempts works for me. All I really did was correct the small mistake on line 12 there was a missing semi colon. I thought it was a copy and paste error maybe it is. Here is my output.
 
~/Documents/test> g++ -Wall -pedantic test.cc 
~/Documents/test> ./a.out
Input the month (numerically: hil
Invalid Input! Input month!
ok
Invalid Input! Input month!
15
Invalid Input! Input month!
Nah Nah
Invalid Input! Input month!
14
Invalid Input! Input month!
9
9


Also, I tried on the Visual c++ compiler and got no errors. However, your second attempt won't work because of the month = 13 as the program needs to get back to that cin line before you change the value again.
Last edited on
Can you post your source? I would love to compare it to my original!
Ok, that's weird... I swear that code wasn't working yesterday! Just tested it and it works fine. Maybe there was something in the full program that wasn't working properly? I'll insert it to the rest of the program and see what happens... Thanks!
Topic archived. No new replies allowed.