Jump to content

C++ help

Go to solution Solved by madknight3,

How do I check to see if they used more or less than 4 digits on the year?

 

I suppose you could use the obvious

if (year < 1000 || year > 9999)

edit: Just saw this 

 

But that would allow a user to enter something like 1, or 333. I need it to be in 4 digits, so something like 0001 or 0333

 

You could get the input as a string instead of an int and use length().

How do I check to see if they used more or less than 4 digits on the year?

#include <iostream>#include <iomanip>using namespace std;int main(){    int Day;    int Month;    int Year;    int Year2num;        cout << "Enter the day\n";    cin >> Day;    cout << "Enter the Month\n";    cin >> Month;    cout << "Enter the Year in a 4 digit format(XXXX)\n";    cin >> Year;        if (Day >= 1)    {        if (Month >= 1)        {            if (Year <= 1)            {    cout << "Error: The numbers can not be less than 1";                return 0;            }        }        else        {    cout << "Error: The numbers can not be less than 1";            return 0;        }    }    else    {            cout << "Error: The numbers can not be less than 1";        return 0;    }    Year2num = (Year % 100);    if (Year2num == Day * Month)        cout << "This is a magic date!\n";    else        cout << "This is not a magic date!\n";    cout << "Program complete";    return 0;}
Link to comment
https://linustechtips.com/topic/476118-c-help/
Share on other sites

Link to post
Share on other sites

if(year >= 9999) /
You could also do:

if(year/10^4 != 0)

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/476118-c-help/#findComment-6380471
Share on other sites

Link to post
Share on other sites

But that would allow a user to enter something like 1, or 333. I need it to be in 4 digits, so something like 0001 or 0333

If you assign 0333 to an int then that int will hold the value 333 though.

int x = 0333; printf("%d\n", x); //will print 333 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/476118-c-help/#findComment-6380482
Share on other sites

Link to post
Share on other sites

if(year/10^4 != 0)
^ is XOR from what I can remember. Either use pow, but that's really not needed, or use 1e4 or even 10000.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/476118-c-help/#findComment-6380710
Share on other sites

Link to post
Share on other sites

How do I check to see if they used more or less than 4 digits on the year?

 

I suppose you could use the obvious

if (year < 1000 || year > 9999)

edit: Just saw this 

 

But that would allow a user to enter something like 1, or 333. I need it to be in 4 digits, so something like 0001 or 0333

 

You could get the input as a string instead of an int and use length().

Link to comment
https://linustechtips.com/topic/476118-c-help/#findComment-6380763
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×