Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Switch Statements C Programming


  • Please log in to reply
5 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:02:28 PM

Posted 06 March 2011 - 11:49 PM

I'm having some problems with my programming. The problem is with my switch statements, but I'll start from the beginning. I currently have 3 functions:

main -> will call my getInput funciton and calcFunction and printDisplay(not coded yet)
getInput -> will prompt the user for 3 values(numbers) and 2 operators( + - * / %) and must be entered in the following form: int char int char int: i.e. 10 - 5 - 5

Now, when doing normal arithmetic you have to take in to account the precedence levels for example: * / % are calculated before + -. So if you input 10 - 5 / 2 = the program will do the following : 10 - (5 / 2) so that (5 / 2) is done first then 10 - answer.


This is where my problem lies. On line 46, I want the program to calculate the code and put the value into variable answer, however I am prompted with this error:
error: expected ';' before 'operator1'

I have purposefully left out the other possibilities as I know they will yield the same answer. I'd like to get one part working before I move on.



1 #include<stdio.h>
  2 
  3 void getInput(int*, char*, int*, char*, int*);
  4 int calcFunction(int, char, int, char, int);
  5 
  6 
  7 int main ()
  8 {
  9 
 10   int value1;
 11   char operator1;
 12   int value2;
 13   char operator2;
 14   int value3;
 15   int answer;
 16 
 17   getInput(&value1, &operator1, &value2, &operator2, &value3);
 18   answer = calcFunction(value1, operator1, value2, operator2, value3);
 19 
 20  // printf("\nvalue1 = %d", value1);
 21  // printf("\noperator1 = %c", operator1);
 22  // printf("\nvalue2 = %d", value2);                   <===== DEBUG STATEMENTS TO SEE IF THE VALUES ARE ACTUALLY BEING ENTERED INTO THE VARIABLES
 23  // printf("\noperator2 =%c", operator2);
 24  // printf("\nvalue3 =%d", value3);
 25  // printf("\n");
 26 
 27   return(0);
 28 }
 29 -------------------------------------------------------------------------------------------------------
 30 void getInput(int* value1, char* operator1, int* value2, char* operator2, int* value3)
 31 {
 32 
 33   printf("Enter the expression in the form [int char int char int]: ");
 34   scanf("%d %c %d %c %d", value1, operator1, value2, operator2, value3);
 35 }
 36 -------------------------------------------------------------------------------------------------------
 37 int calcFunction(int value1, char operator1, int value2, char operator2,  int value3)
 38 {
 39   int answer;
 40      
 41   answer = value1 operator1 value2 operator2 value3;
 42      
 43   switch(operator2)
 44   {
 45          
 46     case '/': answer = value1 operator1 (value2 operator2 value3); 
 47               printf("%d %c (%d %c %d)= %d",value1, operator1, value2, operator2, value3, answer);
 48               printf("\n");
 49               break;
 50     default: printf("It works!!!");
 51              printf("\n");
 52 
 53 
 54   }
 55 
 56   return (0); //   <===== Should return answer to main, however since this function isn't working, I have
    n't added it yet!
 57 }

Edited by altair05, 06 March 2011 - 11:51 PM.


BC AdBot (Login to Remove)

 


#2 Didier Stevens

Didier Stevens

  • BC Advisor
  • 2,705 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:28 PM

Posted 07 March 2011 - 06:45 AM

Your error is here:

answer = value1 operator1 value2 operator2 value3;

You expect your C-compiler to evaluate operator1, notice that it's an operator like +, and then calculate the expression value1 + value2.
This doesn't work, a C-compiler doesn't work like that.

What you are trying to do is dynamically evaluate an expression, and this isn't possible in the C language.

What you need to write is an arithmetic expression parser, which is quite difficult to do if you've never done this before.

Didier Stevens
http://blog.DidierStevens.com
http://DidierStevensLabs.com

