I am new to programming and working on an assignment for my intro CS class. We use Dev C++ v 4.9.9.2. I've been asked to write a program that will prompt the user to enter a simple expression in the form 'value operand value' (ex 6 * 8). The program will then check the expression for +, -, *, and / operands (using either a switch or if else statements) and based on these checks will echo the original expression to an output file along with the appropriate answer. The program is also supposed to check for unknown operators (such as $) and division by zero (an error message should be generated). Right now I have my output printing to the screen to test the program before I get into all that file I/O business, but I'm having trouble knowing which way to write the if else statements. I've included what I have so far:
include <iostream>
include <fstream>
include <iomanip>
using namespace std;
int main()
{
float operand1,
operand2,
total1,
total2,
total3,
total4;
Can mathematic symbols be used that way in an if statement? (The compiler doesn't seem to like it and won't let me run it). Should I assign certain numeric values for them instead...that doesn't seem to fit the original instructions for the program, however. I keep thinking I'm missing something glaringly obvious but I haven't done this before. Our instructor is very picky about every last detail, and I'm not sure what I'm doing wrong or what to do to fix it so that I can get the desired result. When I replaced the +,-,*,/ etc. with number values and tested the program, every combination I tried still resulted in the first cout statement displaying (with 123.5 and 59.3). I hope this makes sense, any assistance is greatly appreciated as it's due tomorrow and I'm at my wit's end with this darn thing!
Thank you!
Sapphiresoda aka Jenn
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-04
w.r.t. the = vs == issue, in equality expressions involving constants, if you place the constant on the left-hand side, the compiler will throw an error if you use assignment where you mean equality:
if (oper = '+') // Valid but usually wrong, compiler won't complain!
if( '+' = oper ) // Constant on LHS of assignment, compiler error - saves runtime pain!
It is just a tip, personally I seldom follow this advice, it is slightly counter-intuitive, but it is a good habit nonetheless. If you see someone produce such code, it is at least a sign that they are thinking about what they are doing more than teh average - good tip for job interviews!
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Also I too used ... system("PAUSE>NUL"); ... until a legend of programming told me it is a very silly thing to do as the same can be achieved with ( in my case getch() ) std::cin>> ... which doesnt cause so much overhead.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you both so much for your help - I have implemented both suggestions and they definitely solved my problem. :) Ugh I should have known about ==, we covered that...scary since there's an exam tomorrow. One last question if you've got time...our instructor wants our output for division by zero to look like this (ex 6 / 0 'Division by zero produces an unidentified result.') With my current code:
include <iostream>
include <fstream>
include <iomanip>
using namespace std;
int main()
{
float operand1,
operand2,
total1,
total2,
total3,
total4;
I think it's got to have something to do with the previous statement on which the nested if statement relies (the fact that I have have included a total in that one which isn't necessary in the division by zero part??)...but I'm not sure how to fix it - reverse things somehow? I guess I've been looking at this for so many hours I can't think anymore.
Oh and Jennbo is my unfortunate family nickname but I've learned to embrace it! ;)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am new to programming and working on an assignment for my intro CS class. We use Dev C++ v 4.9.9.2. I've been asked to write a program that will prompt the user to enter a simple expression in the form 'value operand value' (ex 6 * 8). The program will then check the expression for +, -, *, and / operands (using either a switch or if else statements) and based on these checks will echo the original expression to an output file along with the appropriate answer. The program is also supposed to check for unknown operators (such as $) and division by zero (an error message should be generated). Right now I have my output printing to the screen to test the program before I get into all that file I/O business, but I'm having trouble knowing which way to write the if else statements. I've included what I have so far:
include <iostream>
include <fstream>
include <iomanip>
using namespace std;
int main()
{
float operand1,
operand2,
total1,
total2,
total3,
total4;
}
Can mathematic symbols be used that way in an if statement? (The compiler doesn't seem to like it and won't let me run it). Should I assign certain numeric values for them instead...that doesn't seem to fit the original instructions for the program, however. I keep thinking I'm missing something glaringly obvious but I haven't done this before. Our instructor is very picky about every last detail, and I'm not sure what I'm doing wrong or what to do to fix it so that I can get the desired result. When I replaced the +,-,*,/ etc. with number values and tested the program, every combination I tried still resulted in the first cout statement displaying (with 123.5 and 59.3). I hope this makes sense, any assistance is greatly appreciated as it's due tomorrow and I'm at my wit's end with this darn thing!
Thank you!
Sapphiresoda aka Jenn
w.r.t. the = vs == issue, in equality expressions involving constants, if you place the constant on the left-hand side, the compiler will throw an error if you use assignment where you mean equality:
if (oper = '+') // Valid but usually wrong, compiler won't complain!
if( '+' = oper ) // Constant on LHS of assignment, compiler error - saves runtime pain!
if( '+' == oper ) // Constant on LHS, correct test, compiler happy, runtime happy.
It is just a tip, personally I seldom follow this advice, it is slightly counter-intuitive, but it is a good habit nonetheless. If you see someone produce such code, it is at least a sign that they are thinking about what they are doing more than teh average - good tip for job interviews!
Clifford
The test for equality is == not =, and you can get the value of a character by putting it between ' marks.
So for instance:
if (oper == '+')
would test the value of oper to see if it's the same as the value of a plus sign.
Also I too used ... system("PAUSE>NUL"); ... until a legend of programming told me it is a very silly thing to do as the same can be achieved with ( in my case getch() ) std::cin>> ... which doesnt cause so much overhead.
Thank you both so much for your help - I have implemented both suggestions and they definitely solved my problem. :) Ugh I should have known about ==, we covered that...scary since there's an exam tomorrow. One last question if you've got time...our instructor wants our output for division by zero to look like this (ex 6 / 0 'Division by zero produces an unidentified result.') With my current code:
include <iostream>
include <fstream>
include <iomanip>
using namespace std;
int main()
{
float operand1,
operand2,
total1,
total2,
total3,
total4;
}
...all output is correct except division by zero...I get this:
0 / 0 = -1.#J Division by zero produces an unidentified result.
How can I get rid of that -1.#J business? Thanks again...
Jennbo?!
O_o
You have everything you need to answer your own question. Think about the ordering or your source...
Soma
I think it's got to have something to do with the previous statement on which the nested if statement relies (the fact that I have have included a total in that one which isn't necessary in the division by zero part??)...but I'm not sure how to fix it - reverse things somehow? I guess I've been looking at this for so many hours I can't think anymore.
Oh and Jennbo is my unfortunate family nickname but I've learned to embrace it! ;)