You are not logged in.

#1 2010-01-07 23:24:34

suggy
Member
Registered: 2008-08-12
Posts: 15

Embrassing (probably) while loop prob! ((((N00B ALERT))))

Ok so i'm trying to learn C... again but can't for the life of me get this loop to terminate! I dunno why it's probably noobishness but hell i've given up and i'm gonna post it to see if you lot can sort it! Cheers for the help!

#include <stdio.h>

int main ()
{
    int x;
    char answer;
    x = 0;
        while ( x < 2 ) {
            x++;
            printf( "I'm alive you smuck!! Be afraid be very afraid you ****!!!\n" );
            getchar();
            printf( "You running yet *****?!?!?!?!?!?!?\n Y or N?\n" );
            scanf( "%s", &answer );
        if ( answer == 'y' ) {
            printf( "You can run all you like ******! Yo momma can't anymore!\n MWHAHAHAHAHAHAHAHAHAHA!!!11111123456778990\n" );
            getchar();}
        else if ( 'n' == answer ){
            printf( "Your ***** are writting cheques your *** can't cash..... bitch\n" );
            getchar();}
        else {
            printf( "Can you speak english mother *******?\n" );
            getchar();
    };
    };

    return 0;
}

PS: Please excuse the childish 'humor'!

&&

Cheers all!

Offline

#2 2010-01-07 23:46:30

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

why so many extraneous calls to getchar()? also, since you're using scanf for a string (%s), you should provide a buffer rather than just one character. Like this:

char buffer[4096];
scanf("%s\n", &buffer)

otherwise you should do this:

char c;
scanf("%c\n", &c)

at least i think so anyway, i've never really used scanf() before

Last edited by PirateJonno (2010-01-07 23:49:27)


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#3 2010-01-07 23:49:43

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Because you are supposed to use %c for a single character and %s for a string. Since the answer variable is next to x, you are overflowing answer and x gets overwritten.

Your code style is absolutely atrocious by the way.

Offline

#4 2010-01-08 00:01:24

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

PirateJonno wrote:

why so many extraneous calls to getchar()?

I would guess to make the code pause to monitor the flow. This is a typical beginner's method. It is quite funny though that getchar was used everywhere except where it was needed. tongue

caelestis wrote:

Your code style is absolutely atrocious by the way.

Instead of criticizing a beginner for his (non-existent/undeveloped) coding style, perhaps it would be more helpful to give some tips, e.g. indentation, etc.





I don't know C beyond a few basic concepts and what translates from other languages... is there any reason not to use "answer = getchar();"? One page I found claims that it is more efficient than using scanf.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2010-01-08 00:07:56

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Xyne wrote:

I don't know C beyond a few basic concepts and what translates from other languages... is there any reason not to use "answer = getchar();"? One page I found claims that it is more efficient than using scanf.

yeah I'd agree with that as well, I can't for the life of me figure out how to get scanf to work


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#6 2010-01-08 00:18:56

suggy
Member
Registered: 2008-08-12
Posts: 15

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Wow I'm gettin overflows already............. lol!

Ok i think i know where i'm going wrong now guys! Cheers for all the super quick help!

And whats wrong with my coding style? What should/could i do different? Thank you!

O and i was sorta following this website http://www.cprogramming.com! Can you lot recommend any better ones? Ta!

Last edited by suggy (2010-01-08 00:21:04)

Offline

#7 2010-01-08 00:23:54

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Why your coding style looks bad:

[*]You don't place brackets properly. Why are some below and some next to the line? Choose one and stick with it. Theres also some with spaces before and some not.
[*] No one ever puts a ; after a bracket except for classes and structures.
[*]You should make a new indentation level for every new block. in your while loop, the ifs should be on the same indentation with the statements above it and the statements inside the ifs should be indented another level.


More things to do:

[*]Go learn all the specifiers for scanf, and you should use the right one for the right type of variable
[*]getchar() gets a character from the input and returns it, why would you use a getchar for the job of pausing input but not for getting the answer?
[*]Use a for loop, maybe you haven't learned it yet:

int x;
for (x = 0; x < 2; ++x)  {
    code
}

edit: Why doesn't the list bbcode work? I will keep the asterisks though.

Last edited by caelestis (2010-01-08 00:25:27)

Offline

#8 2010-01-08 00:27:12

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

suggy wrote:

And whats wrong with my coding style? What should/could i do different? Thank you!