SANS ISC Senior Handler
Microsoft MVP 2011-2016 Consumer Security, Windows Insider MVP 2016-2019
MVP_Horizontal_BlueOnly.png

 

If you send me messages, per Bleeping Computer's Forum policy, I will not engage in a conversation, but try to answer your question in the relevant forum post. If you don't want this, don't send me messages.

 

Stevens' law: "As an online security discussion grows longer, the probability of a reference to BadUSB approaches 1.0"


#3 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:28 PM

Posted 07 March 2011 - 07:11 AM

Rather than expecting the program to notice that your character is an operator, why don't you build an additional Evaluate function for evaluating only a single section (like 5+6)? It would take 3 parameters (two integers and a character) and then use a case statement based on the character, which would probably be a simple enough thing to arrange. The original CalcFunction would have to sort the precedence of the operators and pass them to the evaluate function.

int evaluate(int value1, char operator, int value2)
{
  switch(operator)
  {
  case '+':
  return (value1 + value2)
  break;
  case '-':
[i]etc[/i]
  }
}

Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#4 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:02:28 PM

Posted 07 March 2011 - 11:00 PM

Thanks guys, you helped me finish my homework on time. Here is the finished product.


/************************************************************************* 
  2  *
  3  *  Programmer and Purdue Email Address:
  4  *  1. *************************************
  5  *
  6  *  Homework #:04
  7  *
  8  *  Academic Integrity Statement:
  9  *
 10  *       I have not used source code obtained from
 11  *       any other unauthorized source, either modified
 12  *       or unmodified.  Neither have I provided access
 13  *       to my code to another. The project I am submitting
 14  *       is my own original work.
 15  *
 16  *  Lab Division and Section:06/02
 17  *
 18  *  Program Description: This program calulates an expression that the user inputs. The user is prompted to input 3 integers and 2 operators. The program then uses various if/elseif/else and switch     statements to calculate the expression using order of precedence and then prints the answer to the screen. The program contains 3 functions: main, getInput, calcFunction.
 19  *
 20  ***************************************************************************/
 21  #include<stdio.h>
 22 
 23 //GLOBAL DECLARATIONS
 24 void getInput(int*, char*, int*, char*, int*);  //PROMPTS THE USER TO INPUT THE DATA INTO THE FUNCTION
 25 void calcFunction(int, char, int, char, int);    //CALCULATES THE EXPRESSION AND PRINTS OUT THE ANSWER
 26 
 27 /***************************************************************************
 28 *
 29 *     Function Information
 30 *
 31 *     Name of Function: main
 32 *
 33 *     Programmer's Name: ***************
 34 *
 35 *     Function Return Type: int
 36 *
 37 *     Parameters (list data type, name, and comment one per line):
 38 *     NO PARAMETERS REQUIRED
 39 *
 40 *     Function Description: This function will call the getInput and calcFunction functions and store any returned/pass by address variable in local variables.
 41 *
 42 ***************************************************************************/
 43 
 44 int main ()
 45 {
 46 
 47   int value1;       //STORES THE FIRST INTERGER THE USER DEFINES
 48   char operator1;   //STORES THE FIRST OPERATOR THE USER DEFINES
 49   int value2;       //STORES THE SECOND INTEGER THE USER DEFINES
 50   char operator2;   //STORES THE SECOND OPERATOR THE USER DEFINES
 51   int value3;       //STORES THE THIRD INTEGER THE USER DEFINES
 52 
 53   getInput(&value1, &operator1, &value2, &operator2, &value3);  //THIS FUNCTION WILL GET THE INTEGERS AND OPERTATORS FROM THE USER AND STORE THEM IN THE APPROPRIATE VARIABLES
 54   calcFunction(value1, operator1, value2, operator2, value3);   //THIS FUNCTION WILL CALCUALTE THE EXPRESSION AND PRINT THE ANSWER TO THE SCREEN
 55 
 56   return(0);
 57 
 58 }
