Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what wrong with this code? 1

Status
Not open for further replies.

neos

Programmer
Oct 28, 2000
36
CA
hey,
ive just started programming in c++, please go easy on me. ive tried to write a short calculator program, its just a simple console program, but im getting this error.

im compiling with borland 5.5 using "bcc32 calc.cpp"

#include <iostream>

using namespace std;

void add(float a, b) {
float c = a + b;
cout << c << endl;
}

void subtract(float a, b) {
float c = a - b;
cout << c << endl;
}

void multiply(float a, b) {
float c = a * b;
cout << c << endl;
}

void divide(float a, b) {
float c = a / b;
cout << c << endl;
}

int main() {
char x;
float a, b;

cout << &quot;shaun's calculator v0.1\n&quot;;
cout << &quot;please enter the first number\n&quot;;
cin >> a;
cout << &quot;please enter the second number\n&quot;;
cin >> b;

cout << &quot;+\n&quot;;
cout << &quot;-\n&quot;;
cout << &quot;*\n&quot;;
cout << &quot;/\n&quot;;

cin >> x;

if(x = +)
add(a, b)

if(x = -)
subtract(a, b)

if(x = *)
multiply(a, b)

if(x = /)
divide(a, b)

return 0;
}

the error i get this error:
Error E2303 calc.cpp 5: Type name expected
*** 1 errors in Compile ***

what wrong with this code?
 
In your function definitions (starting on line 5) you must explicitly specify both as float, you can't say float a,b you must use float a, float b.

Also, in the section where you are testing the operator, remember a VERY common C++ mistake is to use = when you mean ==. In addition, a character value must be placed in single quotes.

You should be using if(x == 'x') etc. in your condition tests.

Don't worry, you have a great start. These are very common mistakes for beginners. Keep going.
 
Jtm, Neos,

One technique that I've seen used in COM programming is to have the constant part of a comparison operator on the left side of the operator. That way if you forget the second equals-sign the compiler will flag the error for you (because it thinks you're trying to assign to a constant), rather than causing weird behavior at runtime.

So in Neos' code above, it could look like:
[tt]
if('+' == x)
add(a, b)
[/tt]

Sure, it looks a little strange, but you get used to it, plus you *really* get used to the compiler catching your typos. As an experiment, take away one of the equal signs and recompile. Blew up, didn't it?

Chip H.


 
firstly ,
comparision should be done using == opeartor because = is the asignment operator

secondly , function parameteres shoudl be explicitly defined so float a , float b not float a, b

and one more thing everyone is the beginner first time . But always prepare ur logic in the paper first and then go on with coding
 
Consider this...I just HAVE to show you, before you make yourself crash and burn ;)

-- Revised code follows --

#include <iostream.h>

// Define calculations
// This could be done with a typedef better
// but for simplicity, let's do it this way
// You'll see why i did this later
#define C_ADD 0
#define C_SUBTRACT 1
#define C_MULTIPLY 2
#define C_DIVIDE 3

// Local Functions - as long as these are here, you can
// have your routines in any order without your compiler
// complaining...though borland 5.5 doesn't...
float do_calculate(float cInputA, float cInputB,
int c_type);
int show_output();

// This statement isn't really needed here...
// useing namespace std;

// Main should do nothing but call and check routines
// The routines themselves should handle everything
// else
void main()
{
// As long as show_output() returns a value of 0,
// the program will not go any further than this
// statement
while(show_output() == 0);
}

int show_output()
{
char CalcType;
float InputA;
float InputB;
int redo_menu;
float FinalAnswer;

// Always initialize your variables as close to when
// You use them as possible
InputA = 0;
InputB = 0;
// Your &quot;Menu&quot; follows
cout << &quot;Shaun's calculator v0.1\r\n&quot;;
cout << &quot;Please enter the two numbers\r\n&quot;;
cin >> InputA >> InputB;

cout << &quot;+ - * /\r\nEnter calculation type: &quot;;

cin >> CalcType;

cout << &quot;\r\n\r\n&quot;;

// Notice how FinalAnswer gets it's value...any time
// you have something other than &quot;void&quot; infront of a
// routine (eg. float calculate() ) that means this
// routine is returning a value of the type that you
// put infront of this...in this case do_calculate()
// = cAnswer (see below in the routine)
// **MAKE SURE THE ROUTINE TYPE AND THE VARIABLE TYPE
// ARE THE SAME** eg. if you have int calculate, don't
// try to pass it into float FinalAnswer1
switch(CalcType)
{
case '+':
FinalAnswer = do_calculate(InputA, InputB, C_ADD);
redo_menu = 1;
break;
case '-':
FinalAnswer = do_calculate(InputA, InputB,
C_SUBTRACT);
redo_menu = 1;
break;
case '*':
FinalAnswer = do_calculate(InputA, InputB,
C_MULTIPLY);
redo_menu = 1;
break;
case '/':
FinalAnswer = do_calculate(InputA, InputB,
C_DIVIDE);
redo_menu = 1;
break;
default:
cout << &quot;That's not a valid calculation type.\r\n&quot;;
cout << &quot;Try again.\r\n&quot;;
redo_menu = 0;
break;
};
if (redo_menu == 1)
cout << InputA << &quot; &quot; << CalcType << &quot; &quot; << InputB
<< &quot; = &quot; << FinalAnswer << &quot;\r\n\r\n&quot;;

return redo_menu;
}

// do_calculate will accept 2 floating points, and an
// integer (c_type) which will determine the calculation
// to preform. Notice the modular design, each function
// does one thing, and does it well. Also, notice that
// I used different names for my variables...if this was
// a large program, and I used variable names like X,
// it would be pretty hard to figure out what it is.
// Try to keep the variable names descriptive. As long
// as you model your programs this way, they'll be easier
// to read, and a LOT less buggy. :) Good Luck
float do_calculate(float cInputA, float cInputB, int c_type)
{
float cAnswer;

// I used a switch here, as it is more convineint, and
// you don't have to worry about the =/== confusion ;)
switch(c_type)
{
// I could have actually used case 1, case 2, etc.
// as the C_ADD is equal to 1, but this way, if I
// ever want to add more, and have to change the
// numbers around, I just change #define C_ADD #,
// where # is the new number to use.
case C_ADD:
cAnswer = (cInputA + cInputB);
break;
case C_SUBTRACT:
cAnswer = (cInputA - cInputB);
break;
case C_MULTIPLY:
cAnswer = (cInputA * cInputB);
break;
case C_DIVIDE:
cAnswer = (cInputA / cInputB);
break;
// Just incase an invalid type is passed somehow
default:
cAnswer = 0;
cout << &quot;\r\n Error, no case taken!\r\n&quot;;
break;
};
return cAnswer;
}

Pay special attention to the notes in here, I recomend the
book &quot;Code Complete&quot;. It will teach you a lot about program/software construction...No matter what language you use, it's ALWAYS best to follow a format that everyone can READ, not figure out. I don't mean to critisize here, you're doing great, and I'm not even close to the greatest programmer, I just wanted to suggest this.

Good luck, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top