C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 14th, 2007, 12:20 PM
clowkun clowkun is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: America
Posts: 53 clowkun Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 8 h 59 m 35 sec
Reputation Power: 0
Send a message via MSN to clowkun
Can't use a char variable in my equation

So, this chunk of program is a part of a bigger program, and what i need it to do, is be able to prompt the user for an operator, scan it into a variable called mod, then reproduce mod in an equation, but it gives me an error when i try to compile it

here is the code, thanks

Code:
#include <stdio.h>

main()
{
	char mod;
	int x, y, z;

	x = 5;
	y = 6;

	

	printf("Enter in an operator ---> ");
	scanf("%c", &mod);
	
	
	z = ((x) mod (y));  /* want this equation to 
work, I want x to be added, subtrated, whatever 'mod' i
s to y and save it in z */

	printf("\n");
	printf("%d\n", z);
	

	printf("\n");
	printf("%c\n", mod);
	return 0;
}


using Microsoft Visual 6.0 Pro, in a Windows XP Pro environment
and the error:
2\mod.c(17) : error C2146: syntax error : missing ')' before identifier 'mod'

Reply With Quote
  #2  
Old October 14th, 2007, 01:20 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
What you are trying to do is not legal; a char is not an operator (nor can it be cast to one).
You might investigate a switch statement to accomplish your task. Example:
Code:
switch (op) {
   case '+':
      return a + b;
   case '*':
      return a * b;

   /* and so on... */

   default:
      /* not a valid operator */
}
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old October 15th, 2007, 03:34 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
Expecting the language to be able to do that is very wishful thinking!

If you want to evaluated user entered expressions you need to provide your own expression evaluator. You need ot interprete user input, but C is compiled language, it has no capability to interpret runtime input. A simple solution to the problem you posted is possible, as described by L7Sqr, but if you need something more flexible, a more complete expression evaluator might be in order: http://www.arstdesign.com/articles/...evaluation.html

Clifford

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Can't use a char variable in my equation

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap