Xcode8 - Program is not executing correctly - c++

While using Xcode8 says that it is running but there is no output.
Output:
10
Please enter the rectangle's length:
10
Please enter the rectangle's width:
The length for the rectangle is 10.00
The width for the rectangel is 10.00
The area for a rectangle is 100.00
The radius for a circle is 10.00
The area for a circle is 314.16
Program ended with exit code: 0
Wanted Output:
Please enter the rectangle's length: 10
Please enter the rectangle's width: 10
The length for the rectangle is 10.00
The width for the rectangel is 10.00
The area for a rectangle is 100.00
The radius for a circle is 10.00
The area for a circle is 314.16
Program ended with exit code: 0
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Function prototypes
double getLength(); // Function that receives the length
double getWidth(); // Function that receives the width
double getArea(double); //Function calculates area for circle
double getArea(double,double); // Function calculates area for rectangle
void displayData(double,double,double,double, double);
// displayData function is used to display values
int main() {
// Declares variables
double length, width, circleArea, rectangleArea;
length = getLength(); // Stores length
width = getWidth(); // Stores width
circleArea = getArea(width); // Stores area of circle
rectangleArea = getArea(width, length); // Stores are of rectangle
// Displays the length, width, area of circle, and rectangle area
displayData(length, width, rectangleArea, width, circleArea);
return 0;
}
/* This function asks the user to enter the rectangle's length and
then returns that values as a double */
double getLength() {
double length;
cout << "Please enter the rectangle's length: ";
cin >> length; // User input is stored in variable length
cout << "\n";
/* While loop doesn't let user input a
negative value or a value of zero for
the length of the rectangle. */
while (length <= 0) {
if (length < 0) { //if statement used when a negative value is entered for length
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Length of the Rectangle: ";
// Asks the user to enter the length again
cin >> length; // Stores value of length
if (length > 0) {
cout << "\n";
}
}
if (length == 0) { // If statement used when a value of zero is entered for length
cout << "INVALID INPUT!" << endl;//error message
cout << "Please enter a positive value for the Length of the Rectangle: ";
// Asks the user to enter the length again
cin >> length;// Stores the value of length
if (length > 0) {
cout << "\n";
}
}
}
return length;
}
/* This function asks the user to enter the rectangle's width and
then returns that values as a double */
double getWidth() {
double width;
cout << "Please enter the rectangle's width: ";
cin >> width; // User input is stored in variable width
cout << "\n";
/* While loop doesn't let user input a
negative value or a value of zero for the
width of the rectangle. */
while (width <= 0) {
if (width < 0) { // If statement used when a negative value is entered for width
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Width of the Rectangle: ";
// Asks the user to enter the width again
cin >> width; //Stores the value of width in the variable, width
if (width > 0) {
cout << "\n";
}
}
if (width == 0) { // If statement used when a value of zero is entered for width
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Width of the Rectangle: ";
// Asks the user to enter the width again
cin >> width; // Stores the value of width in the variable, width
if (width > 0)
{
cout << "\n";
}
}
}
return width;
}
double getArea(double radius) {
const double PI = 3.14159;
double circleArea;
circleArea = PI * pow(radius, 2); // Formula for the area of a circle
return circleArea;
}
/* This function accepts the rectangle's length and width as arguments and returns
the rectangle's area. The area is calculated by multiplying the length by the
width. */
double getArea(double length, double width) {
double rectangleArea;
rectangleArea = length * width; // Formula for the area of a rectangle
return rectangleArea;
}
/* This function accepts the rectangle's length, width, and area as arguments
and displays them in an appropriate message on the screen */
void displayData(double l, double w, double ra, double r, double ca) {
cout << setprecision(2) << fixed;
cout << "The length for the rectangle is " << l << endl;
cout << "The width for the rectangle is " << w << endl;
cout << "The area for a rectangle is " << ra << endl << endl;
cout << "The radius for the circle is " << r << endl;
cout << "The area for the circle is " << ca << endl;
}

When you use << with cout, it doesn't immediately push to the screen. A common way to send the output, as you've shown, is endl. If you don't want a line return, use flush.
cout << "Please enter the rectangle's length: " << flush;
EDIT: Xcode 8.3 seems to have new rules about cin and cout on the same line.

Related

Segmentation fault in function get_dimensions() when scanning variable into a structure pointer

