Alright so my professor gave us an assignment to make an RPN calculator. He supplied us with the code to read in values and we have to write the code to put the values in an array and do the calculations. I understand the basics of arrays but I'm having a tough time trying to put the numbers that are inputted, into an array.
What's in red is what I've put in so far and the rest is what he supplied. I need to put the values that are inputted into an array. Am I on the right track with the for loop?Code://Header Files #include <stdio.h> #include <math.h> #define MAX_BUFFER_SIZE 100 #define MAX_STACK_SIZE 100 //Start of main function int main(void) { //Declarations char buff[MAX_BUFFER_SIZE]; double i, x, stack[MAX_STACK_SIZE]; int num_operand=0; //Program Title printf(" REVERSE POLISH CALCULATOR\n"); printf("-------------------------------------------------\n"); //This reads a string from the user if( scanf("%s", buff) < 1 ) { printf("I did not understand\n"); return 0; } //Checks to see if the user entered a number if(isdigit(buff[0]) || isdigit(buff[1])) { //Turns string into number and puts value in x sscanf( buff, "%lf", &x); //Place statements here to handle getting a number for(i = 0; i < MAX_STACK_SIZE; i++) { stack[num_operand]=x; num_operand ++; } } else { //Place statements to do operations } //Exit program printf("Press RETURN to exit program \n"); getchar(); getchar(); return 0; }
Also, I really don't understand how the values you want to put in are inputted.. When I run the program, I can type a number in, hit enter, and the program just stops..



LinkBack URL
About LinkBacks




. Maybe a clue on how to add numbers in array?