473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help me switch to no if-else coding?

Hi all,

I'm working on an assignment and I've been asked to change my code so that I am not using any if or if else coding...

So as of now, my full code works perfecting but uses If Else and I've been trying everything but I can't seem to find a good alternative. Here's a small sample of what I have, I don't want to post the whole thing since I don't want my prof to flag me for plagiarism lol

....

case 2:
cout << endl;
cout << "\t\tCalculate the area of a Rectangle\n\n";
cout << "\tEnter length: ";
cin >> length;

cout << "\tEnter width: ";
cin >> width;
if (length <= 0 || width <= 0)
{
cout << endl;
cout << "\tEnter a positive value.\n\n";

}
else
{
RectArea = length * width;
cout << "\tArea= " << RectArea << " square units.";
}
break;
...

I've used this if else coding for the other sections (case 1 is the circle, case 2 is the rectangle, case 3 is the triangle). Just a basic geometry calculator...

I don't want anyone to do the work for me, I just really need help knowing what alternative I should use. I'm on thin ice with my prof, so I'd rather not ask him... thank you so so so much!
Sep 28 '15 #1
3 1336
The option I can think of is a ternary operator.
Sep 28 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
Try writing functions.

Expand|Select|Wrap|Line Numbers
  1. struct Rect
  2. {
  3.    int length;
  4.    int width;
  5.  
  6. };
  7.  
  8. Rect r;
  9.  
  10. GetData(&r);   //enter the data
  11. int area = CalcArea(&r);  
  12. DisplayArea(&area);
  13.  
Then you can introduce object based code:

Expand|Select|Wrap|Line Numbers
  1. Rect r;  //the object;
  2.      r.GetData();
  3.      r.CalacArea();
  4.      r.DisplayArea();
  5.  
The thing with if/else coding is that the logic threads don't stay separated. They intertwine to form spaghetti which s nearly impossible to understand.

This is enough hints for now.
Sep 28 '15 #3
donbock
2,426 Expert 2GB
Another option is to use a loop:
Expand|Select|Wrap|Line Numbers
  1. do {
  2.  length = width = 0;
  3.  cout << "\tEnter length: ";
  4.  cin >> length;
  5.  cout << "\tEnter width: ";
  6.  cin >> width;
  7.  } while ((length <= 0) || (width <= 0));
Sep 28 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: esd03eee | last post by:
I want to put in our arckives this information about the players , can we get help how to make a c++ code. We want it to be as what we wrote below. Iam really need a program for our fotballs teams...
2
by: Tony Girgenti | last post by:
Hello. I'm trying to develop a web comsuming client using VS.NET 2003(VB), .NET Framework 1.1.4322, ASP.NET 1.1.4322, WSE2.0 on a WinXP Pro Sp2 comuter. I want to use a web form for this...
4
mikeinspain
by: mikeinspain | last post by:
I am getting a Fatal Error message as shown below; Fatal error: Call to undefined function: phprequire() in /home/9144/domains/cbweb.co.uk/html/propertyEnquiry.php on line 1 The code behind this...
1
by: sxi12345 | last post by:
I need some help with a program im trying to code. The form consists of a listbox and three texts boxes and a button. Two texts boxes will be to enter a start value and an end value. The other...
4
by: Joe Befumo | last post by:
I've been a project manager for the past several years, and my programming chops are a bit rusty, so I'm soliciting help for a programming test for a job. (I don't feel at all dishonest for doing...
0
by: dotnetmadhu | last post by:
hai frnds this is madhu iam working on charts using ms office 2003 interfaced in VB.NET now my problem is iam able to draw a graph using some ponts but when i moved mouse on lines of path there is...
4
by: vikramjagan | last post by:
Hi, This is my first attempt at coding in microsoft .NET platform. I am trying to gather information on whether it is possible to code a program in .NEt (either VB.NET or C#.NET) to open a Tiff...
7
by: Amy Badgett | last post by:
I have been getting this error: "object doesn't support this object or method." The debug is saying the problem is in the series of if statements based on which product I enter in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.