Indent your code to reflect structure. It makes it easier to mentally parse blocks of code, etc.
Also, when debugging that particular piece of code, I would add the value of x to the output to track it through each loop.

E.g.

#include <stdio.h>

int main ()
{
  int x;
  char answer;
  x = 0;
  while ( x < 2 ) {
    x++;
    printf( "I'm alive you smuck!! Be afraid be very afraid you ****!!!\nOh, and x is %d\n", x );
    getchar();
    printf( "You running yet *****?!?!?!?!?!?!?\n Y or N?\n" );
    answer = getchar();
    if ( answer == 'y' )
    {
      printf( "You can run all you like ******! Yo momma can't anymore!\n MWHAHAHAHAHAHAHAHAHAHA!!!11111123456778990\n" );
      getchar();
    }
    else if ( 'n' == answer )
    {
      printf( "Your ***** are writting cheques your *** can't cash..... bitch\n" );
      getchar();
    }
    else
    {
      printf( "Can you speak english mother *******?\n" );
      getchar();
    }
  }

  return 0;
}

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2010-01-08 00:30:19

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

suggy wrote:

O and i was sorta following this website http://www.cprogramming.com! Can you lot recommend any better ones? Ta!

http://en.wikibooks.org/wiki/C_programming
In that book, you don't have to read bitwise things or remember every function name (just get an IDE). If you find anything which you don't find useful skip it and return to it later when you do find a use.

or if you would like to buy a book, I'd recommend:

C primer plus

Last edited by caelestis (2010-01-08 00:33:49)

Offline

#10 2010-01-08 01:26:40

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

I haven't read C Primer Plus (although I've heard it's good), but I always recommend The C Programming Language, a.k.a. "K&R", "the white bible", and other monikers.  In your case, however, I'm going to suggest learning something else first, and learning to write good "generic" code before trying to learn C.  Python and Perl are easier to learn and have better guides for inexperienced programmers.  (I'm assuming that you don't have much programming experience based on your inconsistent coding style and the simplicity of your program.)

There's no need to be apologetic when asking a question -- even a really, really stupid one.  We've all been newbies before and most of us are sympathetic.  You don't seem to be familiar with Eric Raymond's essay "Ask Questions the Smart Way", and I hope you won't take it as a stinging indictment if I suggest you read it.

Oh yes, there's one minor problem that hasn't yet been mentioned:  getchar() returns an int, not a char (it has to be able to return EOF, which won't fit into a char).  Not the biggest problem in your code, but I mention it for the sake of the future reader.

Offline

#11 2010-01-08 05:57:54

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,804

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

I have not seen it it years, but how about "The C Puzzle Book" [Alan R Feuer].

It is not an example of good style (The author even stressed that point), but it was the tool that finally beat the concept of pointers into my head (1).  I bring this up only because the root of your problem was the mis-use of a pointer.

(1) Especially the problem entitled Pointer Stew


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#12 2010-01-08 21:07:14

suggy
Member
Registered: 2008-08-12
Posts: 15

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Cheers all!

Think i'm going to take the advice of learning perl or python first! Can anyone recommend good websites and/or books (prefer websites tho tbh- hey christmas was expensive, hehe)? Thanks!

Offline

#13 2010-01-08 23:53:38

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Sure, I just started python too. See: http://bbs.archlinux.org/viewtopic.php?id=87973

I just learned from the official tutorial on the website. I don't think there's any better tutorials.

http://docs.python.org/tutorial/

Hello world jumpstart:
pacman -S python

Save this as hello.py

#! /usr/bin/env python

print "Hello World!"

in the terminal where hello.py is
chmod +x *.py

then to run:
./hello.py

Edit: Oh and by the way, theres no brackets in python, you just have to indent blocks of code properly. It will be good for you and others though.

Last edited by caelestis (2010-01-08 23:57:26)

Offline

#14 2010-01-09 00:14:19

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

Or here's a fun tutorial:
http://inventwithpython.com/chapters/

Offline

#15 2010-01-09 03:21:22

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Embrassing (probably) while loop prob! ((((N00B ALERT))))

The online Python tutorials are pretty good.  I'm not aware of anything quite the same for Perl, but I would recommend starting with Python anyway.

If you want to buy a print book (which I always think is a good idea, when you can afford it), I can recommend anything by O'Reilly.  I've heard good things about their recent Head First Programming title, which uses Python to teach programming.  Learning Python is also good, but perhaps not as newbie-friendly as Learning Perl (if you decide to go the Perl route).

Offline

Board footer

Powered by FluxBB