5 Replies - 273 Views - Last Post: 18 April 2013 - 01:26 PM Rate Topic: -----

#1 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 10:54 AM

I am working on a program that will open a .data file and it will perform simple math operations in the format:

The input file:

addition,82,10

subtraction,3,57

multiplication,90,46

division,26,7

division,26,0

addition,23,10

subtraction,14,57

multiplication,90,6

division,26,13


My Output:
82 + 10 = 92
3 - 57 = -54
90 * 46 = 4140
26 / 7 = 0.00
26 / 0 = Undefined Operation
23 + 10 = 33
14 - 57 = -43
90 * 6 = 540
26 / 13 = 0.00




My program is running fine, with the only problem being the division, in which only 0.00 prints out.

This is my code:

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

int main()
{
	
	char old_string[50], new_string[50];/*Calling out variables*/
	int number1, number2, answer, i;
	FILE* file_pointer;/*Pointer to open data file*/
	
	file_pointer = fopen("lab3_1.data", "r");
		
	while (fgets(new_string, 21, file_pointer)!= NULL)
	{
		sscanf(new_string, "%[^,]%*c %i%*c %i%*c ", &old_string, &number1, &number2);
		
		/*Each situation will transform the word operation into a symbol operation*/
		if(strcmp(old_string, "subtraction") == 0)
		{
			answer = number1-number2;
			printf("%d - %d = %i \n", number1, number2, answer);
		}
		
		if(strcmp(old_string, "addition") == 0)
		{
			answer = number1 + number2;
			printf("%d + %d = %i \n", number1, number2, answer);
		}
		
		if(strcmp(old_string, "multiplication") == 0)
		{
			answer = number1 * number2;
			printf("%d * %d = %i \n", number1, number2, answer);
		}
		
		if(strcmp(old_string, "division") == 0)
		{
			if(number2 == 0)/*If the second number is 0, it will print out undefined*/
				printf("%d / %d = Undefined Operation\n", number1, number2);
			else
			{
				answer = number1 / number2;
				printf("%d / %d = %.2f \n", number1, number2, answer);
			}
		}
	}
return 0;
}



Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Translating Word Operand to Symbol Operand Question

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,272
  • Joined: 25-December 09

Re: Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 11:07 AM

You may want to start by fixing any warnings or errors your code produces:

Quote

main.c||In function ‘main’:|
main.c|15|warning: format ‘%[^,’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[50]’ [-Wformat]|
main.c|18|error: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]|
main.c|43|warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat]|
main.c|8|warning: unused variable ‘i’ [-Wunused-variable]|
||=== Build finished: 1 errors, 3 warnings ===|


If you're not getting these warnings or errors I suggest you read your compiler/IDE documentation and increase the warning level. If you are getting warnings, fix them. Warnings are a sure sign that your program is broken.


Next do you realize that an int has no fractional parts. Therefore when you do something like 1/2 you will get zero.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 11:27 AM

I was able to fix the problem, I put the "answer" variable as a double

double answer;


/*In the division if statement*/

answer = number1/((double)number2);
printf("%d / %d = %.2f \n", number1, number2, answer);



Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,272
  • Joined: 25-December 09

Re: Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 11:39 AM

You actually changed two things. You also need that cast in the actual calculation as well.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 11:53 AM

Ok, I ended up just creating two variables for the answer; one is answer1 and the other answer.

int answer1, number1, number2;
double answer;

/*This is for the subtraction, addition, and multiplication*/

answer = number1 /* +, *,  -  */ number2;
printf("%d (+, *,  -)  %d = %i", number1, number2, answer1);

/*And for division*/

answer = number1/((double)number2);
printf("%d / %d = %.2f", number1, number2, answer);



Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Translating Word Operand to Symbol Operand Question

Posted 18 April 2013 - 01:26 PM

sscanf(new_string, "%[^,]%*c %i%*c %i%*c ", &old_string, &number1, &number2);



sscanf's first parameter should be pointer to the beginning of the buffer. Drop the & at the beginning of old_string.


The reason for this is that arrays naturally decay to pointers since old_string is really just the address of the first element in the array.

This post has been edited by jjl: 18 April 2013 - 01:28 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1