Show 80 post(s) from this thread on one page
Page 1 of 7 1234567 Last
• 03-25-2008
bobbie18
hi all
im really new to c programming so i just need some help from you guys to get me started
i got to do this task to create a program that will tell the user whether it is a leap yr and how many days there are in the month when enter...
so far i got this:

printf("Enter yr and month:");
scanf("%d", &a);
if *i got this type just working on it so ignore me*

{
printf("has (366) days\n");}
else{
printf("has (365) days\n");}

scanf("%d", &month);
if (month=1);
{
printf("Janurary has 31 days\n");}

else if (month=2);
{
printf("Feburary has 28 days\n");}
*/
}
return 0;
}

what did i do wrong???

and later on when im required to change the year like to proper words... how do i do it?

EG:

Enter year and month: 1980 2
nineteen hundred eighty<--------------- how do i do this for years from 1792 - 9999???
has three hundred and sixty six (366) days
February has 29 days

THANK YOU ALL FOR YOUR HELP AND I REALLY APPRECIATE IT
• 03-25-2008
matsp
Code:

`if (month=1);`
A) Doesn't do what you want. It does:
Code:

`if ((month = 1) != 0)`
Since 1 is always not equal to zero, what you've achieved is
Code:

`month =1; if (1) ...`
B) Contains only an empty statement ";" - so nothing actually gets done in the if-statement.

This in itself will probably lead to the "else if" part further down being an error, because the else doesn't belong to an if-statement.

--
Mats
• 03-25-2008
bobbie18
first of all thx a lot for ur reply
im only been learning c for a couple of days and my teacher is being a jackass
so how do i fix the if argument if i want my program to acknowledge that the 1 being entered is actually a month? how do i creat a printf statement for this if statemnet
• 03-25-2008
matsp
You probably want
Code:

`if(month == 1) ...`
There should never be a semicolon DIRECTLY after the parenthesis of an if-statement, as that means that the if-statement contains no code to execute for the "true" choice, which means that there's no need to have an if in the first place. [Unless you have an else after it, in which case the condition should be the opposite to what it is right now].

By the way, for days per month, a switch-statement is probably more suitable:
Code:

```switch(month) {   case 4:   case 6:   case 9:   case 11:       ... 30 days.       break;   case 2:       ... 28 or 29  days.       break;   default:       if (month < 1 || month > 12)  ... illegal month. ;       else ... 31 days       break; }```
Only use this if your teacher has introduced "switch" however.

--
Mats
• 03-25-2008
bobbie18
what other options are there if the bastard hasnt taught switch yet? thx a lot for ur time
• 03-25-2008
matsp
Code:

```if(month == x) ... else if (month == y) ... ... ...```
Of course, you only need to check some of them, like in the switch. If you KNOW that the value is between 1 and 12, then there are 5 months that aren't 31 days: February, April, June, September and October, so those are the only ones you need an if-statement for, the rest are 31 days because that's the only "option left".

--
Mats
• 03-25-2008
bobbie18
how do i line up my if statements and else statements if there are several of them?
and also what would i need to do if i want this:
Enter year and month: 1980 2
nineteen hundred eighty<--------------- how do i do this for years from 1792 - 9999???
has three hundred and sixty six (366) days
February has 29 days
• 03-25-2008
matsp
So, lets solve one problem at a time. Here is an example of a if/else/if/else chain:
Code:

```if (x == 1)     printf("x is 1"); else if (x == 2)     printf("x is 2"); else     printf("x is not 1 or 2");```
I'm not going to write the code for you, because you are not learning anything from that.

As to making "number into text", it's not terribly complicated, but you post something that starts us off, and we'll help you along - that's the "Homework policy" here on Cprogramming.com...

--
Mats
• 03-25-2008
bobbie18
THIS is what i got so far... is it right?

#include <stdio.h>

int main()

{
int year, month;

printf("Enter year and month:");
scanf("&#37;d", &year);
if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))
{
printf("has three hundred and sixty six (366) days\n");}
else{
printf("has three hundred and sixty five (365) days\n");}

scanf("%d", &month);
if (month==1);
printf("Janurary has 31 days");}
else if (month==2)
printf("Feburary has 28 days");}
else (month==3)
printf("March has 30 days");}

return 0;
}
• 03-25-2008
matsp
Does it compile?

--
Mats
• 03-25-2008
Elysia
Do not put ending brackets on the same line as other code.
Do use code tags when posting code.
Code:

```#include <stdio.h> int main() {         int year, month;         printf("Enter year and month:");         scanf("&#37;d", &year);         if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))         {                 printf("has three hundred and sixty six (366) days\n");         }         else         {                 printf("has three hundred and sixty five (365) days\n");         }         scanf("%d", &month);         if (month==1);         printf("Janurary has 31 days"); } else if (month==2) printf("Feburary has 28 days"); } else (month==3) printf("March has 30 days"); } return 0; }```
• 03-25-2008
jake123
Quote:

Originally Posted by Elysia
Do not put ending brackets on the same line as other code.
Do use code tags when posting code.

Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that, but putting them at the end of an if statement or onto the next line is just a matter of style.

Quote:

Originally Posted by bobbie18
THIS is what i got so far... is it right?

Code:

```#include <stdio.h> int main()  {     int year, month;     printf("Enter year and month:");     scanf("%d", &year);     if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0))) {         printf("has three hundred and sixty six (366) days\n");}     else{         printf("has three hundred and sixty five (365) days\n");}     scanf("%d", &month);     if (month==1);         printf("Janurary has 31 days");}     else if (month==2)     printf("Feburary has 28 days");}     else (month==3)     printf("March has 30 days");}     return 0; }```

I was under the impression that March always had 31 days.
• 03-25-2008
slingerland3g
Slight improvement on using enumerations as this makes the code slightly self defining.

Code:

``` #include <stdio.h> int main() {         int year, month;         enum months{January = 1, February, March};         printf("Enter year and month:");         scanf("%d %d", &year, &month);         if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))         {                 printf("has three hundred and sixty six (366) days\n");         }         else         {                 printf("has three hundred and sixty five (365) days\n");         }   if (month == January)     printf("Janurary has 31 days");   else if (month == February)     printf("February has 28 days");   else if (month == March)     printf("March has 30 days"); return 0; }```
• 03-25-2008
Elysia
Quote:

Originally Posted by jake123
Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that...

Which is a good thing. But other IDEs are not so nice.
• 03-25-2008
matsp
Quote:

Originally Posted by jake123
Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that, but putting them at the end of an if statement or onto the next line is just a matter of style.

You can write code any way you like from the COMPILERS standpoint. Elysia's comments are intended to make the code more readable, and for a novice that has little experience, the "it's just a matter of style" is probably not helpful. Giving strict, simple, guidance helps a whole lot more. [Shame I don't always follow my own advice].

Quote:

I was under the impression that March always had 31 days.
So was I.

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 7 1234567 Last