#Creating a calculator using gets and sscans

31 messages · Page 1 of 1 (latest)

proven cargo
#

Having issues writing a script for homework where i am needing to create a calculator using gets and sscans for +,-,*,/,%. Having difficulties when executing and dont know where i went wrong....

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

int main()
{

int num1, num2, out;
char op[10];

gets(op);

sscanf(op, "%d, %c, %d", &num1, op, &num2);

if (op[0] == '+') out = num1 + num2 ;
{
printf(op, "%d", out);
}
if (op[0] == '-') out = num1 - num2;
{
printf(op, "%d", out);
}
//else if (op == '*') o = num1 * num2;
//else if (op == '/') o = num1 / num2;
//else if (op == '%') o = num1 % num2;

return 0;
}

when executing in cmd :

input: 1+1
output: 1+11+1

wraith prairieBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

drifting pelican
#

first of all, never ever ever use gets()

#

you can just use scanf() here

#

secondly, why do you have commas in the format string?

#

just "%d %c %d" should work

#

thirdly, that's not how printf() works

#

there are some other stuff as well

proven cargo
#

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

int main()
{

int num1, num2, out;
char op[10];

gets(op);

sscanf(op, "%d %c %d", &num1, op, &num2);

if (op[0] == '+')
{
out = num1 + num2;
printf("%d", out);
}
if (op[0] == '-' )
{
out = num1 - num2;
printf("%d", out);
}
//else if (op == '*') o = num1 * num2;
//else if (op == '/') o = num1 / num2;
//else if (op == '%') o = num1 % num2;

return 0;
}

#

was able to actually fix it and thanks for the input but its a requirement of the questions to use the two functions in my script :/

wraith prairieBOT
#

@proven cargo Has your question been resolved? If so, run !solved :)

proven cargo
#

now i have to figure this part of the problem out "The program should continuously accept new inputs before until the user submits a blank line (return), and should not produce any output except for the answers to provided expressions."

drifting pelican
#

sscanf(op, "%d %c %d", &num1, op, &num2); - don't do this

#

you're parsing the same string that you're writing into

#

also, again, NEVER EVER EVER use gets()

#

you're just complicating your life by not using scanf()

proven cargo
drifting pelican
#

then just use fgets()

#

not gets()

#

and don't sscanf() into the source string

proven cargo
#
#include <stdlib.h>

int main()
{

   int num1, num2, out;
   char op[10];

  //sscanf(op, "%d %c %d", &num1, op, &num2);


 while(fgets(op, 10, stdin)!= '\n')
{
    scanf(op, "%d %c %d", &num1, op, &num2);

        if (op[0] == '+')
        {
            out = num1 + num2;
            printf("%d\n", out);
        }
        if (op[0] == '-' )
        {
            out = num1 - num2;
            printf("%d\n", out);
        }
        if (op[0] == '*' )
        {
            out = num1 * num2;
            printf("%d\n", out);
        }
        if (op[0] == '/' )
        {
            out = num1 - num2;
            printf("%d\n", out);
        }
        if (op[0] == '%' )
        {
            out = num1 - num2;
            printf("%d\n", out);
        }

  }
   return 0;
}
#

okay i am stuck for line 13 i am getting an error of comparison between pointer and interger

drifting pelican
#

again, please stop sscanf(op, ..., op)

#
char line[64];

while (fgets(line, sizeof(line), stdin)) {
  char op;
  int x, y;

  if (sscanf(line, "%d %c %d", &x, &op, &y) != 3)
    break;

  ...
}
proven cargo
#
#include <stdlib.h>

int main()
{
    int out;
    char line[64];

while (fgets(line, sizeof(line), stdin))
{
  char op;
  int x, y;

  if (sscanf(line, "%d %c %d", &x, &op, &y) != 3)
    break;
{
    if (line[0] == '+')
    {
        out = x + y;
        printf("%d", out);
    }
   if (line[0] == '-' )
   {
       out = x - y;
       printf("%d", out);
   }
    if (line[0] == '*' )
   {
       out = x * y;
       printf("%d", out);
   }
      if (line[0] == '/' )
   {
       out = x / y;
       printf("%d", out);
   }
      if (line[0] == '%' )
   {
       out = x % y;
       printf("%d", out);
   }

  }
   return 0;
}
}
#

so code executes but still having issues with it not completing the arithmetic and exiting out of the .exe file

proven cargo
proven cargo
#
#include <stdlib.h>

int main()
{
    int x, y, out;
    char op;
    char line[64];
    while(fgets(line, sizeof(line),stdin))
   {
    if (sscanf(line, "%d %c %d", &x, &op, &y) == EOF)
        break;

    if (op == '+')
    {
        out = x + y;
        printf("%d \n", out);
    }
    if (op == '-' )
   {
       out = x - y;
       printf("%d \n", out);
   }
    if (op == '*' )
   {
       out = x * y;
       printf("%d \n", out);
   }
    if (op == '/' )
   {
       out = x / y;
       printf("%d \n", out);
   }
    if (op == '%' )
   {
       out = x % y;
       printf("%d \n", out);
   }
   }
}
#

got it

#

!solved