-3

I wrote a project that calculates the area of the triangle. I have wrote this program.

#include <iostream>

using namespace std;

int main()
{
int first, two, three;
int all = (first + two) * three;
cout << "Enter first num: ";
cin >> first;
cout << "\nEnter second num: ";
cin >> two;
cout << "\nEnter num three: ";
cin >> three;
cout << "You have choosed to do: (" << first << " + " << two << ") * " << three;
cout << "\n\nThis is equal to: " << all;

return 0;

}

Instead of going down a line and writing cin and cout every time, is there a way to shorten this project and make it shorter. Maybe like write the cout and the cin in a single line ? or anything else just to make it look more clean and nice.

it looked messy.

Thanks!

  • 3
    You need to make it correct before shortening, note you calculate `all` at the wrong place – Slava Nov 20 '19 at 16:35
  • Before even trying to shorten it, are you sure that this works? From what I'm reading, `all` should be some junk value every time you run this. Have you checked to be sure this works? Also, why do you *want* to shorten it? Short code != good/clean code. – scohe001 Nov 20 '19 at 16:35
  • 2
    Any shortening of this program would give you the opposite of "clean and nice". Besides, your program doesn't work yet, you should test it and fix issues before thinking of refactoring. Clean code means nothing when it's not working. – Yksisarvinen Nov 20 '19 at 16:36
  • 1
    There is https://codereview.stackexchange.com/, but firstly, how do you think you can calculate `all` **before** you have read the values for `first`, `two`, and `three`?! – BoBTFish Nov 20 '19 at 16:37
  • @BoBTFish Oh, my fault. You are all right, the all integer need to be at the end, Thanks! –  Nov 20 '19 at 16:40
  • you can put all code on a single line, that will make it shorter, but clean and nice not. Imho your code is as clean and nice as it can be (`using namespace std;` aside), it would rather worry about correctness... – 463035818_is_not_an_ai Nov 20 '19 at 16:40
  • Why do you want to shorten this tiny program? – Lightness Races in Orbit Nov 20 '19 at 16:53
  • Hi @MasterBootRecord, welcome to stackoverflow! It's good to write short code where it makes it clearer, and it's always a great thing to consider. And making it look pretty is a worthy consideration too - again as long as it makes it clearer. Clarity is everything!! – noelicus Nov 20 '19 at 16:55
  • @LightnessRaceswithMonica Hi, I just want to learn how can i shorten a program that contains `cout` and `cin` for example, and not write them in a different line every time i want to use them. –  Nov 20 '19 at 18:03

3 Answers3

1

This takes a whole 1 line fewer. Whether it's cleaner or easier to understand is up to you ....

int sides[3];
for (int i=0; i < 3; i++)
{
  cout << "Enter side " << i+1 << endl;
  cin >> sides[i];
}

It's good to write short code where it makes it clearer, so do keep considering how you can do that. Making it look pretty is a worthy consideration too - again as long as it makes what you're doing clearer.

Clarity is everything!!

noelicus
  • 14,468
  • 3
  • 92
  • 111
0

To make the code more maintainable and readable:

1) Use more meaningful variable names, or if you would name them consecutively, use an array

e.g. int numbers[3]

2) Similarly, when you are taking prompts like this, consider having the prompts in a parallel array for the questions, or if they are the same prompt use something similar to noelicus answer.

I would do something like this:

int numbers[3];
String prompts[3] = {"put your", "prompts", "here"};

for(int i=0; i<3; i++){
    cout << prompts[i] << endl;
    cin >> numbers[i]
}

//do math
//print output

also, you may want to check to make sure the user has entered a number using this.

0

If you really want to shorten your project and make it more "clean" you can do it this way.

#include <iostream>

int main()
{
   std::string str = "string"; std::cin >> str;
   return 0;
}

Like you wanted to write cout and cin in a single line, you could write like that, but I think it's not "clean" to write that way and it's better to drop a line.

And, please don't use using namespace std; its a bad practice.