ive been slaving over this code and i would appreciate if you could help me see the errors that are in this program.

this is a program that i have made with functions and switch statements
the goal is to make a program that will be a 'geometry calculator' and calculate the area for 3 diff geometric shapes. at the end the program should loop asking if i want to do more

this is my completed code

#include <iostream>  
#include <iomanip>  
#include <cmath> 
using namespace std;  
  
//  Function declarations  
char menu();  
void circle();  
void rectangle();
void triangle();
void quit();
  
//  Main function  
int main( int argc, char *argv[] )  
{  

  	//Declaration of Variables.
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;

  bool loop = true;  
  while( loop )  
  {  
    choice = menu();  
    switch (choice)  
    {  
      case '1':  
        circle();  
        break;
	  case '2':
        rectangle();
	  case '3':
        triangle();
	  case '4':  
        loop = false;  
        break;  
    }  
  }  
  return 0;  
}  
  
//************************
//FUNCTIONS              *
//************************

char menu()  
{  
    char temp;  
    cout << "\tGeometry Calculator" << endl << endl;  
    cout << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;  
    cout << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;  
    cout << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;  
    cout << "4. [Q]uit" << endl;  
  
    cout << setw(10) << "Enter your choice (1-4)\n";  
    cin >> temp;  
      
    return temp;  
  
}  
  
void circle()  
{  
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;
	
	switch (choice)
	{
	
		case 'C': 
		case 'c':
		case '1':
		
		cout << "Enter the radius of the circle: "; 
	    cin >> radius;
		
		if (radius < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			circleArea = PI * pow(radius, 2);
			cout << "The area of the circle is: " << circleArea;
		}
		
		cout << endl;
	
		break;
	}

}

void rectangle()
(
    double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;
	
	switch (choice)
	{

		case 'R':
		case 'r':
		case '2':
		
		cout << "Enter the length and width of the rectangle: ";
		cin >> length >> width;
		
		if (length < 0 || width < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			rectangleArea = length * width;
			cout << "The area of the rectangle is: " << rectangleArea;
		}
		
		cout << endl;

		break;
	}

}

void triangle()
{
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;

	switch(choice)
	{
		case 'T':
		case 't':
		case '3':
        
		cout << "Enter the base and height of the triangle: ";
	    cin >> base >> height;
 
		if (base < 0 || height < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else 
		{
			triangleArea = base * height * .5;
			cout << "The area of the triangle is: " << triangleArea;
		} 	
		
		cout << endl;
	
		break;
	
	}

}

void (quit)
{
	switch (choice)
	{
    case 'Q':
	case 'q':
	case '4':
		
		cout << "End of Program" << endl;
		cout << "Have a Great Day!" << endl;
	    cout << endl;
	
		break;
	} 
}

the compiler for some reasons comes up with 14 errors:

(19) : warning C4101: 'length' : unreferenced local variable
(18) : warning C4101: 'triangleArea' : unreferenced local variable
19) : warning C4101: 'base' : unreferenced local variable
18) : warning C4101: 'circleArea' : unreferenced local variable
(19) : warning C4101: 'radius' : unreferenced local variable
(19) : warning C4101: 'height' : unreferenced local variable
(19) : warning C4101: 'width' : unreferenced local variable
(18) : warning C4101: 'rectangleArea' : unreferenced local variable
(67) : warning C4101: 'length' : unreferenced local variable
(66) : warning C4101: 'triangleArea' : unreferenced local variable
(67) : warning C4101: 'base' : unreferenced local variable
(67) : warning C4101: 'height' : unreferenced local variable
(67) : warning C4101: 'width' : unreferenced local variable
(66) : warning C4101: 'rectangleArea' : unreferenced local variable
(100) : error C2061: syntax error : identifier 'rectangleArea'
(101) : error C2143: syntax error : missing ')' before ';'
(101) : error C2091: function returns function
(101) : error C2556: 'void (__cdecl *rectangle(void))(double)' : overloaded function differs only by return type from 'void rectangle(void)'
(9) : see declaration of 'rectangle'
(101) : error C2373: 'rectangle' : redefinition; different type modifiers
(9) : see declaration of 'rectangle'
(105) : error C2059: syntax error : 'switch'
(106) : error C2143: syntax error : missing ';' before '{'
(106) : error C2447: '{' : missing function header (old-style formal list?)
(130) : error C2059: syntax error : '}'
(130) : error C2143: syntax error : missing ';' before '}'
(130) : error C2059: syntax error : '}'
(133) : error C2143: syntax error : missing ';' before '{'
(133) : error C2447: '{' : missing function header (old-style formal list?)
(167) : error C2470: 'quit' : looks like a function definition, but there is no parameter list; skipping apparent body

Recommended Answers

All 9 Replies