I've spent upwards of 12 hours straight trying to figure out what I'm doing wrong and have been working on this program on and off for about a week now. Assignment is due early tomorrow morning, any help at all would be appreciated, I'm really desperate to pass.
When debugging, I found that the program breaks when I scan for data for p_rectangle->length/width, I'm sure it has something to do with how I set up main() or memory allocation, but I just can't figure it out.
Thanks guys.
//**********************************************************************
//* *
//* This program calculates and prints the area and perimeter of a *
//* rectangle and then draws a representation of the proportionality *
//* between length and width as a rectangle with a minimum length *
//* and width of 1. *
//* *
//**********************************************************************
#include <iostream>
using namespace std;
//**********************************************************************
//* Symbolic Constants *
//**********************************************************************
#define COURSE_NAME "Object-Oriented Programming and Design"
// Assigned course name
#define COURSE_NUMBER "CS246" // PCC assigned course number
#define MINIMUM_RECTANGLE_LENGTH 1.0 // Program preset range maximum
#define MINIMUM_RECTANGE_WIDTH 1.0 // Program preset range minimum
#define PROGRAM_NUMBER "1" // Teacher assigned program number
#define RECTANGLE_BORDER "*" // Rectangle border character
#define RECTANGLE_CENTER " " // Rectangle center character
//**********************************************************************
//* Program Structures *
//**********************************************************************
// Measurements specifications of a rectangle
struct rectangle
{
float area,
length,
perimeter,
width;
};
//**********************************************************************
//* Function Prototypes *
//**********************************************************************
void print_heading ();
// Print the program heading
void get_dimensions(struct rectangle *p_rectangle);
// Get the rectangle length and heigth and calculate the area and perimeter
void draw_rectangle(float length, float width);
// Draw a picture of a rectangle
void print_rectangle_specifications(struct rectangle *p_rectangle);
// Print the area, perimeter, width, and length
//**********************************************************************
//* Main Function *
//**********************************************************************
int main()
{
struct rectangle *p_rectangle; // Points to measurements specifications of a rectangle
// Print the program heading
print_heading ();
// Get the rectangle length and height and calculate the area and perimeter
get_dimensions(p_rectangle);
// Draw a picture of a rectangle
draw_rectangle(p_rectangle->length, p_rectangle->width);
// Print the area, perimeter, width, and length
print_rectangle_specifications(p_rectangle);
// Say goodbye and terminate the program
cout << "\n\nThanks for drawing a rectangle today ;)";
cout << "\n\n\n\n\n\n";
free(p_rectangle);
return 0;
}
//**********************************************************************
//* Print the program heading *
//**********************************************************************
void print_heading()
{
cout << "\n\n\n\n\n\n" << COURSE_NUMBER
<< " " << COURSE_NAME
<< " - Program " << PROGRAM_NUMBER;
cout << "\n\n This program draws a rectangle";
cout << "\n ==============================";
return;
}
//**********************************************************************
//* Get the length and width of your rectangle *
//**********************************************************************
void get_dimensions(struct rectangle *p_rectangle)
{
cout << "\n\nEnter the rectangle length now: ";
cin >> p_rectangle->length;
cout << "\nEnter the rectangle width here: ";
cin >> p_rectangle->width;
p_rectangle->area = (p_rectangle->length * p_rectangle->width);
p_rectangle->perimeter = (p_rectangle->length + p_rectangle->width) * 2;
return;
}
//**********************************************************************
//* Draw a picture of the rectangle *
//**********************************************************************
void draw_rectangle(float length, float width)
{
int columns,
i_length = (int)length,
i_width = (int)width,
rows;
// Checks if rectangle length and width is less than the minimum length
// and width
if ((i_length < MINIMUM_RECTANGLE_LENGTH) && (i_width < MINIMUM_RECTANGE_WIDTH))
{
cout << "\n\n\nA " << i_length << " by " << i_width;
cout << " is too small to draw.";
cout << "\nIt must be at least size " << MINIMUM_RECTANGLE_LENGTH;
cout << " by " << MINIMUM_RECTANGLE_LENGTH << " (length by width).";
}
else
{
// Scales the length and width of the rectangle on a 1-to-1 scale
columns = i_length * 1.5,
rows = i_width;
// Prints the rectangle message
cout << "\n\nHere is a picture of your rectangle";
// Prints the top row of the rectangle
for (int top_row = 1; top_row <= columns; top_row++)
cout << RECTANGLE_BORDER;
// Prints all middle rows of the rectangle
for (int middle_row = 1; middle_row <= columns - 2; middle_row++)
cout << "\n" << RECTANGLE_BORDER;``
for (int middle_column = 1; middle_column <= rows; middle_column++)
{
cout << RECTANGLE_CENTER;
if (rows == middle_column)
cout << RECTANGLE_BORDER;
}
// Prints the bottom row of the rectangle
if (i_width > 1)
for (int bottom_row = 1; bottom_row <= columns; bottom_row++)
cout << RECTANGLE_BORDER;
}
return;
}
//**********************************************************************
//* Print the calculations of the rectangle *
//**********************************************************************
void print_rectangle_specifications(struct rectangle *p_rectangle)
{
cout << "\n\nThe rectangle specifications are:";
cout << "\n Length = " << p_rectangle->length << " units";
cout << "\n Width = " << p_rectangle->width << " units";
cout << "\n Area = " << p_rectangle->area << " sq. units";
cout << "\n Perimeter = " << p_rectangle->perimeter << " units";
return;
}
In main():
struct rectangle *p_rectangle;
There are two problems here:
You don't need to specify "struct" again when declaring a variable of type rectangle.
You should always define a variable before you use it. In the case with pointers, you can allocate it on the free store.
So, the solution is this:
rectangle *p_rectangle = new rectangle;

