Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

break statements in c++

  • 28-02-2004 1:17pm
    #1
    Closed Accounts Posts: 439 ✭✭


    Right I wrote this program to take any date for any year greater then 1800 and output the day. Program does that grand. Things is Where i use break statement I want to also be able to cout<<"This is not a real date"<<endl; except it won't let me, is there a way around it.

    [PHP] #include <iostream>
    #include <string>
    using namespace std;
    int leap(int year);
    int offset(int yy);
    int main()
    { int dd,mm,yyyy, date[] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30};
    string day[] = {" ","wednesday","Thursday","Friday","saturday","sunday","monday","tuesday",};
    while (true)
    {
    cout<<"Enter The date or 0 to exist: \n";
    cin >>dd>>mm>>yyyy;
    if ((dd == 0) || (dd > 31) || (mm > 12))break;
    if((leap(yyyy) == true))
    date[2] = 29;
    else date[2] = 28;

    if((dd > date[mm]) || (dd < 0))break;
    for (int i = 0; mm > i; i++)
    {dd += date;}
    dd = (dd + offset(yyyy))%7;

    cout<<"The day on the: "<<dd<<"/"<<mm<<"/"<<yyyy<<" is: "<<day[dd]<<endl;
    }
    return 0;
    }
    int leap(int year)
    { if ((year%400 == 0) || ((year%4 ==0) && (year%100 != 0)))
    return true;
    else
    return false;
    }
    int offset(int yy)
    {int Total_days = 0;
    for (int base = 1800; base != yy;base++)
    {
    if((leap(base) == true))
    Total_days += 366;
    else {Total_days += 365;}
    }
    return (Total_days%7);
    }[/PHP]


Comments

  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    replace this
    if ((dd == 0) || (dd > 31) || (mm > 12))break;
    

    with some scoping indicators, like this
    if ((dd == 0) || (dd > 31) || (mm > 12))
    {
        cout << "stuff" << endl;
        break;
    }
    
    and you will be fine.

    Except that you use integers for your dd/mm/yyyy variables and don't check properly for the cases where the user inputs negative values, using unsigned ints instead would be a good thing.

    [edit] Why this works:
    You are only allowed to have a single statement after an if statement, this is why
    if(condition)
        cout << "blah" << endl;
        break;
    
    will always break, only the cout is covered by the if statement

    If you use curly braces though you are telling the compiler that there is a statement block between those matching braces that is covered by the if statement, so
    if(condition)
    {
        cout << "blah" << endl;
        break;
    }
    
    causes both the cout and the break to happen when condition is true.


  • Closed Accounts Posts: 439 ✭✭Atreides


    Hmm I tried that and it kept closing on me, then i realised it was working just happening so fast dos was closing.

    what do you mean be usign unsigned ints however


  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    There are two types of integer in C++. A signed integer (int) and unsigned integers (unsigned int). Signed integers can have a positive or negative value an unsigned integer will always be interpreted as having a positive value (counting 0 as positive:))

    If you have variables that represent things which shouldn't ever be negative you should use unsigned integers for them (this is the case generally, sometimes with loop counters you need to be careful). Using an unsigned int means you only have to check if the number input by the user (or whatever) is less than it's upper bound, you don't have to check if it is less than 0.

    Take your check on dd
    if ((dd == 0) || (dd > 31) || (mm > 12))break;
    
    Here you check if the day is equal to zero or greater than 31 and break if it is either of these.

    This would be fine with unsigned ints but because dd is a signed int there is another case where it can be less than 0.


  • Closed Accounts Posts: 439 ✭✭Atreides


    Hmm I've taken care of the possibility now. What would happen if someone entered an negative number to an unsigned int, would itjust take the absolute value? If so that would be very handy.


  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    Originally posted by Skanger
    Hmm I've taken care of the possibility now. What would happen if someone entered an negative number to an unsigned int, would itjust take the absolute value? If so that would be very handy.
    It won't take the absolute value. I haven't looked at the iostream source so I can't confirm this, but if iostream reads "-2" as a negative integer and then assigns to an unsigned int the unsigned int will contain the value associated with the bit pattern of the negative int.
    This of course is based on iostream reading "-2" as -2 when it knows it is assigning to an unsigned integer.

    Given that most architectures these days use two's complement signed integer systems this will mean that a value of -1 will, in fact, be interpreted as 4294967295.


  • Advertisement
Advertisement