[Courses] C Programming For Absolute Beginners, Lesson 3: Else If, While, Operators

Carla Schroder carla at bratgrrl.com
Mon Mar 5 21:45:00 UTC 2012


You are so right-- you can never have too many books :)

You might add line break or space after "Please enter how many books you 
have."

I got bit again by email inserting hard line breaks, so when you copy a 
program from email and get a wad of error messages check your line breaks. A 
nice syntax-highlighting text editor will show you right where this is, for 
example in Kate the first part of the line is colored, and then the text after 
the bogus line break is not colored.

Carla

On Thursday, March 01, 2012 07:55:23 AM Emily Gonyer wrote:
> //books you have
> 
> #include <stdio.h>
> 
> int main()
> {
> int book=0;
> int book2=0;
> int book3=0;
> 
> printf( "Please enter how many books you have.");
> scanf( "%d", &book );
> printf( "So, you have %d books.", book);
> 
> while ( book < 10 ){
> printf( "Books are good! But is that all you have? Please enter how many
> books you have.\n" );
> scanf( "%d", &book );
> printf( "OK, so you have %d books.", book);
> }
> if ( book > 50 ){
> printf( "Awesome! I love books too!\n" );
> }
> else if ( book > 11 ){
> printf( "Books are good! You can never have too many you know!\n" );
> }
> return 0;
> }
> 
> Hey there, after much playing this is what I've come up with :)
> 
> Emily
> 
> On Tue, Feb 28, 2012 at 1:51 PM, Carla Schroder <carla at bratgrrl.com> wrote:
> > Now that was fun! Because the first few times I tried to compile this it
> > freaked out with lots of errors. I finally figured out that copying it
> > from email inserted a couple of line breaks where there should not have
> > been line
> > breaks. Got rid of those and it ran perfectly. Thanks Leslie!
> > 
> > Carla
> > 
> > On Monday, February 27, 2012 08:25:17 PM Leslie wrote:
> > > On Mon, 2012-02-27 at 14:21 -0800, Carla Schroder wrote:
> > > > Write your own 'howold' variation with three different
> > > > conditions: how many socks in your drawer, it's your party and
> > > > you will...what?, what happens when the price of gasoline hits
> > > > various levels...it can be anything you want.
> > > 
> > > I decided to put the if, else-if, and else inside the while loop:
> > > 
> > > //party.c
> > > #include <stdio.h>
> > > 
> > > int main()
> > > {
> > > 
> > >       int guests=0;
> > >       printf( "Of course you can have your party at my house!
> > >       " );
> > >       printf(  "How many people were you thinking of asking?\n
> > >       " );
> > >       scanf ("%d", &guests);
> > >       while (guests > 12) {
> > >       
> > >               if (guests > 20){
> > >               
> > >                 printf( "Wow, that's a lot. Can you
> > >                 whittle it down?\n");> >               
> > >               }
> > >               else if (guests > 15) {
> > >               
> > >                 printf( "It will be a little tight
> > >                 but I know I'll like
> > 
> > your
> > 
> > > friends.\n" );
> > > 
> > >               }
> > >               else {
> > >               
> > >                 printf( "I don't know if we can
> > >                 quite squeeze everyone
> > 
> > in.\n" );
> > 
> > >               }
> > >               printf ( "How about just your closest
> > >               friends? How many
> > 
> > would that
> > 
> > > be?\n" );
> > > 
> > >               scanf ("%d", &guests );
> > >       
> > >       }
> > >       printf("No problem!  You know I'd do anything for
> > >       you!\n\n");
> > >       return 0;
> > > 
> > > }
> > > 
> > > Thanks for this course!
> > > 
> > > -Leslie
> > > 
> > > _______________________________________________
> > > Courses mailing list
> > > Courses at linuxchix.org
> > > http://mailman.linuxchix.org/mailman/listinfo/courses
> > 
> > --
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > Carla Schroder
> > ace Linux nerd
> > author of Linux Cookbook,
> > Linux Networking Cookbook,
> > Book of Audacity
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > _______________________________________________
> > Courses mailing list
> > Courses at linuxchix.org
> > http://mailman.linuxchix.org/mailman/listinfo/courses
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
ace Linux nerd
author of Linux Cookbook,
Linux Networking Cookbook,
Book of Audacity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list