-7

I wrote a program that implements a simple calculator. However, it does not compile. The compiler says that there are 22 errors, and I don't know why.

Desired behavior:

  1. Asks user about the desired operation
  2. Asks user about the parameters
  3. Outputs result

Specific problem or error:

Compilation errors at any occurrence of cin,cout, endl, case and break

Minimal, Complete, and Verifiable example:

#include <iostream>
int main()
{
    float area, r, l, h, b;
    int choice;
    cout<<"\n area of?";
    cout<<"\n[1]square \n[2]rectangle \n[3]circle \n[4]triangle"<<endl;
    cin>>choice;
    switch(choice);
    {
    case 1:
        cout<<"enter length"<<endl;
        cin>>l;
        area=l*l;
        cout<<area<<endl;
        break;
    case 2:
        cout<<"enter height"<<endl;
        cin>>h;
        cout<<"enter length"<<endl;
        cin>>l;
        area=l*h;
        cout<<area<<endl;
        break;
    case 3:
        cout<<"enter radius"<<endl;
        cin>>r;
        area=r*r*3.14;
        cout<<area<<endl;
        break;
    case 4:
        cout<<"enter height"<<endl;
        cin>>h;
        cout<<"enter breadth"<<endl;
        cin>>b;
        area=h*b*0.5;
        cout<<area<<endl;
        break;
    }

    return 0;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
Seto Kaiba
  • 19
  • 5
  • 6
    You screwed up the indentation. – Osiris Nov 06 '18 at 12:26
  • 1
    So it's up to us to guess what these 22 errors are??? – fvu Nov 06 '18 at 12:27
  • 3
    `switch(choice);` -> `switch(choice)`. No `;` after `switch`. – Osiris Nov 06 '18 at 12:27
  • Could you please share error messages? – kurtfu Nov 06 '18 at 12:28
  • "switch(choice)" remove semicolon ";" – Abhishek Nov 06 '18 at 12:29
  • 3
    1) "_but the compiler shows 22 errors_" Typically the errors tell you, exactly, what is wrong. Did you try reading them? 2) "_and i really don't understand the concept of scope too_" Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) then. – Algirdas Preidžius Nov 06 '18 at 12:29
  • Use `std::cout`, `std::cin` and `std::endl` for starters. – BluesSolo Nov 06 '18 at 12:29
  • @mjcs "_and `using namespace std` is missing_" [`using namespace std` is considered a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Algirdas Preidžius Nov 06 '18 at 12:31
  • 1
    Sorry, Seto, but _"the compiler shows 22 errors and i really don't understand the concept of scope too so yeah"_ is not really a valid problem description here. – Lightness Races in Orbit Nov 06 '18 at 12:32
  • 1
    @mjcs *slaps mjcs with paperroll* Bad contributor, bad! No developer-treats for you tonight! (`using namespace std` is usually considered [bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Let's avoid teaching beginners bad habits.) – CharonX Nov 06 '18 at 12:35
  • You seem to have misspelt `std::cout`, `std::cin`, and `std::endl`. But really, this is 22 questions in one - you should be able to reduce your code to just *one* error (and if you did so, you'd likely be able to fix it yourself!) – Toby Speight Nov 06 '18 at 15:44

1 Answers1

5

The are two errors (compile time errors, at least). First of all, cin, cout and endl are not known, you have to write them as std::cin, std::cout and std::endl.

The second problem is here:

switch (choice);

Remove that semicolon and it's fine. The reson why it's not working with the semicolon is because then switch (choice); is its own one and done deal, and the statements after it don't make sense without it.

Also, although it's not causing any compile time errors, I would highly recommend that you indent your code properly. mjcs edited the code you provided for you, it now looks much nicer and it is much easier to find the errors this way. In a big program, it is absolutely vital that the code is indented well, otherwise it's very hard to work with.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 2
    I'd drop the paragraph about `using namespace std;` - or at least word it more strongly that doing so is a very bad idea. (Let's teach beginners good habits, not bad ones. Someone may need to work with her/his code someday. It might be you...) – CharonX Nov 06 '18 at 12:39
  • Great idea, I got rid of that sentence altogether. – Blaze Nov 06 '18 at 12:39