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

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 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.

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