Like Tree2Likes
  • 2 Post By Adak

Simple printf and for loop help

This is a discussion on Simple printf and for loop help within the C Programming forums, part of the General Programming Boards category; A while back I wrote a program to annoy my programming teacher that would open a terminal window and sing ...

  1. #1
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80

    Simple printf and for loop help

    A while back I wrote a program to annoy my programming teacher that would open a terminal window and sing the "99 bottles of 'coke' on the wall" song. I wrote that in C++, which was a simple enough process, but now that I'm writing it in C, I get a few errors. I'm really new to C and I promise not to annoy anyone else after this without really trying to look around. This should be a very simple fix for anyone with experience, so I'll leave you guys to it.

    Source of beer.c
    Code:
    #include<stdio.h>
    int main() {
    int i=0;
    for (i=0;i!=100;i--) {
        int j = i-1;
        printf(i," bottles of beer on the wall, ",i,"bottles of beer take one down pass it around, ", j," bottles of beer on the wall!\n");
        }
    return 0;
    }
    Compiler error output
    Code:
    beer.c: In function ‘main’:
    beer.c:6: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
    /usr/include/stdio.h:339: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
    I would like to thank you guys ahead of time for helping me with this and I look forward to a long mutually beneficial relationship.

    Edit: The printf() line is pretty long, in C++ I could just << and start on the next line, it would be helpful if there were a way to do that here as well, I think it's a '\' but I wasn't willing to chance it.

    Also, I'm using nano/gedit running Ubuntu 10.04 if that matters at all.
    Last edited by andrew89; 12-22-2011 at 10:04 PM. Reason: Additional information

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Did you look up the documentation for printf()? What should the first argument be? What is your first argument?
    Your printf should look something like:
    Code:
    int num = 10;
    printf("%d %s\n", num, "Print this string");
    Jim

  3. #3
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Looked at it, kind of understand it. Did a bit of editing and I've got some of it to work.
    Code:
    #include<stdio.h>
    int main() {
    int i=0;
    for (i=99;i!=0;i--) {
        int j = i-1;
        printf("%d %s\n",i," bottles of beer on the wall, ",i,"bottles of beer take one down pass it around, ", j," bottles of beer on the wall!\n");
        }
    return 0;
    }
    Output:
    Code:
    99  bottles of beer on the wall, 
    98  bottles of beer on the wall, 
    97  bottles of beer on the wall, 
    96  bottles of beer on the wall, 
    95  bottles of beer on the wall, 
    94  bottles of beer on the wall, 
    ...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jimblumberg View Post
    Did you look up the documentation for printf()? What should the first argument be? What is your first argument?
    Your printf should look something like:
    Code:
    int num = 10;
    printf("%d %s\n", num, "Print this string");
    Jim
    Sorry Jim, that can't be right - everyone knows this is a song, and your call to printf() doesn't have a single musical note in it!



    Merry Christmas!

    Andrew:

    Code:
    for(bottleNum=99;bottleNum!=0;bottleNum--) {
        printf("%d bottles of beer on the wall,\n%d bottles of beer,\n",bottleNum,bottleNum);
        printf("You take one down, and pass it around, %d bottles of beer on the wall\n"bottleNum-1);
    }
    Last edited by Adak; 12-22-2011 at 10:53 PM.
    andrew89 and jimblumberg like this.

  5. #5
    Registered User fdrake's Avatar
    Join Date
    Dec 2011
    Posts
    6
    Quote Originally Posted by Adak View Post
    Sorry Jim, that can't be right - everyone knows this is a song, and your call to printf() doesn't have a single musical note in it!



    Merry Christmas!

    Andrew:

    Code:
    for(bottleNum=99;bottleNum!=0;bottleNum--) {
        printf("%d bottles of beer on the wall,\n%d bottles of beer,\n",bottleNum,bottleNum);
        printf("You take one down, and pass it around, %d bottles of beer on the wall\n"bottleNum-1);
    }
    probably by copy and pasting you lost the format of the code(spaces). Also don't forget the comma after the string sentence.

    Code:
    #include <stdio.h>
    main() {
    int bottleNum=0;
    for(bottleNum=99; bottleNum!=0; bottleNum--) {
        printf("%d bottles of beer on the wall, %d bottles of beer.\n", bottleNum, bottleNum);
        printf("You take one down, and pass it around, %d bottles of beer on the wall! \n", bottleNum-1);
    }
    return 0;
    }
    99 bottles of beer on the wall, 99 bottles of beer.
    You take one down, and pass it around, 98 bottles of beer on the wall!
    98 bottles of beer on the wall, 98 bottles of beer.
    You take one down, and pass it around, 97 bottles of beer on the wall!.......
    cheers..

  6. #6
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80

    Yay!

    Here's the source:
    Code:
    #include<stdio.h>
    int main() {
    int i=99;
    for (i;i!=2;i--) {
        printf("\n\n%d bottles of beer on the wall, %d bottles of beer.",i,i);
        printf("\nYou take one down, and pass it around, %d bottles of beer on the wall!",i-1);
    }
    //  Number of bottles of beer on the wall is too low to be grammatically
    //  correct, last two lines (verses?) will be printed the lazy way.
    printf("\n\n2 bottles of beer on the wall, 2 bottles of beer.");
    printf("\nYou take one down, and pass it around, 1 bottle of beer on the wall!");
    
    printf("\n\n1 bottle of beer on the wall, 1 bottle of beer.");
    printf("\nYou take one down, and pass it around, no more bottles of beer on the wall!\n\n");
    
    return 0;
    }
    Last 3 'lines' of output:
    Code:
    3 bottles of beer on the wall, 3 bottles of beer.
    You take one down, and pass it around, 2 bottles of beer on the wall!
    
    2 bottles of beer on the wall, 2 bottles of beer.
    You take one down, and pass it around, 1 bottle of beer on the wall!
    
    1 bottle of beer on the wall, 1 bottle of beer.
    You take one down, and pass it around, no more bottles of beer on the wall!
    
    ?@?~/c/boredom$ _
    Thank you all for your help, I hope this leaves you all feeling accomplished... or at least a little tipsy :P

    I seriously just started messing around with C two days ago, last night being the first try at doing anything beyond printing strings. Now that I understand the variable printing system a little better after reading a OUCS (Oxford University Computer Services) 'primer' after a google search, C is a little easier for me

    Just don't be expecting me to program an o/s anytime soon. Cheers all.

    Edit: Next on the menu is file manipulation (input/output etc.). It's gonna be fun!!!
    Last edited by andrew89; 12-23-2011 at 04:10 AM. Reason: Just wanted to let everyone know what I'll be up to next :)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very simple Function using printf()
    By twazzler in forum C Programming
    Replies: 17
    Last Post: 04-14-2011, 05:04 PM
  2. fscanf printf while loop help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 02:10 AM
  3. Very simple printf
    By GokhanK in forum C Programming
    Replies: 3
    Last Post: 02-18-2011, 07:09 AM
  4. Simple Scanf and printf
    By xamlit in forum C Programming
    Replies: 9
    Last Post: 08-31-2005, 05:20 PM
  5. newbie - while loop and printf
    By nickch in forum C Programming
    Replies: 7
    Last Post: 04-14-2005, 02:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21