Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Halp

Name: Anonymous 2007-11-28 5:12

Beginner Cfag here, what am I doing wrong? I keep segfaulting.

(Unused ints are there for when I write the fight function. The dumb math stuff is just there because I am a beginner and try to do every function I can as much as possible, to drum it into me.)

/* hello.c: A simple program to learn the basics of C */
#include <stdio.h>

int main()
{
  int a;
  int b;
  int a_hp;
  int b_hp;
  int a_atkpwr;
  int b_atkpwr;
  int a_acracy;
  int b_acracy;
  char skill;

  printf( "Enter an integer\n" );
  scanf( "%d", &a );
  printf( "You entered %d\nAdd 5 to this number, and enter the result.\n", a );
  b = a + 5 ;
  scanf( "%d", &a );
  if ( a == b )
    {
      printf( "Correct.\n" );
      printf( "Now divide the number by 5 (Rounded).\n" );
      b = a / 5;
      scanf( "%d", &a );
      if ( a == b )
    {
      printf( "Correct. Now, let's play a game.\n" );
      printf( "Would you like your character to be strength or skill-based? Enter ``str'', without quotes, for strngth, and ``ski'', without quotes, for skill.\n" );
      printf( "Skill-based players have more health, but strength-based players have more attack.\n");
      printf( "Both are useful with different types of attack.\n" );
      scanf( "%s", &skill );
      if ( strcmp( skill, "str" ) == 0 )
        {
          a_atkpwr = 10;
          printf( "Your character is STRONG.\n" );
        }
      else if ( strcmp( skill, "ski" ) == 0 )
        {
          a_atkpwr = 5;
          printf( "Your character is SKILLFUL.\n" );
        }
      else
        {
          printf( "Please re-read the instructions and try again.\n" );
        }
    }
      else
    {
      printf( "No. Now you may not play the game.\n" );
    }
    }
  else
    {
      printf( "Incorrect.\n" );
    }
  getchar();
  return 0;
}

Name: Anonymous 2007-11-28 5:16

char skill;

That's your problem

Name: Anonymous 2007-11-28 5:22

>>2
Plz to be elaborating :(

Name: Anonymous 2007-11-28 5:30

Name: Anonymous 2007-11-28 5:42

>>4
Awesome. Thanks. Is this the first time /prog/ has helped someone ever?

Name: Anonymous 2007-11-28 5:48

>>5
/prog/ has odd periods of being helpful, now and again. This week has been surprisingly good.

Name: Anonymous 2007-11-28 6:02

>>5
You might want to investigate the string class in C++ too. It automatically allocates memory so you don't need to concern yourself with how large your string will be in advance. Note that you can't use it directly with sscanf, but instead you would  likely use C++ iostreams to read from the console.

Name: Anonymous 2007-11-28 6:09

>>7

Fuck off C++ fag.

Name: Anonymous 2007-11-28 7:41

>>1

As a general rule, when you require the user choose between a small, exclusive list of options, you should provide them with a menu instead of requiring they type in an error-prone string.  E.g., what does your program do when they type in "SKI" or "STR" or "1922" or "strength"?

I know you admitted to being a beginner, so now's the time for you to start worrying about input validation.

Name: Anonymous 2007-11-28 8:29

You know, every time your program segfaults, you start your local gdb and find out where, why, and when it crashes. Then you fix the problems. Easy, isn't it?

Name: Anonymous 2007-11-28 9:20

protip: gdb

Name: Anonymous 2007-11-28 9:40

>>10
Or if you use a decent IDE, it'll have debugging built in which should be even better than gdb and its arcane, confusing commands.

Name: Anonymous 2007-11-28 10:02

>>10,12
Or you could spend some time actually learning the language and don't depend on system-specific output assembly of a system-specific compiler that might be buggy anyways.

fucking gdb/ide idiots.
You only need a text editor to code.

Name: Anonymous 2007-11-28 10:26

Fatal error! Message could not be posted.

Please post threads less often!

Name: Anonymous 2007-11-28 10:33

>>13 has never worked on a program more complicated than Hello World.

Name: Anonymous 2007-11-28 10:34

>>13
I use Emacs.

Name: Anonymous 2007-11-28 11:38

>>15
A debugger cant tell you shit about your C/lisp/haskell/whatever program.
A debugger can only give you details about your assembler program.

Name: Anonymous 2007-11-28 11:49

>>17
Are you trolling, or have you simply never used a modern debugger before?

Name: Anonymous 2007-11-28 12:19

OP here, project is going OK.

http://www.cprogramming.com/tutorial.html#ctutorial is awesome.

Name: Anonymous 2007-11-28 12:28

>>18
Bitches don't know 'bout my Squeak.

Name: Anonymous 2007-11-28 12:30

>>18
I am not trolling, debuggers suck.

Name: Anonymous 2007-11-28 12:42

>>21
Debuggers can show and allow you to step through and breakpoint the original source code, if you have it available. It's very useful indeed.

Name: Anonymous 2007-11-28 21:27

>>13 has never had to debug a module without the source code before.

Name: Anonymous 2007-11-28 21:54

>>22
Surely debuggers also allow you to inspect values stored in the system, inspect the call stack and the arguments that were given to each function, fix the code and continue from the problem point, and so on.

Name: Anonymous 2007-11-28 21:55

>>17
A debugger can only give you details about your assembler program.
Ah, I see. Damn, trolls on /prog/ still got it.

Name: Anonymous 2007-11-28 22:57

>>24
I'm not sure what you're getting at, but it can. What's your point? Are  you >>17?

Name: Anonymous 2007-11-28 23:00

i love /prog/

Name: Anonymous 2007-11-29 0:27

>>8
/prog/ couldnt even try to stay helpful after 7 posts

FUCK and I downloaded emacs

Name: Anonymous 2007-11-29 0:45

>>26
I'm getting at >>22's omission.

Name: Anonymous 2007-11-29 1:02

-g, motherfuckers.

Name: Anonymous 2009-03-06 9:38


From the standpoint in   the U S   A Today 18.

Name: Anonymous 2010-11-13 18:15

Name: Anonymous 2010-12-10 14:50

Name: Anonymous 2013-01-19 23:38

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List