Hi,
I need to get a sentence from a user and I have problem with that.
scanf - not works because I need to get the spaces.
gets - not secure and for some reason not working, it just skips the part where i need to write the sentence.
fgets - don't even write: "Enter the argument: " and getting stuck.
Line 17 - please help
Code:
#include <stdio.h>
#include <stdlib.h>
#include "Stack_point.h"
// print_table
void print_table() {
char str[50];
stack s1;
stack s2;
int i = 0;
char poped;
create_stack(&s1);
create_stack(&s2);
printf("Enter the argument: ");
fgets(str,50,stdin);
while(str[i] != '\0'){
if(str[i] == 32){
i++;
}
else if((str[i] > 64) && (str[i] < 91) || (str[i] > 47) && (str[i] < 58)){
push(str[i], &s1);
i++;
}
}
}
void first_main(){
print_table();
}
void second_main(){
printf("Second Main\n");
}
int main(){
int taskNumber;
while(1){
printf("Please enter task number(1-2, 0 to exit): ");
scanf("%d",&taskNumber);
switch(taskNumber){
case (0):
printf("\nGood bye\n");
return 0;
case(1):
first_main();
printf("\n");
break;
case(2):
second_main();
printf("\n");
break;
default:
printf("\nError. Invalid task.\n");
}
}
}
stack_point.h :
Code:
typedef char stack_type;
typedef struct stack_element
{
stack_type data;
struct stack_element *link;
} stack_element;
typedef stack_element *stack;
void create_stack (stack *);
int push (stack_type, stack *);
int pop (stack *, stack_type *);
int stack_top (stack *, stack_type *);
int stack_is_empty (stack *);
int stack_is_full (stack *);
stack_pont.c :
Code:
#include <stdlib.h>
#include "Stack_point.h"
void create_stack (stack *ps)
{
*ps = NULL;
}
int push (stack_type x, stack *ps)
{
stack_element *temp;
temp = malloc(sizeof(stack_element));
if (!temp)
return 0;
temp->data = x;
temp->link = *ps;
*ps = temp;
return 1;
}
int pop(stack *ps, stack_type *px)
{
stack_element *temp;
if (*ps)
{
temp = *ps;
*ps = temp->link;
*px = temp->data;
free (temp);
return 1;
}
return 0;
}
int stack_top (stack *ps, stack_type *px)
{
if (*ps)
{
*px = (*ps)->data;
return 1;
}
return 0;
}
int stack_is_empty (stack *ps)
{
return *ps == NULL;
}
int stack_is_full (stack *ps)
{
return 0;
}