FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C (friend needs help bad....and fast!)





m-productions
Code:
printf("Please indicate you specificed month: ");   
    scanf("%d", &month);
    if (month = 1,3,5,7,8,10,12, days=31 );
    if (month = 4,6,9,11, days=30 );
    if (month = 2, days=28 );
    printf("There are %d days in this month.\n\n", days);
    printf("Your current balance is $%8.2f.\n\n", balance);

keeps thinking every month is February -_-

that is what he has, and what hes problem is
rheanna
m-productions wrote:
Code:
printf("Please indicate you specificed month: ");   
    scanf("%d", &month);
    if (month = 1,3,5,7,8,10,12, days=31 );
    if (month = 4,6,9,11, days=30 );
    if (month = 2, days=28 );
    printf("There are %d days in this month.\n\n", days);
    printf("Your current balance is $%8.2f.\n\n", balance);

keeps thinking every month is February -_-

that is what he has, and what hes problem is


if (month = 2, days=28 ); Maybe because 2 stand for February, so keeps thinking you want the out put of February. From the way I see it but I don't code. Sorry.. GL

specificed * Spelling is wrong. But not sure if that goes with the script and if it has to be right.
m-productions
I just got it to work for him...I dont know C, but i figured it cant be to much from c++ and c++ is just about the same as AS...so I gave him a actionscript code..moded it a bit...and it worked..ROFL
rheanna
Smile wtg
MrBlueSky
It should be:

Code:

printf("Please indicate you specificed month: ");   
    scanf("%d", &month);
    days=31;
    if (month = 4,6,9,11) days=30;
    if (month = 2) days=28 ;
    printf("There are %d days in this month.\n\n", days);
    printf("Your current balance is $%8.2f.\n\n", balance);


by the way. Smile
Twisol
Your friend was confusing the == and = operators. The '=' operator assigns, and the '==' operator compares. You were assigning 2 to month, so it always believed it was February, because "month = 2" always evaluates to true.

Unless its "= 0". There, it will probably be false, but honestly, I've never needed to check that, so I'm not sure...

EDIT: And I have NEVER seen the comma operator used like that, and I doubt it works right. Use "if (month == 1 || month == 3)" and so on for that. The '||' operator means "or", so basically, "if month is January, or month is March". Dont confuse it with '|'... trust me.

Code:

printf("Please indicate you specificed month: ");   
scanf("%d", &month);

switch (month)
{
    // These multiple case tags basically mean, if the month
    // is any of these grouped together, set days to 31.
    case  1:
    case  3:
    case  5:
    case  7:
    case  8:
    case 10:
    case 12:
        days=31;
        break; // This breaks out of the switch so control doesn't follow
                  // down to the months with 30 days.

    case  4:
    case  6:
    case  9:
    case 11:
        days=30;
        break;

    case 2:
        days=28;
        break;
}

printf("There are %d days in this month.\n\n", days);
printf("Your current balance is $%8.2f.\n\n", balance);


I think your friend needs a good C++ book... 'if' statements follow the form of "if (conditions) { statements; }", such as "if (a == 1) {b = 2;}" or "if (myName == "Twisol") { isOnline = true; }"
MrBlueSky
Twisol wrote:
Your friend was confusing the == and = operators. The '=' operator assigns, and the '==' operator compares. You were assigning 2 to month, so it always believed it was February, because "month = 2" always evaluates to true.



Oh yes, I forgot to correct that mistake in my code.

Code:

if (month = 1,3,5,7,8,10,12, days=31 );


In this line, days is always set to 31, and the condition is always true. In C (and more languages) the comma (,) operator evaluates all the expressions and returns the value of the last one. So:

Code:

b = 3,a=5,6;


sets b to 6 and a to 5. The 3 is ignored.
Antoine_935
MrBlueSky wrote:

Code:

b = 3,a=5,6;


sets b to 6 and a to 5. The 3 is ignored.


Hey.
Didn't know this. It may be useful ! Thanks
Indi
Antoine_935 wrote:
MrBlueSky wrote:

Code:

b = 3,a=5,6;


sets b to 6 and a to 5. The 3 is ignored.


Hey.
Didn't know this. It may be useful ! Thanks

Just because something is possible doesn't mean that it's useful. The comma operator just confuses the hell out of 95% of C and C++ programmers, and doesn't really offer any benefit. Just about the only time it's useful is inside of if statements, and even there it's mostly a clever hackish trick that's unnecessary.

Write clear code, not clever code.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.