59 
 60 /***************************************************************************
 61 *
 62 *     Function Information
 63 *
 64 *     Name of Function: getInput
 65 *
 66 *     Programmer's Name: *******************
 67 *
 68 *     Function Return Type: void
 69 *
 70 *     Parameters (list data type, name, and comment one per line):
 71 *       1. int* value1 - Stores the first integer value and passes it to the main
 72 *       2. char* operator1 - Stores the first operator and passes it to the main
 73 *       3. int* value2 - Stores the second integer value and passes it to the main
 74 *       4. char* operator2 - Stores the second operator and passes it to the main
 75 *       5. int* value3 - Stores the third integer value and passes it to the main
 76 *
 77 *     Function Description: This function will prompt the user to input the first, second, and third integer values and 2 operators. The function will then send the data back to the main via pass-by    -address
 78 *
 79 ***************************************************************************/
 80 
 81 void getInput(int* value1, char* operator1, int* value2, char* operator2, int* value3)
 82 {
 83 
 84   printf("Enter the expression in the form [int char int char int]: ");
 85   scanf("%d %c %d %c %d", value1, operator1, value2, operator2, value3);  //TAKES THE INPUT AND STORES IT IN THE APPROPRIATE VARIABLES FOR THE FUNCTION
 86 
 87 }
 88 
 89 /***************************************************************************
 90 *
 91 *     Function Information
 92 *
 93 *     Name of Function: calcFunction
 94 *
 95 *     Programmer's Name: *************
 96 *
 97 *     Function Return Type: void
 98 *
 99 *     Parameters (list data type, name, and comment one per line):
100 *       1. int value1 - Holds the first integer value
101 *       2. char operator1 - Holds the first operator
102 *       3. int value2 - Holds the second integer value
103 *       4. char operator2 - Holds the second operator
104 *       5. int value3 - Holds the third integer value
105 *
106 *     Function Description: This function will take the integers and operators that the user has stored and calculates the expression, eventually printing the answer to the screen. The function util    izes if/elseif/else and switch statements to determine the order of precedence and then calculates the answer. It also goes on to add parenthesis to show the user what two integers were calculated f    irst
107 *
108 ***************************************************************************/
109 
110 void calcFunction(int value1, char operator1, int value2, char operator2,  int value3)
111 {
112  
113   int answer1;      //HOLDS THE VALUE OF THE FIRST 2 VALUES IF CALCULATED
114   int answer2;      //HOLDS THE VALUE OF THE SECOND 2 VALUES IF CALCULATED
115   int answerFinal;  //HOLDS THE ANSWER OF THE EXPESSION
116  
117   if ((operator1 == '%' && operator2 == '%') || (operator1 == '/' && operator2 == '/') || (operator1 == '*' && operator2 == '*')) //CHECKS TO SEE IF BOTH OPERATORS ARE THE SAME(% OR / OR *)
118   {
119  
120     switch(operator1)   //TAKES OPERATOR1 AS A PARAMETER TO BE COMPARED WITH CASES
121     {
122       case '%': answerFinal = value1 % value2 % value3;     //IF BOTH OPERATORS ARE THE SAME THIS CASE IGNORES PRECEDENCE AND CALCULATES FROM LEFT TO RIGHT
123                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
124                 printf("\n");
125                 break;
126  
127       case '/': answerFinal = value1 / value2 / value3;     //IF BOTH OPERATORS ARE THE SAME THIS CASE IGNORES PRECEDENCE AND CALCULATES FROM LEFT TO RIGHT
128                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
129                 printf("\n");
130                 break;
131  
132       case '*': answerFinal = value1 * value2 * value3;     //IF BOTH OPERATORS ARE THE SAME THIS CASE IGNORES PRECEDENCE AND CALCULATES FROM LEFT TO RIGHT
133                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
134                 printf("\n");
135                 break;
136     }
137  
138   }
139 
140   else if ((operator1 == '+' && operator2 == '+') || (operator1 == '-' && operator2 == '-'))  //CHECKS TO SEE IF BOTH OPERATORS ARE THE SAME(+ OR -)
141   {
142 
143     switch(operator1)   //TAKES OPERATOR1 AS A PARAMETER TO BE COMPARED WITH CASES
144     {
145       case '+': answerFinal = value1 + value2 + value3;     //IF BOTH OPERATORS ARE THE SAME THIS CASE IGNORES PRECEDENCE AND CALCULATES FROM LEFT TO RIGHT
146                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
147                 printf("\n");
148                 break;
149 
150       case '-': answerFinal = value1 - value2 - value3;     //IF BOTH OPERATORS ARE THE SAME THIS CASE IGNORES PRECEDENCE AND CALCULATES FROM LEFT TO RIGHT
151                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
152                 printf("\n");
153                 break;
154     }
155 
156  }
157  
158   else if  (operator1 == '%' || operator1 == '/' || operator1 == '*') //CHECKS TO SEE IF OPERATOR1 IS % OR / OR *
159   {
160 
161     switch(operator1)   //TAKES OPERATOR1 AS A PARAMETER TO BE COMPARED WITH CASES
162     {
163       case '%': answer1 = value1 % value2;      //TAKES THE MODULUS OF THE FIRST 2 VALUES AND STORES IT IN ANSWER1
164                 break;
165 
166       case '/': answer1 = value1 / value2;      //TAKES THE DIVISION OF THE FIRST 2 VALUES AND STORES IT IN ANSWER1
167                 break;
168  
169       case '*': answer1 = value1 * value2;      //TAKES THE MULTIPLICATION OF THE FIRST 2 VALUES AND STORES IT IN ANSWER1
170                 break;
171     }
172 
173     switch(operator2)   //TAKES OPERATOR2 AS A PARAMETER TO BE COMPARED WITH CASES
{
175       case '%': answerFinal = answer1 % value3;     //TAKES THE MODULUS OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
176                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
177                 printf("\n");
178                 break;
179  
180       case '/': answerFinal = answer1 / value3;     //TAKES THE DIVISION OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
181                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
182                 printf("\n");
183                 break;
184  
185       case '*': answerFinal = answer1 * value3;     //TAKES THE MULTIPLICATION OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
186                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
187                 printf("\n");
188                 break;
189  
190       case '+': answerFinal = answer1 + value3;     //TAKES THE ADDITION OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
191                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
192                 printf("\n");
193                 break;
194  
195       case '-': answerFinal = answer1 - value3;     //TAKES THE SUBTRACTION OF ANSWER1 AND THE THRID INTEGER AND STORES IT IN ANSWERFINAL
196                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
197                 printf("\n");
198                 break;
199     }
200  
201   }
202  
203   else if (operator2 == '%' || operator2 == '/' || operator2 == '*')  //CHECKS TO SEE IF OPERATOR2 IS % OR / OR *
204   {
205  
206     switch(operator2)   //TAKES OPERATOR2 AS A PARAMETER TO BE COMPARED WITH CASES
207     {
208  
209       case '%': answer2 = value2 % value3;      //TAKES THE MODULUS OF THE SECOND 2 VALUES AND STORES IT IN ANSWER2
210                 break;
211  
212       case '/': answer2 = value2 / value3;      //TAKES THE DIVISION OF THE SECOND 2 VALUES AND STORES IT IN ANSWER2
213                 break;
214  
215       case '*': answer2 = value2 * value3;      //TAKES THE MULTIPLICATION OF THE SECOND 2 VALUES AND STORES IT IN ANSWER2
216                 break;
217     }
218 
219     switch(operator1)   //TAKES OPERATOR2 AS A PARAMETER TO BE COMPARED WITH CASES
220     {
221       case '%': answerFinal = value1 % answer2;     //TAKES THE MODULUS OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
222                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
223                 printf("\n");
224                 break;
225  
226       case '/': answerFinal = value1 / answer2;     //TAKES THE DIVISION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
227                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
228                 printf("\n");
229                 break;
230  
231       case '*': answerFinal = value1 * answer2;     //TAKES THE MULTIPLICATION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
232                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
233                 printf("\n");
234                 break;
235  
236       case '+': answerFinal = value1 + answer2;     //TAKES THE ADDITION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
237                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
238                 printf("\n");
239                 break;
240  
241       case '-': answerFinal = value1 - answer2;     //TAKES THE SUBTRACTION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
242                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
243                 printf("\n");
244                 break;
245     }
246  
247   }
248 
249   else if (operator1 == '+' || operator1 == '-')  //CHECKS TO SEE IF OPERATOR1 IS + OR -
250   {
251  
252     switch(operator1)   //TAKES OPERATOR1 AS A PARAMETER TO BE COMPARED WITH CASES
253     {
254       case '+': answer1 = value1 + value2;      //TAKES THE ADDITION OF THE FIRST 2 INTEGERS AND STORES IT IN ANSWER1
255                 break;
256  
257       case '-': answer1 = value1 - value2;      //TAKES THE SUBTRACTION OF THE FIRST 2 INTEGERS AND STORES IT IN ANSWER1
258                 break;
259     }
260 
261     switch(operator2)   //TAKES OPERATOR2 AS A PARAMETER TO BE COMPARED WITH CASES
262     {
263       case '+': answerFinal = answer1 + value3;     //TAKES THE ADDITION OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
264                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
265                 printf("\n");
266                 break;
267  
268       case '-': answerFinal = answer1 - value3;     //TAKES THE SUBTRACTION OF ANSWER1 AND THE THIRD INTEGER AND STORES IT IN ANSWERFINAL
269                 printf("(%d %c %d) %c %d = %d", value1, operator1, value2, operator2, value3, answerFinal);
270                 printf("\n");
271                 break;
272     }
273  
274   }
275 
276   else if (operator2 == '+' || operator2 == '-')  //CHECKS TO SEE IF OPERATOR2 IS + OR -
277   {
278  
279     switch(operator2)   //TAKES OPERATOR2 AS A PARAMETER TO BE COMPARED WITH CASES
280     {
281       case '+': answer2 = value2 + value3;      //TAKES THE ADDITION OF THE SECOND 2 INTEGERS AND STORES IT IN ANSWER2
282                 break;
283  
284       case '-': answer2 = value2 - value3;      //TAKES THE SUBTRACTION OF THE SECOND 2 INTEGERS AND STORES IT IN ANSWER2
285                 break;
286     }
287  
288     switch(operator1)   //TAKES OPERATOR1 AS A PARAMETER TO BE COMAPRED WITH CASES
289     {
290       case '+': answerFinal = value1 + answer2;     //TAKES THE ADDITION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
291                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
292                 printf("\n");
293                 break;
294  
295       case '-': answerFinal = value2 - answer2;     //TAKES THE SUBTRACTION OF THE FIRST INTEGER AND ANSWER2 AND STORES IT IN ANSWERFINAL
296                 printf("%d %c (%d %c %d) = %d", value1, operator1, value2, operator2, value3, answerFinal);
297                 printf("\n");
298                 break;
299     }
300  
301   }
302 
303   else; //CLOSES IF/ELSEIF/ELSE STATEMENT
304 
305 }


#5 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:02:28 PM

Posted 07 March 2011 - 11:00 PM

...

Edited by altair05, 07 March 2011 - 11:02 PM.


#6 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:28 PM

Posted 11 March 2011 - 05:38 PM

I still think you would have better code if you had a function to handle a single Value Operation Value set and called it as many times as was necessary. If you needed to expand this code for 3 operations there would be hundreds of lines of code just to get at the combinations, and that excludes the possibility that you might want to process a flexible number of operations. Still, it's good that you've got something working.
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users