void circle()  
{  
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;
	char choice;
	const double PI = 3.14159;
	
	switch (choice)
	{
	
		case 'C': 
		case 'c':
		case '1':

You're making a switch and not giving "choice" a value. It's just getting garbage.

I'm still reading through it, I'll edit this post if I find more problems.

char menu()  
{  
    char temp;  
    cout << "\tGeometry Calculator" << endl << endl;  
    cout << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;  
    cout << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;  
    cout << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;  
    cout << "4. [Q]uit" << endl;  
  
    cout << setw(10) << "Enter your choice (1-4)\n";  
    cin >> temp;  
      
    return temp;  
  
}

If you're basing your selections on a char, why are you asking for a number (1-4)? And someone correct me if I'm wrong but doesn't an entry of a number into a char return the ascii-equivalent?

Still reading...

should i set choice to an initial value of 0?

choice = 0;

switch (choice)

{

Oh my wow.

You are redeclaring every variable you already declared in main. You are re-doing a switch for choice unecessarily in every function, you do not need to do all these things. Let me give you some direction.. Give it about 4-5 mins I'll post some code.

see the different between my code and yours

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//  Function declarations
char menu();
void circle();
void rectangle1();
void triangle();


//  Main function
int main( int argc, char *argv[] )
{

	char choice;

  bool loop = true;
  while( loop )
  {
    choice = menu();
    switch (choice)
    {
      case '1':
        circle();
        break;
	  case '2':
        rectangle1();
        break;
	  case '3':
        triangle();
        break;
	  case '4':
        loop = false;
        break;
      default: cout << " ERROR " << endl;   
    }
  }
  return 0;
}

//************************
//FUNCTIONS              *
//************************

char menu()
{
    char temp;
    cout << "\tGeometry Calculator" << endl << endl;
    cout << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;
    cout << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;
    cout << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;
    cout << "4. [Q]uit" << endl;

    cout << setw(10) << "Enter your choice (1-4)\n";
    cin >> temp;

    return temp;

}

void circle()
{
	double circleArea, radius;
	const double PI = 3.14159;

		cout << "Enter the radius of the circle: ";
	    cin >> radius;

		if (radius < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else
		{
			circleArea = PI * pow(radius, 2);
			cout << "The area of the circle is: " << circleArea;
		}
}

void rectangle1()
{
    double area;
    double length, width;

		cout << "Enter the length and width of the rectangle: ";
		cin >> length >> width;

		if (length < 0 || width < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else
		{
			area = length * width;
			cout << "The area of the rectangle is: " << area;
		}

}

void triangle()
{
	double triangleArea,height, base;

		cout << "Enter the base and height of the triangle: ";
	    cin >> base >> height;

		if (base < 0 || height < 0)
		{
			cout << "Number must be greater than 0. Try again" << endl;
		}
		else
		{
			triangleArea = base * height * .5;
			cout << "The area of the triangle is: " << triangleArea;
		}

}

1) delete all those unreferenced variables. They just clutter up your code and compiler err/warning messages.


2) after completing 1) then you can see the real errors. One of them is that you used ( when you should have used { in the opening function brace. The other is that function quit is not declared correctly.

#include <iostream>  
#include <iomanip>  
#include <cmath> 
using namespace std;  
  
//  Function declarations  
char menu();  
void circle();  
void rectangle();
void triangle();
  
//  Main function  
int main()  
{  

  	//Declaration of Variables.
	
  char choice;

  bool loop = true;  
  while( loop )  
  {  
    choice = menu();  
    switch (choice)  
    {  
      case 'c':  
      case 'C':  // Circle
        circle();  
        break;
	  case 'r':
      case 'R':    
        rectangle();
        break;
      case 't':
	  case 'T':
        triangle();
        break;
	  case 'q':
      case 'Q':
        loop = false;              
        break;  
         
    }  
  }  
  return 0;  
}  
  
//************************
//FUNCTIONS              *
//************************

char menu()  
{  
    char temp;  
    cout << "\tGeometry Calculator" << endl << endl;  
    cout << setw(30) << "Calculate the Area of a [C]ircle" << endl;  
    cout << setw(30) << "Calculate the Area of a [R]ectangle" << endl;  
    cout << setw(30) << " Calculate the Area of a [T]riangle" << endl;  
    cout << "[Q]uit" << endl;  
  
    cout << setw(10) << "Enter your choice:";  
    cin >> temp;  
      
    return temp;  
  
}  
  
void circle()  
{  
	double circleArea, radius;
	const double PI = 3.14159;
	
	cout << "Enter the radius of the circle: "; 
	cin >> radius;
		
	if (radius < 0)
	{
		cout << "Number must be greater than 0. Try again" << endl;
	}
	else 
	{
		circleArea = PI * pow(radius, 2);
		cout << "The area of the circle is: " << circleArea;
	}
		
	cout << endl;
		
}

void rectangle()
{     // THIS BRACE WAS AN OPEN PARENTHESIS!!!!!!!!!!
  

}

void triangle()
{


}

Fill in the blanks ;)


ALSO ONE LAST THING:

When you do this:

//Declaration of Variables.
	double circleArea, rectangleArea, triangleArea,
		length, width, height, base, radius;

Compilers sometimes don't like it. (I know that gcc wasn't very happy about it). If you have to go two lines with variable declarations, try to just put another type identifier.

double circleArea, blah, blah2;
double blah3, blahgahblah1, blahgahblah2;

<3

^^thanks!!!
as you can see my skills in c++ arent that superb-unfortunatley i know c++ is not for me but i still have to finish this course (which is why im struggling)

Trust me, all of our first attempts were without doubt riddled with errors.

hi every one!
i m the new user of DAV-C++ 4.9.9.2 and i have a problem help me out of this. my problem is that when ever i double click on dav-c++ icon than a message appear on the screen

please help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.