5 Replies - 443 Views - Last Post: 09 September 2009 - 11:16 AM Rate Topic: -----

#1 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Really simple program but wrong result

Post icon  Posted 09 September 2009 - 11:02 AM

Hello there.Im really a beginner in C since Im only using Java.
But I have some assigments to do.So here is an example that I tried to execute but I get the wrong result.
Im sure it has something to do with the initiation.(or perhaps im wrong :P )

#include <stdio.h>

main(){
int a=0,b=0,result=0;
char c='';

printf("Give an expression:");  // where expression is something like 5+5
scanf("%f %c %f",&a,&c,&b);

switch(c){
	case '+': result = a + b;
			  break;
		case '-': result = a - b;
				  break;
		case '*': result = a * b;
				  break;
		default: result = 0;

}

printf("The result is  %d",result);

}



It must be something simple.Thanx in advance for every tip :)

Is This A Good Question/Topic? 0
  • +

Replies To: Really simple program but wrong result

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 428
  • Joined: 28-September 08

Re: Really simple program but wrong result

Posted 09 September 2009 - 11:07 AM

 
   int a=0,b=0,result=0;
   char c; 

   printf("Give an expression:");  // where expression is something like 5+5
   scanf("%d %c %d",&a,&c,&b);


So here are the changes.
Just declare c. No need to set it to empty or null.
%d is an integer. %c for char. Sorry about the confusion.
Also your switch statement can not evaluate strings or chars. Only integers.

This post has been edited by trixt.er: 09 September 2009 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Really simple program but wrong result

Posted 09 September 2009 - 11:08 AM

scanf for integers is %d


trixt.er %c need to remain, he only wants a single char, not a series of chars

This post has been edited by KYA: 09 September 2009 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Really simple program but wrong result

Posted 09 September 2009 - 11:12 AM

Damn.I missed that.Im a noob :angry:
Thnx guys. <3
Was This Post Helpful? 0
  • +
  • -

#5 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Really simple program but wrong result

Posted 09 September 2009 - 11:13 AM

Yeah, so use

scanf("%d %c %d",&a,&c,&b);


Was This Post Helpful? 0
  • +
  • -

#6 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 428
  • Joined: 28-September 08

Re: Really simple program but wrong result

Posted 09 September 2009 - 11:16 AM

View PostKYA, on 9 Sep, 2009 - 10:08 AM, said:

scanf for integers is %d
trixt.er %c need to remain, he only wants a single char, not a series of chars

Sorry about the confusion. Still waking up. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1