I am getting a "Type mismatch in parameter 1 in call to 'push'" error at the 28th line of the code in Turbo C++, please help me as I have to submit my project, I know that Turbo C++ is a very old compiler but that's what our University Teacher recommends so I cant do nothing in that
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#include <stdio.h>
#include <conio.h>
int stack[100];
int top = -1;
void in2post(char[]);
void push(int);
int pop();
int prec(char);
int cal(char[]);
void main()
{
char in[100], post[100];
clrscr();
printf("Enter an infix expression: ");
gets(in);
in2post(in);
getch();
}
void in2post(char in_exp[])
{
int x = 0, y = 0, z, result = 0;
char a, c, post_exp[100];
char t;
push("\0");
t = in_exp[x];
while (t != '\0')
{
if (isalnum(t))
{
post_exp[y] = t;
y++;
}
else if (t == '(')
{
push('(');
}
else if (t == ')')
{
while (stack[top] != '(')
{
c = pop();
post_exp[y] = c;
y++;
}
c = pop();
}
else
{
while (prec(stack[top]) >= prec(t))
{
c = pop();
post_exp[y] = c;
y++;
}
push(t);
}
x++;
t = in_exp[x];
}
while (top != -1)
{
c = pop();
post_exp[y] = c;
y++;
}
printf("\nThe equivalent postfix expression is: ");
for (z = 0; z < y; z++)
printf("%c", post_exp[z]);
printf("\n\nDo you want to evaluate the postfix expression? (Y/N): ");
scanf("%c", &a);
if (a == 'y' || a == 'y')
{
result = cal(post_exp);
printf("\nResult = % d\n", result);
getch();
}
else if (a == 'n' || a == 'N')
{
exit(0);
}
}
int cal(char post[])
{
int m, n, x, y, j = 0, len;
len = strlen(post);
while (j < len)
{
if (isdigit(post[j]))
{
x = post[j] - '0';
push(x);
}
else
{
m = pop();
n = pop();
switch (post[j])
{
case '+':
x = n + m;
break;
case '-':
x = n - m;
break;
case '*':
x = n * m;
break;
case '/':
x = n / m;
break;
}
push(x);
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Related
im new to programming and im a first year in college, my teacher gave me and my group mates a task to create a console calculator but we run into a problem since this is our first problem.
the problem is that if the input does not satisfied or met any condition in my if statement it loops the error.
here is the code:
#include <stdbool.h>
double calculate(double number1, char operation, double number2);
bool isInteger(double val)
{
int truncated = (int)val;
return (val == truncated);
}
int main()
{
double number1, number2, result;
char operation;
int validator = 0, operationValid = 0;
while(validator == 0)
{
fflush(stdin);
printf("Enter number and operator to solve:\n\nExapmple [a + b][a - b][a * b][a / b]\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
while(operationValid == 0)
{
fflush(stdin);
if(operation == '+' || operation == '-' || operation == '*' || operation == '/')
{
if(isInteger(number1) && isInteger(number2))
{
result = calculate(number1, operation, number2);
printf("%.1lf %c %.1lf = %.1lf", number1, operation, number2, result);
operationValid = 1;
validator = 1;
}
else
{
printf("Invalid Inputed numbers!");
operationValid = 0;
validator = 0;
}
}
else
{
printf("Invalid Operation!");
operationValid = 0;
validator = 0;
}
}
}
}
double calculate(double number1, char operation, double number2)
{
double a, b, c;
char x;
a = number1;
b = number2;
x = operation;
if(x == '+')
{
c = a + b;
}
else if(x == '-')
{
c = a - b;
}
else if(x == '*')
{
c = a * b;
}
else if(x == '/')
{
c = a / b;
}
return c;
}
please if someone can provide help and maybe a little explanation. Thanks.
#include <stdio.h>
int main(void)
{
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
char values[2];
scanf("%c", &values[0]);
scanf("%d", &values[1]);
if (values[0] == '*')
{
a = a * values[1];
printf(" = %d\n", a);
}
else if (values[0] == '+')
{
a = a + values[1];
printf(" = %d\n", a);
}
else if (values[0] == '%')
{
a = a % values[1];
break;
}
}
printf("%d", a);
}
When I input 5 + 3 + 7 + 10 + 2 + 3 + 1 % 11, it would show 5 (because 5%11 = 5). But the + operation didn't work. Can you see what is the problem here?
I think as values[2] is only two variable you need, you can use two different variable to do your job. use one char type variable and one int type variable as you need these two. You have another problem in your code, use a getchar() in the inside of the loop then your code will work fine, cause when you give a integer value as input then take a character value last you enter the new line that goes to that character that is why your code was giving error.
#include <stdio.h>
int main(void){
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
getchar();
char ch;
int value;
scanf("%c", &ch);
scanf("%d", &value);
if (ch == '*')
{
a = a * value;
printf(" = %d\n", a);
}
else if (ch == '+')
{
a = a + value;
printf(" = %d\n", a);
}
else if (ch == '%')
{
a = a % value;
break;
}
}
printf("%d", a);
}
There are multiple problems:
value is an array of char: scanning the operator into a char is fine, but the value should be converted into an int variable.
the scanf("%c", ...) will store the next byte into the variable, but after converting an int, the next byte is the pending space or newline, not the '+'. You should use scanf(" %c", ...) to skip the whitespace after the previous conversion.
Here is a modified version:
#include <stdio.h>
int main(void) {
int a;
if (scanf("%d", &a) != 1)
return 1;
for (int i = 0; ; i++) {
char op;
int value;
if (scanf(" %c", &op) != 1)
break;
if (scanf("%d", &value) != 1)
break;
if (op == '*') {
a = a * value;
printf(" = %d\n", a);
} else
if (op == '+') {
a = a + value;
printf(" = %d\n", a);
} else
if (op == '-') {
a = a - value;
printf(" = %d\n", a);
} else
if (op == '%') {
a = a % value;
printf(" = %d\n", a);
} else
if (op == '/') {
a = a / value;
printf(" = %d\n", a);
} else {
printf("invalid operator %c\n", op);
break;
}
}
printf("%d\n", a);
return 0;
}
I am creating a program that evaluates a postfix expression contained in a single line of a text file. I'm having some trouble dealing with blank spaces in the scanned file. What I've done so far is scan the single line from the file into a buffer, and then process the string one character at a time. How do I ignore blank spaces once I've read the line into a string? For example:
2 4 3 * +
Here is the full program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int stack[1000];
int top = -1;
void push(int x);
int pop();
bool isOperator(char ch);
int performOperation(int op1, int op2, char op);
int main()
{
char exp[1000], buffer[15];
int i, num, op1, op2, len, j, x;
int stack[1000];
char fileName[20];
FILE *inFile;
char *e;
printf("Please enter text file:");
scanf("%s", fileName);
inFile = fopen(fileName, "r");
if (inFile == NULL) {
printf("Error\n");
return -1;
}
int N = 0; i = 0, temp;
while (!feof(inFile)) {
fgets(buffer, 15, inFile);
N++;
}
printf("Postfix expression:\n");
printf("%s", buffer);
e = buffer;
while (*e != '\0') {
if (isdigit(*e)) {
num = *e - 48;
push(num);
} else {
op1 = pop();
op2 = pop();
if (isOperator(*e)) {
int ans;
ans = performOperation(op1, op2, *e);
}
push(ans);
}
e++;
}
printf(" The value of the expression is %d\n", ans);
}
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
bool isOperator(char ch) {
if (ch == '+' || ch == '-' || ch == '*'|| ch == '/')
return true;
else
return false;
}
int performOperation(int op1, int op2, char op) {
int ans;
switch (op) {
case '+':
ans = op2 + op1;
break;
case '-':
ans = op2 - op1;
break;
case '*':
ans = op2 * op1;
break;
case '/':
ans = op2 / op1;
break;
}
return ans;
}
Any help is appreciated.
I forgot to mention that we are only dealing with single-digit numbers.
You can remove the spaces so you don't have to deal with them
void remove_spaces(char* s) {
const char* d = s;
do {
while (*d == ' ') {
++d;
}
} while (*s++ = *d++);
}
If I have understood correctly the problem is processing embedded white spaces.
To resolve the problem you can rewrite the while loop at least the following way
int ans = 0;
for ( e = buffer; *e != '\0'; ++e ) {
if ( !isspace( ( unsigned char )*e ) )
{
if ( isdigit( ( unsigned char )*e ) ) {
num = *e - '0';
push(num);
} else if ( isOperator(*e) ) {
op1 = pop();
op2 = pop();
ans = performOperation(op1, op2, *e);
push(ans);
}
}
}
ans = pop();
printf(" The value of the expression is %d\n", ans);
Pay attention to this statement before the printf call
ans = pop();
You have to pop the answer from the stack before printing it. Also you should process invalid characters and check whether the stack is empty.
Also this loop
int N = 0; i = 0, temp;
while (!feof(inFile)) {
fgets(buffer, 15, inFile);
N++;
}
seams does not make sense and the condition of the loop is incorrect. For example for an empty file the variable buffer will not contain a valid data.
There are multiple problems in your code:
The loop while (!feof(inFile)) is incorrect. You should instead use:
while (fgets(buffer, sizeof buffer, inFile)) {
/* handle the string expression in buffer */
you should not push the number immediately when encountering a digit, you should parse the number that may have more than one digit.
once you parse numbers correctly, you can discard any white space encountered in the parser.
Here is a modified version:
#include <stdio.h>
bool isOperator(char ch) {
if (ch == '+' || ch == '-' || ch == '*'|| ch == '/')
return true;
else
return false;
}
int performOperation(int op1, int op2, char op) {
int ans;
switch(op) {
case '+':
ans = op2 + op1;
break;
case '-':
ans = op2 - op1;
break;
case '*':
ans = op2 * op1;
break;
case '/':
ans = op2 / op1;
break;
}
return ans;
}
int main() {
char fileName[100];
char buffer[100];
FILE *inFile;
printf("Please enter text file:");
if (scanf("%99s", fileName) != 1) {
printf("No input\n");
return 1;
}
inFile = fopen(fileName, "r");
if (inFile == NULL) {
printf("Error\n");
return -1;
}
while (fgets(buffer, sizeof buffer, inFile);
printf("Postfix expression:\n");
printf("%s", buffer);
char *e = buffer;
while (*e != '\0') {
if (isdigit((unsigned char)*e)) {
int num = 0;
while (isdigit((unsigned char)*e)) {
num = num * 10 + *e++ - '0';
}
push(num);
} else
if (isspace((unsigned char)*e) {
e++; // ignore white space
} else
if (isOperator(*e)) {
int op1 = pop();
int op2 = pop();
int ans = performOperation(op1, op2, *e++);
push(ans);
} else {
printf("Invalid character in expression: %c\n", *e++);
}
}
int ans = pop();
printf(" The value of the expression is %d\n", ans);
}
fclose(inFile);
return 0;
}
I'm doing a homework problem which is to create a calculator using the argc and argv[] parameters in the main function.
What we were supposed to do, and I did, was to create a char pointer array containing the operators I want to use.
this is what I have:
const int OTabMax = 10;
const char *OTab[] = {
"+",
"-",
"*",
"/",
"quad",
"wurz",
"sinR",
"sinG",
"cosR",
"cosG"
};
int OTabCheck(char S[]) // char *S
{
int n;
for (n = 0; n < OTabMax; n++){
if (strcmp(S, OTab[n]) == 0) {break;}
}
return n;
}
It returns a number depending on which operator was used, which goes into a switch case where the calculation is done.
looks like this:
switch(key){
case 0:
break;
case 1:
break;
case 2:
x = atof(argv[1]);
y = atof(argv[3]);
z = 0;
z = x*y;
printf("%f * %f = %f\n",x,y,z);
break;
case 3-8;
break;
case 9:
break;
default: printf("Wrong input!\n"); exit(0); break;
}
The case in question is case 2: which whenever I do a calculation like calling the program through the terminal with ./"program_name" 2 * 2
it printf's 2.000000 * 0.000000 = 2.000000
here is the MRE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
const int OTabMax = 10;
const char *OTab[] = {
"+",
"-",
"*",
"/",
"quad",
"wurz",
"sinR",
"sinG",
"cosR",
"cosG" };
int OTabCheck(char S[]) // char *S
{
int n;
for (n = 0; n < OTabMax; n++){
if (strcmp(S, OTab[n]) == 0) {break;}
}
return n;
}
int main(int argc,char *argv[]){
float x = 0;
float y = 0;
float z = 0;
int key = 0;
if(argc < 3){
exit(0);
}
if(argc == 4){
key = OTabCheck(argv[2]);
}else if(argc == 3){
key = OTabCheck(argv[1]);
}
switch(key){
case 2:
x = atof(argv[1]);
y = atof(argv[3]);
z = 0;
z = x*y;
printf("%f * %f = %f\n",x,y,z);
exit(0);
break;
default: printf("Wrong input!\n"); exit(0); break;
}
}
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
I have generated random number into a 2d array.What I want now is to connect the same number by moving it inside the game board.The problems is it doesn't move.
I really need help!!! I am new to c programming btw. Thanks in advance.
Here's my code:
void playgame(char box[ROW][COL])
{
int x, y, choice2,num,direction=0;
char input;
do{
printf("Please select a number (1-7) : \n");
scanf("%i",&num);
if(x==0 || x==ROW-1 ||y==0 || y==COL-1)
{
printf("Invalid!\n");
}
else
{
printf("Numer %i is currently selected!\n", num);
}
}while(x==0 || x==ROW-1 ||y==0 || y==COL-1);
printf("[1]Move\n[2]Sign out\n");
printf("Enter choice: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1:
{
printf("Press 'E' to go up\n");
/*codes for moving the character up.....*/
}
{
printf("Press 'D' to go right\n");
/*codes for moving the character down.....*/
}
{
printf("Press 'S' to go left\n");
/*codes for moving the character left.....*/
}
{
printf("Press 'X' to go down\n");
/*codes for moving the character up.....*/
}
{
printf("Press 'R' to remove the existing path\n");
}
fflush(stdin);
scanf("%c", &input);
break;
case 2: printf("Bye!\n");
}
for(x=0; x<9; x++)
for(y=0; y<9; y++)
{
if(input == 'E')
if(box[x][y]==num)
{
box[--x][y]==num;
}
if(input == 'D')
if(box[x][y]==num)
{
box[x][y++]==num;
}
if(input == 'S')
if(box[x][y]== num)
{
box[x][--y]== num;
}
if(input == 'X')
if(box[x][y]==num)
{
box[--x][y]==num;
}
}
}
try this:
#include <stdio.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
void display(char box[ROW][COL]);
int validMove(int r, int c, char box[ROW][COL]);
void playgame(char box[ROW][COL]){
int r, c, num;
int choice, nr, nc;
char input, n;
for(;;){
printf("Please select a number (1-7) : \n");
if(1 == scanf("%i", &num) && 1<= num && num <= 7)
break;
printf("Invalid!\n");
while(getchar() != '\n');
}
printf("Numer %i is currently selected!\n", num);
n = num + '0';
for(r = 1; r < ROW-1; ++r)
for(c = 1; c < COL-1; ++c)
if(box[r][c] == n)
goto find;
find:
for(;;){
printf("[1]Move\n"
"[2]Sign out\n"
"Enter choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Press 'E' to go up\n"
"Press 'D' to go right\n"
"Press 'S' to go left\n"
"Press 'X' to go down\n"
"Press 'R' to remove the existing path\n");
scanf(" %c", &input);
nr = r;
nc = c;
switch(input){
case 'E':
nr = r - 1;
break;
case 'D':
nc = c + 1;
break;
case 'S':
nc = c - 1;
break;
case 'X':
nr = r + 1;
break;
case 'R':
break;
default:
break;
}
if(validMove(nr, nc, box)){
box[nr][nc] = box[r][c];
box[r][c] = ' ';
r = nr;
c = nc;
display(box);
} else if(input != 'R'){
printf("invalid move!\n");
}
break;
case 2: printf("Bye!\n");
exit(0);
}
}
}
int validMove(int r, int c, char box[ROW][COL]){
if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
return 0;
if(box[r][c] != ' ')
return 0;
return 1;
}
void initBoard(char box[ROW][COL]){
int r, c;
for(r = 0; r < ROW; ++r){
for(c = 0; c < COL; ++c){
if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
box[r][c] = '#';
else
box[r][c] = ' ';
}
}
for(int i = 1; i <= 7; ++i){
int r = rand() % (ROW-1-1) + 1;
int c = rand() % (COL-1-1) + 1;
if(box[r][c] == ' ')
box[r][c] = i + '0';
else
--i;
}
}
void display(char box[ROW][COL]){
for(int r = 0; r < ROW; ++r){
for(int c = 0; c < COL; ++c){
putchar(box[r][c]);
}
putchar('\n');
}
}
int main(void){
char board[ROW][COL];
initBoard(board);
display(board);
playgame(board);
return 0;
}