I am currently doing exercices on Kattis and I meet a problem at the time of recovering the character of scanf...
I manage to recover my characters before my conditions but not during these latter...
Here is my code with comments to target my problems
#include <stdio.h>
void bela(int m, char c)
{
char card[2];
int point = 0;
printf("0 = %c\n",c); // Here c = my enter of scanf
for(int i = 0; i != m * 4; i += 1) {
scanf("%s", card);
for(int i = 0; card[i] != '\0'; i += 1) {
if(card[i] == 'A') {
point = point + 11;
printf("A = %i\n", point);
}
if(card[i] == 'K') {
point = point + 4;
printf("K = %i\n", point);
}
if(card[i] == 'Q') {
point = point + 3;
printf("Q = %i\n", point);
}
printf("1 = %c\n",c); // Here c = nothing instead of the value of c
if(card[i] == 'J' && card[i + 1] == c) { // Here c can not to be compared since he's equal to nothing...
point = point + 20;
printf("%c", card[i + 1]);// Here card[i + 1] = nothing instead of the value of c
}
else if(card[i] == 'J' && card[i + 1] != c)
point = point + 2;
if(card[i] == 'T') {
point = point + 10;
printf("T = %i\n", point);
}
if(card[i] == '9' && card[i + 1] == c) {
point = point + 14;
printf("9 + S %i\n", point);
}
}
}
printf("%i\n", point);
}
int main(void)
{
int m = 0;
char c = 0;
scanf("%i %c", &m, &c);
bela(m, c);
return (0);
}
Do you have any ideas for solve the problems ?
Thanks for your help.
PtiCassarin
Related
**
Hello :)
So I have this task to write a program that will convert int to arg using malloc and well it all works fine, gdb shows no errors, but it is not printing any output in this form. If i delete itoa_buffer[i] = '\0'; than sometimes it shows output sometimes not. I do not know what is wrong here, seems fine.
I do not know where to ask for help as I like to follow my logic and find errors here instead of copying solutions from the internet. I willl appreciate some tips, its probably some small thing Id do not know and wont let me go further.**
#include<stdio.h>
#include "libft.h"
char *ft_itoa(int n)
{
int i;
int z;
char x;
char *itoa_buffer;
if (n == INT_MIN)
return(ft_strdup("-2147483648"));
if (n < 0)
{
n = -n;
z = n;
i = 1;
}
else
{
z = n;
i = 0;
}
while(z > 0)
{
z = z/10;
i++;
}
if(!(itoa_buffer = (char *)malloc((i+1)*sizeof(char))))
return(0);
i = i + 1;
while(--i)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 2)
{
i--;
itoa_buffer[i] = '-';
i--;
break;
}
}
itoa_buffer[i] = '\0'; // it stopped showing answers when i tried to add this symbol at the end.
return(itoa_buffer);
}
int main()
{
int n;
n = 1980;
printf("%s", ft_itoa(n));
}
You are putting '\0' as the first character of the string.
Instead of that, you should put that as the last character.
Instead of this part
i = i + 1;
while(--i)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 2)
{
i--;
itoa_buffer[i] = '-';
i--;
break;
}
}
itoa_buffer[i] = '\0';
you should do this
itoa_buffer[i] = '\0';
while(i--)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 1)
{
i--;
itoa_buffer[i] = '-';
break;
}
}
I am coding in C in a university course and we got a project to take equations from the user and give solutions for matrices etc...
My problem is that I am trying to use atof() function and for a reason I can't find in the same loop once it works and the other times it doesn't.
I have tried already other functions to replace atof like strtod but it doesn't work as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdbool>
void main()
{
int num, check = 0,i,j,k=0,len1=0;
char equ[80],tempx[20],tempy[20], tempz[20], tempd[20];
double *x, *y, *z, *d;
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
getchar();
while (check == 0) //a check to see if we got a number between 1-3.
{
if (num > 0 && num < 4)
check = 1;
else
{
printf_s("Please enter a number between 1-3.\n");
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
}
}
x = malloc(sizeof(double)*num);
if (!x) exit(1);
y = malloc(sizeof(double)*num);
if (!y) exit(1);
z = malloc(sizeof(double)*num);
if (!z) exit(1);
d = malloc(sizeof(double)*num);
if (!d) exit(1);
for (i = 0; i < num; i++) //getting the equations and putting them into the matrix
{
printf_s("Enter equation %d: ", i + 1);
gets_s(equ, sizeof(equ));
len1 = strlen(equ);
for (j = 0; j <len1 ; j++)
{
if (equ[j] == 'x')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j-k>=0)
{
tempx[j-k] = equ[j-k];
k++;
}
x[i] = atof(tempx);
}
else if (equ[j] == 'y')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j - k >= 0)
{
tempy[j-k] = equ[j-k];
k++;
}
y[i] = atof(tempy);
}
else if (equ[j] == 'z')
{
k = 0;
while ((equ[j - k] != '+' || equ[j - k] != '-') && j - k >= 0)
{
tempz[j-k] = equ[j - k];
k++;
}
z[i] = atof(tempz);
}
else if (equ[j] == '=')
{
k = 0;
while (equ[j+k])
{
tempd[k] = equ[j + k];
k++;
}
d[i] = atof(tempd);
}
}
}
free(x);
free(y);
free(z);
free(d);
}
I expected to get the same result in d[i] as I did in x[i] but every time I try to print d[i] I get 0.0000. When I tried the function _strrev on tempd inside atof I got the reverse result inside d[i].
So the problem was that in the last loop i inserted a string that start with "=" and not a number. Apparently atof() doesn't work when the first char is not a number.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
First time posting a question on stack overflow so be nice.
I'm trying to write a program for school. This program is suppose to take a data set and turn it into a maze. The error I'm getting is a segmentation fault in putty but not in the IDE I'm using. Not sure what to do or how to handle it. I tried putting printf statements everywhere but none of them really show up doesnt make sense. Maybe because the functions themselves cause the fault not sure though what part.
//CODE BEGINS****************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define FALSE 0
#define TRUE 1
typedef struct mazeStruct
{
char **arr; /* allows for a dynamic 2-D maze of any size */
int xsize, ysize;
int xstart, ystart;
int xend, yend;
bool end;
} maze;
struct linkedStruct
{
int x;
int y;
bool Unvisited;
struct linkedStruct* next;
};
typedef struct linkedStruct linked;
typedef linked* linkedPtr;
void push(linkedPtr* hd, int Xval, int Yval)
{
linkedPtr ptr = (linkedPtr) malloc(sizeof(linked));
ptr->x = Xval;
ptr->y = Yval;
ptr->Unvisited = FALSE;
ptr->next = *hd;
*hd = ptr;
}
int isEmpty(linkedPtr hd)
{
if (hd == NULL)
return TRUE;
else
return FALSE;
}
int top(linkedPtr hd)
{
return (hd->x && hd->y);
}
void pop(linkedPtr* hd)
{
linkedPtr ptr = (linkedPtr) malloc(sizeof(linked));
ptr->x = NULL;
ptr->y = NULL;
ptr->Unvisited = TRUE;
ptr->next = *hd;
*hd = ptr;
free(ptr);
}
int main(int argc, char **argv)
{
maze m1;
linkedPtr head = NULL;
int xpos, ypos;
int i, j;
m1.end = FALSE;
FILE *src;
//FILE *src = fopen ("mazeData1.txt",'r');
/* verify the proper number of command line arguments were given */
if (argc != 2)
{
printf("Usage: %s <input file name>\n", argv[0]);
exit(-1);
}
/* Try to open the input file. */
if ((src = fopen(argv[1], "r")) == NULL)
{
printf("Can't open input file: %s", argv[1]);
printf("Standard Error.\n");
exit(-1);
}
/* read in the size, starting and ending positions in the maze */
fscanf(src, "%d %d", &m1.xsize, &m1.ysize);
if (m1.xsize < 1 || m1.ysize < 1)
{
printf("Size has to be 1 or above.\n");
fscanf(src, "%d %d", &m1.xsize, &m1.ysize);
}
fscanf(src, "%d %d", &m1.xstart, &m1.ystart);
if (m1.xstart > m1.xsize || m1.ystart > m1.ysize || m1.xstart < 1
|| m1.ystart < 1)
{
printf("The start has to be within the maze.\n");
fscanf(src, "%d %d", &m1.xstart, &m1.ystart);
}
fscanf(src, "%d %d", &m1.xend, &m1.yend);
if (m1.xend > m1.xsize || m1.yend > m1.ysize || m1.xend < 1 || m1.yend < 1)
{
printf("The end has to be within the maze.\n");
fscanf(src, "%d %d", &m1.xend, &m1.yend);
}
if (m1.xend == NULL || m1.yend == NULL)
{
printf("Error: Need at least three lines of input");
exit(-1);
}
/* print them out to verify the input */
printf("size: %d, %d\n", m1.xsize, m1.ysize);
printf("start: %d, %d\n", m1.xstart, m1.ystart);
printf("end: %d, %d\n", m1.xend, m1.yend);
/* allocate the maze */
m1.arr = (char **) malloc(sizeof(char *) * (m1.xsize + 2));
for (i = 0; i < m1.xsize + 2; i++)
m1.arr[i] = (char *) malloc(sizeof(char) * (m1.ysize + 2));
/* initialize the maze to empty */
for (i = 0; i < m1.xsize + 2; i++)
for (j = 0; j < m1.ysize + 2; j++)
m1.arr[i][j] = '.';
/* mark the borders of the maze with *'s */
for (i = 0; i < m1.xsize + 2; i++)
{
m1.arr[i][0] = '*';
m1.arr[i][m1.ysize + 1] = '*';
}
for (i = 0; i < m1.ysize + 2; i++)
{
m1.arr[0][i] = '*';
m1.arr[m1.xsize + 1][i] = '*';
}
/* mark the starting and ending positions in the maze */
m1.arr[m1.xstart][m1.ystart] = 's';
m1.arr[m1.xend][m1.yend] = 'e';
/* mark the blocked positions in the maze with *'s */
while (fscanf(src, "%d %d", &xpos, &ypos) != EOF)
{
if (xpos > m1.xsize || ypos > m1.ysize || xpos < 1 || ypos < 1
|| (xpos == m1.xstart && ypos == m1.ystart)
|| (xpos == m1.xend && ypos == m1.yend))
{
printf(
"Error: X or Y is: out of range or is on the end or is on the start\n");
continue;
}
m1.arr[xpos][ypos] = '*';
}
/* print out the initial maze */
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
printf("%c", m1.arr[i][j]);
printf("\n");
}
// THE START OF THE DEPTH FIRST SEARCH METHOD
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
{
if (m1.arr[i][j] != '*')
{
head->Unvisited = FALSE;
head->next = head->next + 1; //MAYBE
}
}
}
head->x = m1.xstart;
head->y = m1.ystart;
head->Unvisited = FALSE;
while ((isEmpty(head) == FALSE) && (m1.end == FALSE))
{
if ((m1.xend == head->x) && (m1.yend == head->y))
{
printf("The END has be found!\n");
m1.end = TRUE;
}
if ((head->x + 1 && head->y) == TRUE)
{
push(&head, head->x + 1, head->y);
}
else if ((head->x - 1 && head->y) == TRUE)
{
push(&head, head->x - 1, head->y);
}
else if ((head->x && head->y + 1) == TRUE)
{
push(&head, head->x, head->y + 1);
}
else if ((head->x && head->y) == TRUE)
{
push(&head, head->x, head->y - 1);
}
else
{
pop(head);
}
}
if (isEmpty(head) == TRUE)
{
printf("Maze has no solution");
exit(0);
}
else
{
printf("%d %d", &head);
}
printf("%d", top(head));
free(m1.arr);
m1.arr = NULL;
return 1;
}
The main problem here is that you are hiding pointer with typedef:
typedef linked* linkedPtr;
In main you are declaring
linkedPtr head = NULL;
but you never allocate/mallocate space for that variable and the first piece of code that dereference it invokes Undefined Behavior because you are dereferencing a null pointer
// THE START OF THE DEPTH FIRST SEARCH METHOD
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
{
if (m1.arr[i][j] != '*')
{
head->Unvisited = FALSE; <----------BOOOOOOOOOOOOOOM-------
head->next = head->next + 1;
}
}
}
Moreover you have a type mismatch calling pop function, change
pop(head);
to
pop(&head);
I'm trying to create a code that converts a decimal into any base between 2 and 16. But I don't know how to write a new value in my string.
void base_conversion(char s[], int x, int b) {
int j, y;
j = 0;
// your code here
while(b > 1 && b < 17) {
if (x < 0) {
x = (-x);
}else if(x == 0) {
s[j] = '\0';
}
x = (x/b);
while(x > 0) {
y = (x%b);
if (y == 10) {
s[j] = 'A';
}else if(y == 11) {
s[j] = 'B';
}else if(y == 12) {
s[j] = 'C';
}else if(y == 13) {
s[j] = 'D';
}else if(y == 14) {
s[j] = 'E';
}else if(y == 15) {
s[j] = 'F';
}else{
s[j] = y;
}
}
}j = j + 1;
}
You were almost there, although several mistakes, so I have "improved" your code. The infinite loop testing the base which needed to be done once only. The while() loops weren't quite organised right - x/b being done outside the digit extraction loop. Another change I made was to use a lookup array to convert each digit to a character, which saves a lot of laborious testing. I also returned the string passed as the function value - might as well add a tad more functionality. In the case of passing a bad base value, I could have returned NULL instead of an empty string. Note also I update j in the same statements where I use it as an index, which makes the code a little more fluent.
#include <stdio.h>
char *base_conversion (char *s, int x, int b) {
char charr[] = "0123456789ABCDEF";
int i, j = 0, len, digit, neg = 0;
*s = 0; // terminate the string passed
if (b < 2 || b > 16) // check the base
return s; // return an empty string
if (x < 0) {
x = -x; // adjust for negative input
neg = 1;
}
do {
digit = x % b; // extract each l.s. digit
s[j++] = charr [digit]; // convert to character
} while (x /= b); // implicitly test for 0
if (neg) // negative input
s[j++] = '-'; // append a minus sign
s[j] = 0; // terminate the string
// reverse the string
len = j;
for (i=0; i<len/2; i++) {
digit = s[i];
s[i] = s[--j]; // pre-decrement j to next char index
s[j] = digit;
}
return s;
}
int main () {
int n;
char strig[65];
for (n=1000; n>=-1000; n-=2000) {
printf ("Binary %d: %s\n", n, base_conversion (strig, n, 2));
printf ("Ternary %d: %s\n", n, base_conversion (strig, n, 3));
printf ("Octal %d: %s\n", n, base_conversion (strig, n, 8));
printf ("Decimal %d: %s\n", n, base_conversion (strig, n, 10));
printf ("Hexadecimal %d: %s\n", n, base_conversion (strig, n, 16));
}
return 0;
}
Program output:
Binary 1000: 1111101000
Ternary 1000: 1101001
Octal 1000: 1750
Decimal 1000: 1000
Hexadecimal 1000: 3E8
Binary -1000: -1111101000
Ternary -1000: -1101001
Octal -1000: -1750
Decimal -1000: -1000
Hexadecimal -1000: -3E8
I am trying to make a reverse Polish printer which can perform the following operation-
Inputs:
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))
Outputs:
abc*+
ab+zx+*
at+bac++cd+^*
This is my code:
#include <stdio.h>
#include <string.h>
char pop(int t);
void push(int c, int t);
int main()
{
int z;
scanf("%d", &z);
char *a[100];
int i = 0;
int q = z;
while (q-- > 0)
{
char v[400];
scanf("%s", &v);
int t;
for (t = 0; t < strlen(v); t++) //loop to put the values and signs in the 2 stacks
{
if ((v[t] == '*') || (v[t] == '+') || (v[t] == '-') || (v[t] == '^'))
{
push(v[t], 2);
}
else if (v[t] == ')')
{
int y = pop(2);
push(y, 1);
}
else
{
push(v[t], 1);
}
}
int k = 0;
char c;
while ((c = pop(1)) !='\0') //loop to put elements in the array v
{
if (c != '(')
{
v[k++] = c;
}
}
v[k--] = '\0';
int m;
for (m=0; m != k; m++, k--) //for reversing the string
{
char t = v[m];
v[m] = v[k];
v[k] = t;
}
a[i++] =v;
printf("%s",a[i - 1]);
}
int p;
for (p = 0; p <z ; p++) //printing the elements
printf("%s\n",*a[p]);
return 0;
}
char ac[400];
char as[400];
int ic = 0;
int is = 0;
void push(int c,int t)
{
if (t == 1 && ic != 400)
ac[ic++] = c;
else if (t == 2 && is != 400)
as[is++] = c;
}
char pop(int t)
{
if (t == 1 && ic != 0)
return ac[--ic];
if (t == 2 && is != 0)
return as[--is];
return '\0';
}
But it is not even inputting properly and I am not able to figure out what are the mistakes in this code.Please help to figure out what are the problems.
after inputing the no of test cases i.e.int z and first line if input
it crashes
This is due to the
printf("%s\n",*a[p]);
as BLUEPIXY noticed, *a[p] is a char; but %s expects a char *, thus you need
printf("%s\n", a[p]);
and regarding v is out of scope, the crucial factor is not the scope (visibility), but the storage duration (lifetime) of v - its lifetime ends when execution of the block with which it is associated ends, and the value of a pointer a[i] to it becomes indeterminate; by changing
a[i++] =v;
to
a[i++] = strdup(v);
you can remedy that.