How can I enter the total_area formula in a c++? It shows these error

I am a newbie in C++ and I am quite confused in programmer - defined functions.
It shows these errors
40 [Error] cannot convert 'double' to 'double(double, double)' in assignment
40 [Error] assignment of function 'double total_area(double, double)'
I cannot enter total_area = cross_area + side_area;
If I try to remove the double, it results to more errors
I can't find any information in youtube or in google that seems helpful.
// P33_2.cpp This program illustrates the local and global variables and call-by-value.
// This program computes the side area and the cross section area of a cylinder
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)`enter code here`
{
total_area = cross_area + side_area;
return 0;
}
See improved working code below
in your code, you have not passed any argument to total_area function. How do you think you will calculate area without passing arguments.
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
double dcross_area = cross_area(r);
double dside_area= side_area(r,h);
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area(dcross_area, dside_area) << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)
{
return cross_area + side_area;
}

Area of figures(equaliteral triangle, square, penta, hexa, hepta, octa) using if-else (C++)

This is my first C++ programming practice. When I run this it displays "Enter the length of the side" and "Which figure's area are you calculating? ". I enter 3 and square but the only result I can get is "Unknown figure. Try again." I'm not sure why this happens. Maybe something’s not connected well..
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int Side;
float Area;
float sqrt2 = 1.414;
float sqrt3 = 1.732;
float sqrt4 = 2;
float sqrt5 = 2.236;
float cot = 2.077;
float pi = 3.141;
char figure;
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
cout << "Enter the length of the side: " << endl;
cin >> Side;
cout << "Which figure's area are you calculating? " << endl;
cin >> figure;
if(figure == equaliteral_triangle) {
Area = (sqrt3/4) * (Side * Side);
cout << "The area of triangle is, "<< Area << endl;
}
else if (figure == square) {
Area = (Side * Side);
cout << "The area of square is, "<< Area << endl;
}
else if (figure == pentagon) {
Area = (0.25 *(5 *(5 +(2*sqrt5)))) * (Side * Side);
cout << "The area of pentagon is, "<< Area << endl;
}
else if (figure == hexagon) {
Area = ((3*sqrt3)/2) * (Side * Side);
cout << "The area of hexagon is, "<< Area << endl;
}
else if (figure == heptagon) {
Area = (7/4) * (Side * Side) * cot;
cout << "The area of heptagon is, "<< Area << endl;
}
if (figure == octagon) {
Area = (2 * (1+ sqrt2)) * (Side * Side);
cout << "The area of octagon is, "<< Area << endl;
}
else {
cout << "Unknown figure. Try again." << endl;
}
}
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
You have declared these variables and haven't initialized them
IT should be
char equaliteral_triangle='t', square='s', pentagon='p', hexagon='h', heptagon='H', octagon='o';
You need to give some values to the char variables you defined. Without giving them some values they will remain undefined and therefore you will not match them. Note that these
char figure;
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
are variables. If you type square into the console, it will not match the square variable's value. So, first of all, work with the characters like this:
char figure;
char equaliteral_triangle = '3',square = '4',pentagon = '5',hexagon = '6',heptagon = '7',octagon = '8';
and when you are successful and the program works, refactor it so that it will use a switch-case instead of if-else if which is more elegant in this case, when you have characters.
You have no values in your chars which means they all are NULL and when you compare them with the value user entered they will not be equal.
I would recommend only using the number of sides only if you want to have only these figures.
Eg if you have 3 sides you know it would be a triangle so use the formula accordingly .

