Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 17th, 2009, 4:28 PM   #1
DJ007
Newbie
 
Join Date: Mar 2009
Posts: 10
Rep Power: 0 DJ007 is on a distinguished road
Calculate using '-' HELP!

Hi, this is neary working how i want it exept i cant calculate negative numbers
example- (-56 67 +). at one stage it was but i messed the code up abit and now cant seem to fix it.

C Syntax (Toggle Plain Text)
  1. /******************************************************************************
  2. * ERROR5: "Input strings too long. Applies to numbers and operators."
  3. * ERROR4: "Operation not possible."
  4. * ERROR3: "Division by zero."
  5. * ERROR2: "Unknown operation."
  6. * ERROR1: "Number contains alphabetic characters."
  7. *
  8. ******************************************************************************/
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <ctype.h>
  15.  
  16. #define MAX 11
  17.  
  18.  
  19.  
  20. void main()
  21. {
  22. float Divide1,Divide2 = 0.0;
  23. void function(char,float,float);
  24.  
  25. int Num1;
  26. int Num2,y,v,k=0,x;
  27. char isdigit(char);
  28. char Str2[MAX];
  29. char Str1[MAX];
  30. char Operator[1];
  31. //do-while loop until Exit is Typed
  32. do
  33. {
  34. printf("\nPlease enter the required calculation\n");
  35. //Get first string
  36. scanf("%s", Str1);
  37. //Exit program if 'Exit' typed
  38. if (strcmp(Str1,"Exit") == 0)
  39. break;
  40. //Get second string
  41. scanf("%s", Str2);
  42. if (strcmp(Str2,"Exit") == 0)
  43. break;
  44. // if numbers entered are above MAX display error
  45. if (strlen(Str1) > MAX -1)
  46. printf("ERROR5");
  47.  
  48. else if (strlen(Str2) > MAX -1)
  49. printf("ERROR5");
  50.  
  51. Divide1= atof(Str1);
  52. Divide2= atof(Str2);
  53.  
  54. //Calculate Squareroot if string2 is an 'S'
  55. if ((*Str2) == 'S')
  56. printf("=%f\n",sqrt(Divide1) );
  57. //Calculate Reciprocal if string2 is an 'R'
  58. else if ((*Str2) == 'R')
  59. printf("=%f\n",1/Divide1 );
  60.  
  61. // Check if strings are alphabetacal
  62. if ((*Str2 != 'S') & (*Str2 != 'R'))
  63. {
  64. y = strlen(Str1);
  65. for(x = 0; x < y; x++)
  66. {
  67. if ((!isdigit(Str1[x])) & (Str1[0] != '-'))
  68. {
  69.  
  70. k = 1;
  71. }
  72. else if ((Str1[0] == '-') & (!isdigit(Str1[x+1])))
  73. {
  74. k = 1;
  75. }
  76. }
  77. y = strlen(Str2);
  78. for(x = 0; x < y; x++)
  79. {
  80. if ((!isdigit(Str2[x])) & (Str2[0] != '-'))
  81. {
  82. k = 1;
  83. }
  84. else if ((Str2[0] == '-') & (!isdigit(Str2[x+1])))
  85. {
  86. k = 1;
  87. }
  88. }
  89. if (k == 1)
  90. {
  91. printf("ERROR1");
  92. }
  93. else
  94. {
  95. scanf(" %s", &Operator);
  96. if (strlen(Operator) > 1)
  97. {
  98. printf("ERROR2");
  99. }
  100.  
  101. Num1= atoi(Str1);
  102. Num2= atoi(Str2);
  103. //Switch statement to calculate Addition,Subtraction,Multiplication and Division
  104. switch(Operator)
  105. {
  106. case '+':
  107. printf("= %d", Num1+Num2);
  108. break;
  109. case '-':
  110. printf("= %d \n", Num1-Num2);
  111. break;
  112. case '*':
  113. printf("=%d ", Num1*Num2);
  114. break;
  115. case '/':
  116. function(*Str2,Divide1,Divide2);
  117. break;
  118. default:
  119. printf("ERROR2");
  120.  
  121. }
  122. }
  123. }
  124. k = 0;
  125. fflush(stdin);
  126.  
  127. }
  128. while(strcmp(Str1, "Exit") != 0);
  129. getch();
  130. }
  131.  
  132.  
  133. //function to display error if divide by '0' else division performed
  134. void function(char Str2, float Divide1, float Divide2)
  135. {
  136. if(Str2 == '0')
  137. printf("ERROR3");
  138. else
  139. printf("=%f",Divide1/Divide2);
  140.  
  141. }
DJ007 is offline   Reply With Quote
Old May 17th, 2009, 10:09 PM   #2
The Dark
Programming Guru
 
Join Date: Jun 2005
Posts: 1,170
Rep Power: 6 The Dark will become famous soon enough
Re: Calculate using '-' HELP!

I think your problem is with this test:
if ((!isdigit(Str1[x])) & (Str1[0] != '-'))
You only want to test for - in the first character once, not for every other character
Something like:
if (! (isdigit(Str1[x]) || (x == 0 && (Str1[0] == '-')))
  k = 1;

Note that you should use && for boolean 'and', a single & is bitwise 'and'.

Also note, if you are checking the string content, you should also be checking the return value from scanf.
The Dark is offline   Reply With Quote
Old May 18th, 2009, 1:26 AM   #3
DJ007
Newbie
 
Join Date: Mar 2009
Posts: 10
Rep Power: 0 DJ007 is on a distinguished road
Re: Calculate using '-' HELP!

above Solution didn't work at all.. Plz help anyone..
DJ007 is offline   Reply With Quote
Old May 18th, 2009, 1:47 AM   #4
The Dark
Programming Guru
 
Join Date: Jun 2005
Posts: 1,170
Rep Power: 6 The Dark will become famous soon enough
Re: Calculate using '-' HELP!

above solution works fine. It was missing a bracket at the end of the if test which would have been pointed out by your compiler.
You also have the isdigit redeclared locally which stopped your code working on my computer.
You also have switch(Operator) where Operator is a char array - this isn't allowed (at least on my compiler) and would certainly not work as you are expecting even if the compiler allowed it. You need switch(Operator[0]).

Also, this line:
scanf(" %s", &Operator);
Will overflow the Operator array (which is length 1)

PS. "didn't work at all" is not a useful message. You would do much better by saying what didn't work.
The Dark is offline   Reply With Quote
Old May 19th, 2009, 5:39 PM   #5
DJ007
Newbie
 
Join Date: Mar 2009
Posts: 10
Rep Power: 0 DJ007 is on a distinguished road
Re: Calculate using '-' HELP!

Yes u are right.. Thanks a great deal
DJ007 is offline   Reply With Quote
Old Jun 28th, 2009, 5:43 AM   #6
mindblaster
Healthy Programmer
 
mindblaster's Avatar
 
Join Date: Jun 2009
Posts: 27
Rep Power: 0 mindblaster is on a distinguished road
Re: Calculate using '-' HELP!

Yeah i agree with u
mindblaster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate distance using values in an array annie_singh Perl 3 Oct 4th, 2008 11:33 PM
VB.NET: Loops - calculate selected items jaybird01 Visual Basic .NET 7 Aug 14th, 2006 5:35 AM
need help on how to calculate between 2 input times. oslan C 18 Feb 14th, 2006 9:15 PM
Problem with calculate difference between two dates!!! Esroh Java 3 May 26th, 2005 9:39 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC