0

my code is as follows:

   #include<stdio.h>
   #include <string.h>
   #include <math.h>
   #include <stdlib.h>
   int string_length(char*);
   void reverse(char*);
   int main(void)
   {
      char num[256];
      printf("%c[%d;%d;%dmPlease enter a number of rows: ", 0x1B, 5, 32, 40);
      gets(num);
      //gets number

printf("%c[%dm\n", 0x1B, 0);
//Resets color

//Variables
char revnum[50], q[50] = "\0", r[50] = "\v";
int i, x, j, z, w;

//Reverses inputted character string
reverse(num);

//Takes reversed inputted character string and out puts it as an integer
for(i = 0; num[i] != '\0'; ++i) {
    j = -(48-num[i]);
    double y = i;
    x = j*pow(10, y) + x;
}

//Takes integer version of inputted character string and assigns a counter's value (counting up to our integer version) to a character string
for (z = 0; z <= x; z++) {
    sprintf(revnum, "%d", z);

    //Takes the new character string for each number up to the inputted value and prints it vertically, but no differentiating between numbers and printing them horizontally to each other
    for (w = 0; revnum[w] != '\0'; ++w) {
        strcat(q, r);
        printf("%s", q);
        strcat(q, revnum[w]);
      }
   }

  }

  //Function which reverses a character string
   void reverse(char *num)
{
int length, c;
char *begin, *end, temp;

length = string_length(num);

begin = num;
end = num;

for ( c = 0 ; c < ( length - 1 ) ; c++ )
    end++;

for ( c = 0 ; c < length/2 ; c++ )
{
    temp = *end;
    *end = *begin;
    *begin = temp;

    begin++;
    end--;
}
}

 int string_length(char *pointer)
 {
int c = 0;

while( *(pointer+c) != '\0' )
    c++;

return c;
}

the point of the program is to output all the numbers before a number inputted with the digits of each number vertical and then the numbers themselves listed horizontally.

please help!!!

6
  • 2
    Try running your program in a debugger. It will help you by telling you where the crash is, let you walk the function call stack so you can see how you ended up there, and let you examine variable to help you figure out the reason. Feb 20, 2013 at 2:28
  • There are a couple of things though: Why do you make your own string length function, when strlen is perfectly fine? And instead of looping to find the end of the string in reverse you could just add (length - 1) to the pointer. Feb 20, 2013 at 2:30
  • However, the main problem might be that you don't initialize all variables you use. If you use e.g. the option -Wall to GCC when compiling you would get a warning about this. Feb 20, 2013 at 2:32
  • 2
    You should be getting at least one compiler warnings from your compiler. If you are, fix your code until it compiles cleanly. If you aren't, find out how to increase your compiler's warning level. strcat(q, revnum[w]); passes a char as the second argument to strcat; it requires a char*. And never use the gets() function; it's inherently unsafe. Feb 20, 2013 at 2:38
  • string_length? Is there some problem with the standard C strlen() which we've all been unaware of for the last 30 years? :-)
    – paxdiablo
    Feb 20, 2013 at 2:39

1 Answer 1

0

You've made several mistakes. This code works:

#include<stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int string_length(char*);
void reverse(char*);
int main(void)
{
  char num[256];
  printf("Please enter a number of rows: ", 0x1B, 5, 32, 40);
  gets(num);
  //gets number

//Variables
char revnum[50], q[50] = "\0";
int i, x, j, z, w;

//Reverses inputted character string
reverse(num);

//Takes reversed inputted character string and out puts it as an integer
x = atoi(num);

//Takes integer version of inputted character string and assigns a counter's value (counting up to our integer version) to a character string
for (z = 0; z <= x; z++) {
sprintf(revnum, "%d", z);

//Takes the new character string for each number up to the inputted value and prints it vertically, but no differentiating between numbers and printing them horizontally to each other
for (w = 0; revnum[w] != '\0'; ++w) {
    printf("%s\v", q);
char a[2];
sprintf(a,"%c",revnum[w]);
strcat(q,a);
  }
}

}

//Function which reverses a character string
void reverse(char *num)
{
int length, c;
char *begin, *end, temp;

length = string_length(num);

begin = num;
end = num;

for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;

for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;

 begin++;
 end--;
}
}

int string_length(char *pointer)
{
int c = 0;

while( *(pointer+c) != '\0' )
c++;

return c;
}

And compile with option -fno-stack-protector to prevent stack smaching detection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.