"Function call missing argument list"?

#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES //needed to include the math constants
#include <math.h>
#include <string> //needed to include texts
using namespace std;
double Volume(double Length, double Width, double Height)
{
double volume;
volume = Length*Width*Height;
return volume;
}
double Area(double Length, double Width, double Height)
{
double area;
area = 2 * Width*Length + 2 * Length*Height + 2 * Height*Width;
return area;
}
void DisplayData(double Length, double Width, double Height, double volume, double area)
{
cout << "For the width " << Width << ", the length " << Length << " and the Height " << Height << "; the volume of the box is " << volume << " and the surface area is " << area << ".";
}
int main()
{
double Length, Width, Height;
cout << "Welcome! This program will calculate the volume and surface area of a box. All this program needs is you to input the length, width and height of the box." << endl;
cout << "Please note that all meausurments are in meters." << endl;
cout << "Please insert a value for the length: " << endl;
cin >> Length;
cout << "Please insert a value for the width: " << endl;
cin >> Width;
cout << "Please insert a value for the height: " << endl;
cin >> Height;
cout << endl;
Volume;
Area;
DisplayData;
return 0;
}//end main
I am writing a program with functions but it gives me the error in the title. How exactly do I call functions? I don't really understand that part. Do you just write the name of the function or is there something else involved?
I guess you should call the functions like this
double volume = Volume(Length, Width, Height);
double area = Area(Length, Width, Height);
DisplayData(Length, Width, Height, volume, area);
instead of the three meaningless statements
Volume;
Area;
DisplayData;
Function name and variable name is clashing with each other. Compiler is treating volume variable as function and looking for arguments after it.
double volume;
Change it to
double dVolume;
dVolume = Length*Width*Height;
return dVolume;
Do similar changes for area as well.

My program is printing the wrong thing after completing, and I can't figure out why. C++

When the program is operating correctly, it is supposed to ask the user for a code (A, C ,D) and then ask for a Radius.
Which it does fine.
But when it's finished it is supposed to cout something that looks like "The area of a circle with radius 6.75 is 143.14"
But when I run mine, it prints "The area of a circle with radius 6.75 is A." I just can't seem to find what I'm doing wrong, and any help is greatly appreciated
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
'A' == Const * Radius;
'C' == 2 * Const * Radius;
'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << 'A' << endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << 'C' << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << 'D' << endl;
//output the result
return 0;
}
I think you better review the latest lesson in C++ first.
Here is the fix for your code:
double a = Const * Radius; //'A' == Const * Radius;
double c = 2 * Const * Radius; //'C' == 2 * Const * Radius;
double d = 2 * Radius; //'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << a /*'A'*/<< endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << c /*'C'*/ << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << d /*'D'*/ <<
endl;
'A', 'B', 'C' are values as well as 1, 3, 100, etc.
= is an assignment operator while == is compare operator.
Once you write something like 'A' == Radius; it will be evaluated to boolean (most likely false value) and that's it. In the same way you can write false; or 5; in your code. It does nothing at all.
You're not assigning the value but making it equal to that. by the you can assign a value to char.
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
/* NO need of this. you can't assign value to a char.
'A' == Const * Radius;
you have used == sign whic is not assigning
'C' = 2 * Const * Radius;
'D' = 2 * Radius;
*/
if (Code == 'A'){
float temp; //change
temp=Const * Radius; //change
cout << "The area of a circle with radius" << Radius << "is" << temp << endl;
}
else if (Code == 'C'){
float temp;
temp=2 * Const * Radius;
cout << "The circumference of a circle with radius" << Radius << "is" << temp<< endl;
}
else if (Code == 'D'){
float temp;
temp=2 * Radius;
cout << "The diameter of a circle with radius" << Radius << "is" << temp << endl;
}
//Hope this helps.
//output the result
return 0;
}