Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:26:06+00:00 2026-06-12T09:26:06+00:00

Hi i have program here that accept int as value. i wanted to translate

  • 0

Hi i have program here that accept int as value. i wanted to translate it to accept strings in array then. i have read about using struct but i couldnt get into it. i hope someone can help me getting into that without using struct i dont know where to start i want to keep this lines of code.

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int top = 0;
int *stack = NULL;
int size = 0;

main()
{
    int opt, num;
    char cont[] = { 'y' };
    clrscr();

    /* <start Declaring Stack Size { */
               printf("Stacking Program");
               printf("\n\nData Size: ");
               scanf("%d", &size);
               printf("\n");
    /* } end> */

               /* <start Allocates size of stack { */
               if(size > 0)
               {
                stack = malloc(size * sizeof(int));
                if(stack == NULL)
                {
                    printf("ERROR: malloc() failed\n");
                    exit(2);
                }
               }
               else
               {
                    printf("ERROR: size should be positive integer\n");
                    exit(1);
               }
                /* } end> */

    while((cont[0] == 'y') || (cont[0] == 'Y'))
    {
        clrscr();

        /* <start Main Menu { */
            printf("Stacking Program");
            printf("\n\nData Size: %d\n\n", size);
            printf("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: ");
            scanf("%d", &opt);
            printf("\n");

        switch(opt) {
            case 1:
                pop();
                break;
            case 2:
                if(top==size)
                {
                    printf("You can't push more data");
                }
                else
                {
                    printf("Enter data for Stack[%d]: ", top+1);
                    scanf("%d", &num);
                    push(num);
                }
                break;
            case 3:
                pick();
                break;
            case 4:
                view();
                break;
            default:
                printf("Your choice is not on the list.");
                break;
        }
        /* } end> */

        printf("\n\nDo you want continue\(Y\/N\)?");
        scanf("%s", &cont[0]);
    }
    free(stack);
}

pop()
{
    int a;
    loading();
    if(top <= 0)
    {
        printf("Stack empty.");
        return 0;
    }
    else
    {
        top--;
        a=stack[top];
        printf("\(Stack[%d] = %d\) removed.", top+1, a);
    }
}
push(int a)
{
        stack[top]=a;
        top++;
        loading();
}
pick()
{
    loading();
    if(top <= 0)
    {
        printf("Nothing to display.");
        return 0;
    }
    else
    {
        printf("\(Stack[%d] = %d\) is the last data.", top, stack[top-1]);
    }
}
view()
{
    int i;
    loading();
    if(top <= 0)
    {
        printf("Nothing to display.");
        return 0;
    }
    else
    {
        for(i=0;i<top;i++)
        {
            printf("Stack[%d] = %d\n", i+1, stack[i]);
        }
    }
}

loading()
{
    float i, x;
    float load;
    int loadarea[] = { 5000, 10000, 15000, 20000, 25000, 30000 };
    int percentLoad;
    x=0;
    load=0;
    percentLoad = loadarea[random(5)];
    gotoxy(26,11);
    printf("[");
    for(i=0;i<25;i++)
    {
        x = i+27;
        gotoxy(x, 11);
        printf("=");
        delay(percentLoad);
        gotoxy(51,11);
        printf("]");
        gotoxy(53,11);
        load=(i/25)*104.5;
        if(load>100)
            load = 100.00;
        printf("%.2f\%",load);
    }
    delay(60000);
    for(i=0;i<60;i++) {
        printf("\b \b");
    }
    printf("\n");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:26:07+00:00Added an answer on June 12, 2026 at 9:26 am

    Easiest way is to convert your stack to store char* instead of int.

    char **stack;
    stack = malloc( size * sizeof(char*) );
    

    Now, your push operation will accept a char* from some buffer that is storing the string that was just input, duplicate it with strdup, and store that new pointer in the stack.

    typedef enum {
        STACK_MEM_ERROR = -1,
        STACK_FULL = 0,
        STACK_OK = 1
    } StackStatus;
    
    StackStatus push(const char *str)
    {
        char *newstr;
        if( top >= size ) return STACK_FULL;
        newstr = strdup(str);
        if( newstr == NULL ) return STACK_MEM_ERROR;
        stack[top++] = newstr;
        return STACK_OK;
    }
    

    When you pop a string, you just get a pointer.

    char *pop()
    {
        if( top == 0 ) return NULL;
        return stack[--top];
    }
    

    You are responsible for freeing that memory when you are finished with the pointer (by calling free).

    char * val;
    while( NULL != (val = pop()) )
    {
        printf( "I popped: %s\n", val );
        free(val);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: This is a followup to my question here. I have a program that
Well, i have written a simple python program that parses HTML with HTMLParser. Here
I have a program that accepts a number that represents an array index and
here , i am creating file server program .in this i have noticed that
I have trouble deallocating memory that I allocated using malloc. The program runs fine
A slightly archaic question I'm afraid but here goes: I have a program which
Just a quick question here. I have a program in which I need to
I am trying to separate a few things in here. I have a program
I have been looking at ways to program a UI in ruby here, on
I'm at the end of my rope here: I have a single-threaded C++ program.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.