Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

kathy025
Guest
Hi guys,

I've been fiddling with JS for a while, although I understand a few things through trial and error and general experience from years of ProBoarding, I'm far from being code savvy.

Yesterday, my InfoTech101 class officially entered the programming world of C. Yay! Forgive me my shallow-ness guys haha.

Here is one of the exercise code we were given:
#include <stdio.h>
#include <stdlib.h>
main ()
{
int yearBorn, yearNow, ageNow;
yearNow = 2012;


printf ("Enter the year you were born: ");
scanf ("%d", &yearBorn);
ageNow = yearNow - yearBorn;
printf ("Your age is %d\n", ageNow, yearBorn);

system("pause");
}

Can someone kindly explain the red line? Particularly why was it needed to put both ageNow, yearBorn? I tried removing yearBorn and the program still ran.

I know this is really basic for you guys, I am but a humble beginner. :-[

Thanks!

~Memzak~

~Memzak~ Avatar
Inquire never, so always need elephants.

****
Senior Member

408


May 2009
printf ("Your age is %d\n", ageNow, yearBorn);


I would suspect the reason behind it is that you have only one %d and two ints in the printf.
I'd make it this:

#include <stdio.h>
#include <stdlib.h>
main ()
{
int yearBorn, yearNow, ageNow;
yearNow = 2012;


printf ("Enter the year you were born: ");
scanf ("%d", &yearBorn);
ageNow = yearNow - yearBorn;
printf ("Your age is %d\n", ageNow);

system("pause");
}

If the input was 2000, the output would be, "Your age is 12"

...since I'm assuming you want to calculate how old the person is, there's no need to print out the year they were born in again. (aside from the fact it was giving errors anyways...)

If you DO want both ints output change
#include <stdio.h>
#include <stdlib.h>
main ()
{
int yearBorn, yearNow, ageNow;
yearNow = 2012;


printf ("Enter the year you were born: ");
scanf ("%d", &yearBorn);
ageNow = yearNow - yearBorn;
printf ("Your age is %d and  you were born in %d\n", ageNow, yearBorn);

system("pause");
}

If input was 2000, the output would be, "Your age is 12 and you were born in 2000"

EDIT:I feel like I should mention this is all based on my C++ knowledge and not what I know have C... but as far as I know printf was something from C that remained the same in C++.


Last Edit: Jan 6, 2013 20:03:49 GMT by ~Memzak~




newBookmarkLockedFalling