GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 09-Sep-2008, 07:31
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

Re: Write a calculator program that lets users enter a simple mathematic operation


Erm .... I got another question now ....

CPP / C++ / C Code:
switch(compare){

	case '>':
	if(result>result2)
	printf("\nTRUE");
	else
	printf("\nFALSE");
	break;

	case '<':
	if(result<result2)
	printf("\nTRUE");
	else
	printf("\nFALSE");
	break;
	
	case '>=':
	if(result>=result2)
	printf("\nTRUE");
	else
	printf("\nFALSE");
	break;
How to do this part ? >= and <= , when i compile , the compiler say it is multi-character ...... How to do this part ?
Last edited by admin : 09-Sep-2008 at 18:02. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #22  
Old 09-Sep-2008, 23:15
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Write a calculator program that lets users enter a simple mathematic operation


Quote:
How to do this part ? >= and <= , when i compile , the compiler say it is multi-character ......
...well, that's because it IS multi character....and wrong!

Single quotes are used to enclose a SINGLE char integer value.
Check you book for what this ascii value is for these characters.

The combination of >= is TWO char integer values and so not suitable for single quotes. (double would be ok though (a C string))

So what you need is a method to check the characters individually but having a way to detect a certain combination.
This might be helpful:
CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
  char compare , str[] = " >= < = > ";  /* sample character array to mimic user input for test */
  int  i;

  printf("\nOur sample array is: \"%s\" \n", str);
  for(i = 0;  (compare = str[i]) != '\0'; i++)  /* '\0' is the special string ending "null" character*/
  {
    printf("i= %d:   ", i);
    switch(compare)
    {
      case '>':
        if( str[i + 1] == '=')  /* check for the next char in sting without incrementing */
        {  printf("do >= stuff   (including an extra incrementation of i)\n");
          i++;
        }
        else
          printf("just do > stuff (no extra incrementation) \n");
      break;      
      case '<' :
        printf(" what might we do here? < \n");
      break;
      case '=' :
        printf(" here we have a '=' by itself. \n");
      break;
      default:
        printf("space \n");
    }
  }
  return 0;
}
/*  For fun , look up all these characters in an ascii chart. (and/or print out thier values!  */
Now a word , if I may, suggest some space around operators and NOT crammed like this:

if(result>=result2)

Not only might it cause problems but it's hard to for old farts to read and just looks like poop.
Good coding is an art!
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An easy problem solve for most of you slapshot6819 C++ Forum 2 20-Nov-2007 03:14
Please help me to solve C++ problem fahad C++ Forum 1 01-Nov-2007 05:43
Problem Description: Graph’s Degree.....can help me to solve this out? thefinalyy C++ Forum 0 24-Aug-2007 06:42
I have a problem to solve summation in c++ logieen C++ Forum 4 22-May-2007 08:25
A problem using streams. (I've spent a month trying to solve it) Mararia C++ Forum 8 14-Dec-2006 17:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.