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 575959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:45+00:00 2026-05-13T13:59:45+00:00

How do I read in the following input in my RPN calculator so that

  • 0

How do I read in the following input in my RPN calculator so that it will find the operator no matter what order?

2
2+
4

As of now my scanf only sees the first char in the string and I can only do this:

2
2
+
4

I’m also trying to add an option for integer vs floating point mode. (ex. when ‘i’ is entered, operate in floating point and vice versa.)

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

int *p;
int *tos;
int *bos;

void push(int i);
int pop(void);

int main (void)
{
int a, b;
//float c, d;
char s[80];
//char op;  //declare string of 80 chars

p = (int *) malloc(MAX*sizeof(int)); //get stack memory
if (!p){
 printf("Allocation Failure\n");
 exit(1);
 }
 tos = p;
 bos = p + MAX-1;

 printf("\nRPN Calculator\n");
 printf("Enter 'i' for integer mode\n");
 printf("Enter 'f' for floating point mode\n");
 printf("Enter 'q' to quit\n");

 do {
 printf("> ");
 // gets(s);
 // scanf("%s", s);  //read integer
 scanf("%s", s);
 // switch (*s) {


 switch(*s) {
   case 'i':
       printf("(Integer Mode)\n");
   break;
   case 'f':
       printf("(Floating Point Mode)\n");
   break;
   case '+':
       a = pop();
       b = pop();
       printf("%d\n", a+b);
       push(a+b);
   break;
   case '-':
       a = pop();
       b = pop();
       printf("%d\n", b-a);
       push(b-a);
       break; 
  case '*':
       a = pop();
       b = pop();
       printf("%d\n", a*b);
       push(a*b);
  break;
  case '/':
       a = pop();
       b = pop();
       if(a == 0){
         printf("Cannot divide by zero\n");
  break;
  }
       printf("%d\n", b/a);
       push(b/a);
  break;
  case '.':
       a = pop();
       push(a);
       printf("Current value on top of stack: %d\n", a);
  break; 
  default:
       push(atoi(s));
  }

 } while (*s != 'q');

 return 0;
}


 // Put an element on the stack

 void push (int i)
 {
 if (p > bos){
   printf("Stack Full\n");
 return;
 }
  *p = i;
  p++;
 }

// Get the element from the top of the stack

int pop (void)
{
 p--;
 if(p < 0) {
    printf("Stack Underflow\n");
 return 0;
 }
 return *p;
 }
  • 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-05-13T13:59:45+00:00Added an answer on May 13, 2026 at 1:59 pm

    Your scanf reads the whole string. It’s the following switch that judges by the first character and misses that + in 2+.

    To improve it you can use the strtol function. It will parse an integer out of the string and return to you the location where the integer ended – if that’s still not the end of the string, there may be an operator there.

    A similar function for floating point numbers is strtod.


    Here’s some sample code of strtol applicable to your example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        char* input = "25+";
        char* endptr;
    
        int val = strtol(input, &endptr, 10);
    
        if (*endptr == '\0')
        {
            printf("Got only the integer: %d\n", val);
        }
        else
        {
            printf("Got an integer %d\n", val);
            printf("Leftover: %s\n", endptr);
        }
    
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following: you read in a string with scanf() but you only need
First of all let me tell you that i have read the following questions
I need to write a simple program for work that does the following: read
I'm trying to read a binary file that is in the following format: number
I'm trying to read a file that exists in the following location of my
I need to make a script that automates the following: Read comma or tab
I read the following comment on PHP doc pages : Be warned that most
I would like to have a program that makes the following actions: Read several
I am taking input from a file in following format: (int1,int2) (int3,int4) Now I
I need to read a string in a following order: Read any amount of

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.