Want to know how to bypass strange result during runtime in VS2015 - c++

The following code asks the user to choose a shape, enter the dimensions for said shape, and display its volume.
It is used mostly of variable declarations and function calls, as this is required.
When I run the code I get the following output :
I dont understand why - nan (ind) appears instead of the result.
here is the full code:
#include <iostream>
using namespace std;
double height, width, length, radius, base_area, result;
//Function prototypes
int ReadInputShapeChoice();
void readshapedimension(int choice);
float CalculateBasicVolume(int choice);
void PrintResult(int choice);
double rectangular_solid(double length1, double width1, double height1);
double cylinder(double radius2, double height2);
double cone(double radius3, double height3);
double sphere(double radius4);
double square_based_pyramid(double height5, double base_area5);
//function definitions
double rectangular_solid(double length1, double width1, double height1)
{
double value;
value = (length1 * width1 * height1);
return value;
}
double cylinder(double radius2, double height2)
{
double value;
value = (3.14159 * (radius2 * radius2) * height2);
return value;
}
double cone(double radius3, double height3)
{
double value;
value = ((3.14159 * (radius3 * radius3) * height3) / 3);
return value;
}
double sphere(double radius4)
{
double value;
value = ((3.14159 * (radius4 * radius4 * radius4))*(4 / 3));
return value;
}
double square_based_pyramid(double height5, double base_area5)
{
double value;
value = ((height5 * base_area5) * (1 / 3));
return value;
}
int ReadInputShapeChoice()
{ int choice;
cout << "Choose what shape you want to calculate" << endl;
cout << "1 = Rectangular solid" << endl;
cout << "2 = Cylinder" << endl;
cout << "3 = Cone" << endl;
cout << "4 = Sphere" << endl;
cout << "5 = Square based pyramid" << endl;
cin >> choice;
return choice;
}
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
case 1:
{
result = rectangular_solid(length, width, height);
break;
}
case 2:
{
result = cylinder(radius, height);
break;
}
case 3:
{
result = cone(radius, height);
break;
}
case 4:
{
result = sphere(radius);
break;
}
case 5:
{
result = square_based_pyramid(height, base_area);
break;
}
return result;
}
}
void PrintResult(int choice)
{
switch (choice)
{
case 1:
{
cout << "The volume of the rectangular solid is " << result << endl;
break;
}
case 2:
{
cout << "the volume of the cylinder is " << result << endl;
break;
}
case 3:
{
cout << "The volume of the cone is " << result << endl;
break;
}
case 4:
{
cout << "The volume of the sphere is " << result << endl;
break;
}
case 5:
{
cout << "the volume of the square based pyramid is " << result << endl;
break;
}
}
}
int main() {
int choice;
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
PrintResult(choice);
return 0;
}
PLease can someone help me find a way to modify this code, so that it outputs the correct results ? Thankyou.

You're reading the radius and height into local variables declared in your switch statement. The global variables used in the calculation are never set, and have their default values of 0 in them. Your calculation results in a 0.0 / 0.0, which results in your NaN (Not a Number).

Related

Volume Shapes Calculator

I was trying out c++ function and recursion but it seems I'm having quite a problem in the variable. I left a comment below where the error usually is found. I dont know where is my problem hoping someone can help me point it out. Thank you! Is it the Variables? or the whole program is flawed? ;-;
#include <conio.h>
#include <iomanip>
#include <iostream>
using namespace std;
float Volume_Cube(float s);
float Volume_Sphere(float r);
float Volume_Rectangle(float w, float h, float l);
int main() {
char opt;
float V;
do {
do {
cout << "*******************************************************" << endl;
cout << " Mark Justine H. Monterde" << endl;
cout << " Volume of Shapes" << endl;
cout << "*******************************************************" << endl;
cout << " [C]-cube" << endl;
cout << " [S]-Sphere" << endl;
cout << " [R]-Rectangular Parallelepiped" << endl;
cout << " [X]-Exit" << endl;
cout << "*******************************************************" << endl;
cin >> opt;
switch (opt) {
case 'C':
case 'c':
float side;
cout << "Enter side: " << endl;
cin >> side;
V = Volume_Cube(side);
cout << fixed << setprecision(2);
cout << "The Volume of the cube is " << V << endl;
break;
case 'S':
case 's':
float radius;
cout << "Enter Radius: " << endl;
cin >> radius;
V = Volume_Sphere(radius);
cout << fixed << setprecision(2);
cout << "The Volume of the Sphere is " << V << endl;
break;
case 'R':
case 'r':
float width, height, length;
cout << "Enter Length: " << endl;
cin >> length;
cout << "Enter Width: " << endl;
cin >> width;
cout << "Enter Height: " << endl;
cin >> height;
V = Volume_Rectangle(width, height, length);
cout << fixed << setprecision(2);
cout << "The Volume of the Rectangular Parallelepiped is " << V
<< endl;
break;
case 'X':
case 'x':
return 0;
break;
default:
cout << "Denied...." << endl;
}
cout << "\nPress any key to continue...\n";
_getch();
} while (opt != 'X ' && opt != 'x');
cout << "\nThank you for using the program \n";
return 0;
}
float Volume_Cube(float s) { //<-mostly my errors comes from here//
float VOLUME;
VOLUME = s * s * s;
return VOLUME;
}
float Volume_Sphere(float r) {
float VOLUME;
const float PI = 3.14;
VOLUME = ((4 / 3) * PI * r * r * r);
return VOLUME;
}
float Volume_Rectangle(float w, float h, float l) {
float VOLUME;
VOLUME = w * h * l;
return VOLUME;
}
}
Here is your code again, with the compiler errors and warnings fixed. It now behaves as [mostly] expected. I placed comments where I made changes and marked them with the text CHANGE: . Your logic was sound, the syntax was just sloppy.
One area that I have not made changes to is how you exit. You immediately return, which means that your code below (thank you message, etc.) is never executed. Making those changes hinders this answer (in my opinion), so I'll leave it pointed out for you to work on. Suggestions are in the comments to this answer as of this writing.
// #include <conio.h> // CHANGE: Removed; outdated and not standard
#include <iomanip>
#include <iostream>
using namespace std;
float Volume_Cube(float s);
float Volume_Sphere(float r);
float Volume_Rectangle(float w, float h, float l);
int main() {
char opt;
float V;
// do { // CHANGE: Removed
do {
cout << "*******************************************************" << endl;
cout << " Mark Justine H. Monterde" << endl;
cout << " Volume of Shapes" << endl;
cout << "*******************************************************" << endl;
cout << " [C]-cube" << endl;
cout << " [S]-Sphere" << endl;
cout << " [R]-Rectangular Parallelepiped" << endl;
cout << " [X]-Exit" << endl;
cout << "*******************************************************" << endl;
cin >> opt;
switch (opt) {
case 'C':
case 'c':
float side;
cout << "Enter side: " << endl;
cin >> side;
V = Volume_Cube(side);
cout << fixed << setprecision(2);
cout << "The Volume of the cube is " << V << endl;
break;
case 'S':
case 's':
float radius;
cout << "Enter Radius: " << endl;
cin >> radius;
V = Volume_Sphere(radius);
cout << fixed << setprecision(2);
cout << "The Volume of the Sphere is " << V << endl;
break;
case 'R':
case 'r':
float width, height, length;
cout << "Enter Length: " << endl;
cin >> length;
cout << "Enter Width: " << endl;
cin >> width;
cout << "Enter Height: " << endl;
cin >> height;
V = Volume_Rectangle(width, height, length);
cout << fixed << setprecision(2);
cout << "The Volume of the Rectangular Parallelepiped is " << V << endl;
break;
case 'X':
case 'x':
return 0;
break;
default:
cout << "Denied...." << endl;
}
cout << "\nPress any key to continue...\n";
// _getch(); // CHANGE: Removed; Not found
} while (opt != 'X' && opt != 'x'); // CHANGE: Extra space after X removed
cout << "\nThank you for using the program \n";
return 0;
}
float Volume_Cube(float s) { //<-mostly my errors comes from here//
float VOLUME;
VOLUME = s * s * s;
return VOLUME;
}
float Volume_Sphere(float r) {
float VOLUME;
const float PI = 3.14;
VOLUME = ((4.0 / 3.0) * PI * r * r * r); // CHANGE: integers to doubles
return VOLUME;
}
float Volume_Rectangle(float w, float h, float l) {
float VOLUME;
VOLUME = w * h * l;
return VOLUME;
}
// } // CHANGE: Removed
Proper formatting, and enabling compiler warnings caught all of these without the need for any actual investigations.
Proper styling can be achieved in many ways. In my case, my editor (VS Code) performs some styling automatically like auto-indents. But I also back that up with a tool, clang-format.
For warnings, -Wall -Wextra should be considered a minimum. Some people might recommend a couple others to go with these, but I consider this a good minimum.
You are declearing functions inside main, that is a function itself, and this is illegal, try declearing them outside.

switch cases not opening, getting choice through return on a menu function

I am having trouble using a switch inside a do-while loop. menu is displaying, but after selection it is just displaying the menu again rather than opening up the proper switch case. and help would be greatly appreciated. I have tried looking for help and could not seem to find much.
#include <iostream>
using namespace std;
//function prototypes
int DisplayMenu(); //shows menu and returns input
double CalcAreaCircle(double radius ); //returns the area of the circle
double CalcAreaRectangle(double length, double width ); //returns the area of a rectangle
double CalcAreaTriangle(double base, double height ); //returns the area of a triangle
int Choice;
double AreaOfCircle;
double radius;
double AreaOfRectangle;
double length;
double width;
double AreaOfTriangle;
double base;
double height;
//function main
int main()
{
Choice = -1;
while (Choice != 4)
{
Choice = DisplayMenu();
switch (Choice)
{
case '1':
{
cout << "What is the radius of the circle?" << endl;
cin >> radius;
cout << endl;
AreaOfCircle = CalcAreaCircle(radius);
cout << endl << "The area of your circle is " << AreaOfCircle << endl;
break;
}
case '2':
{
cout << "what is the length of the rectangle?" << endl;
cin >> length;
cout << endl << "What is the width of the rectangle?" << endl;
cin >> width;
cout << endl;
AreaOfRectangle = CalcAreaRectangle(length, width);
cout << endl << "The area of your rectangle is " << AreaOfRectangle << endl;
break;
}
case '3':
{
cout << "What is the base of the triangle?" << endl;
cin >> base;
cout << endl << "What is the height of the triangle?" << endl;
cin >> height;
cout << endl;
AreaOfTriangle = CalcAreaTriangle(base, height);
cout << endl << "The area of your triangle is " << AreaOfTriangle << endl;
break;
}
}
}
system ("pause");
return 0;
}
//function DisplayMenu
int DisplayMenu()
{
int selection;
cout << "What would you like to know the area of?" << endl;
cout << "\t1. Area of a Circle." << endl;
cout << "\t2. Area of a Rectangle." << endl;
cout << "\t3. Area of a Triangle." << endl;
cout << "\t4. Quit." << endl;
cin >> selection;
while (selection < 1 || selection > 4)
{
cout << "Please enter a valid option." << endl;
cin >> selection;
cout << endl;
}
return selection;
}
//function CalcAreaCircle
double CalcAreaCircle(double radius)
{
double area;
const double PI = 3.14159;
area = PI * (area * area);
return area;
}
//function CalcAreaRectangle
double CalcAreaRectangle(double length, double width)
{
double area;
area = length * width;
return area;
}
//function CalcAreaTriangle
double CalcAreaTriangle(double base, double height)
{
double area;
area = base * height;
return area;
}
DisplayMenu() returns int. But your case statements are using char literals. When comparing char to int, it uses the character's code, e.g. case '1': is equivalent case 49:. Change your cases to use integer literals.
case 1:
and so on.

expected primary-expression before '*'?

I have looked through all of these and am relatively new to coding c++ and just don't know what I am missing. Any idea's?
My error occurs at line 45 "return pi * (Radius * Radius);" I am almost positive that the syntax for that line is correct but why am i getting compile errors.
#include <iostream>
#include <cstdlib>
using namespace std;
const double pi = 3.14159;
class Rectangle
{
protected:
float length, width;
public:
Rectangle(): length(0), width(0)
{
cout<<"Enter length: "; cin>>length;
cout<<"Enter width: "; cin>>width;
}
};
class Circle
{
protected:
float Radius;
public:
double radius;
Circle(): Radius(0)
{
cout<<"Enter Radius: "; cin>>Radius;
}
};
class Area : public Rectangle
{
public:
float getArea()
{
return length*width;
}
};
class Radius : public Circle
{
public:
float getRadius()
{
return pi * (Radius * Radius);
}
};
int main()
{
char choice;
for (int i = 0; i < 4; i++) //loop statement
{
cout << "Program to Find Area of a Square and Circle" << endl << //selection of which calculation to run
"Enter S for square square." << endl <<
"Enter C for circle." << endl <<
"Enter Q to Quit the program." << endl << endl <<
"Enter an option above: ";
cin >> choice;
switch(choice)
{
//Square option:
case 'S':
case 's': {
cout<<"Enter data for rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.getArea()<<" square\n\n";
break;}
//Circle option:
case 'C':
case 'c': {
cout<<"Enter data for circle to find radius.\n";
Radius c;
cout<<"Radius = "<<c.getRadius()<<" meter\n\n";
break;}
//Quit option:
case 'Q':
case 'q': {
cout << "Thank you for using Area Application" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
break;}
//default option binds to a non-selected choice function:
default:
cout << choice << " is not a valid selection." << endl;
cout << "Select a valid shape choice: S or C" << endl << endl;
break;
}
}
cout << "Press enter to continue ..." << endl;
return EXIT_SUCCESS;
}
Thanks
David
Now I see you have a member in base class which has a name Radius which is the same as in derived class, this is what is causing an error. The solution is to qualify it with base class name:
change:
return pi * (Radius * Radius);
to:
return pi * (Circle::Radius * Circle::Radius);
this additonal: double radius; is probably from some testing - right?
[edit]
From design point of view the existance of class Radius : public Circle makes little sense, it should be fine to just use Circle to get its radious.
#include <iostream>
#include <cstdlib>
using namespace std;
const double pi = 3.14159;
class Rectangle
{
protected:
float length, width;
public:
Rectangle(): length(0), width(0)
{cout<<"Enter length: "; cin>>length;cout<<"Enter width: "; cin>>width;}
float getArea(){return length*width;}
};
class Circle
{
protected:
float Radius;
public:
Circle(): Radius(0) {cout<<"Enter Radius: "; cin>>Radius;}
float getRadius() {return pi * (Radius * Radius);}
};
int main()
{
char choice;
for (int i = 0; i < 4; i++) //loop statement
{
cout << "Program to Find Area of a Square and Circle" << endl << //selection of which calculation to run
"Enter S for square square." << endl <<
"Enter C for circle." << endl <<
"Enter Q to Quit the program." << endl << endl <<
"Enter an option above: ";
cin >> choice;
switch(choice)
{
//Square option:
case 'S':
case 's': {
cout<<"Enter data for rectangle to find area.\n";
Rectangle a;
cout<<"Area = "<<a.getArea()<<" square\n\n";
break;}
//Circle option:
case 'C':
case 'c': {
cout<<"Enter data for circle to find radius.\n";
Circle c;
cout<<"Radius = "<<c.getRadius()<<" meter\n\n";
break;}
//Quit option:
case 'Q':
case 'q': {
cout << "Thank you for using Area Application" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
break;}
//default option binds to a non-selected choice function:
default:
cout << choice << " is not a valid selection." << endl;
cout << "Select a valid shape choice: S or C" << endl << endl;
break;
}
}
cout << "Press enter to continue ..." << endl;
return EXIT_SUCCESS;
}

Runtime error due to variables being local not global

The following code asks the user to choose a shape, enter the dimensions for said shape, and display its volume.
When I run the code I get the following output, showing me the result is not a number (NaN) :
I realise this must have something to do with the fact my variables are local not global, and that the following function calls:
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
Are not passing the required data. Have tried to get around this and put the variables in the correct place but to no avail, I am a beginner at using any form of coding language and found the whole program fairly challenging to create.
I have posted a similar question but this is much more detailed, if anyone can shed any light on how I modify the location of my variables in order for the program to run correctly I would be very grateful. Thank you.
The full code is:
#include <iostream>
using namespace std;
double height, width, length, radius, base_area, result;
//Function prototypes
int ReadInputShapeChoice();
void readshapedimension(int choice);
float CalculateBasicVolume(int choice);
void PrintResult(int choice);
double rectangular_solid(double length1, double width1, double height1);
double cylinder(double radius2, double height2);
double cone(double radius3, double height3);
double sphere(double radius4);
double square_based_pyramid(double height5, double base_area5);
//function definitions
double rectangular_solid(double length1, double width1, double height1)
{
double value;
value = (length1 * width1 * height1);
return value;
}
double cylinder(double radius2, double height2)
{
double value;
value = (3.14159 * (radius2 * radius2) * height2);
return value;
}
double cone(double radius3, double height3)
{
double value;
value = ((3.14159 * (radius3 * radius3) * height3) / 3);
return value;
}
double sphere(double radius4)
{
double value;
value = ((3.14159 * (radius4 * radius4 * radius4))*(4 / 3));
return value;
}
double square_based_pyramid(double height5, double base_area5)
{
double value;
value = ((height5 * base_area5) * (1 / 3));
return value;
}
int ReadInputShapeChoice()
{ int choice;
cout << "Choose what shape you want to calculate" << endl;
cout << "1 = Rectangular solid" << endl;
cout << "2 = Cylinder" << endl;
cout << "3 = Cone" << endl;
cout << "4 = Sphere" << endl;
cout << "5 = Square based pyramid" << endl;
cin >> choice;
return choice;
}
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
case 1:
{
result = rectangular_solid(length, width, height);
break;
}
case 2:
{
result = cylinder(radius, height);
break;
}
case 3:
{
result = cone(radius, height);
break;
}
case 4:
{
result = sphere(radius);
break;
}
case 5:
{
result = square_based_pyramid(height, base_area);
break;
}
return result;
}
}
void PrintResult(int choice)
{
switch (choice)
{
case 1:
{
cout << "The volume of the rectangular solid is " << result << endl;
break;
}
case 2:
{
cout << "the volume of the cylinder is " << result << endl;
break;
}
case 3:
{
cout << "The volume of the cone is " << result << endl;
break;
}
case 4:
{
cout << "The volume of the sphere is " << result << endl;
break;
}
case 5:
{
cout << "the volume of the square based pyramid is " << result << endl;
break;
}
}
}
int main() {
int choice;
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
PrintResult(choice);
return 0;
}}
You are re-declaring within your functions; this causes the functions to use their local values rather than the globals, as an example I've commented out the re-declarations in your reshapedimensions function:
// This is our global declaration
double height, width, length, radius, base_area, result;
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
// Take out our local declarations
// Otherwise cin below will write to the local and these values
// will subsequently be lost when the function exits
//int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
//int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
//int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
//int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
//int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
I notice, however, that you are using int within the function and double in your global declaration; if this was intentional you would need to include a conversion somewhere and store the result back to the globals.
It's a good idea to keep your global names unique so you won't get them confused with locals; common techniques are to use ALLCAPS or a prefix such as glob_myvar.
You are redefining the result variable.
At the top of your file you declare it;
double result;
But you never initialize it, instead you redefine it later and put the value you want to print out in this new variable;
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
The variable you print later is the uninitialized double and not the int which actually contains the volume of the cone.

How can I initialize the structure variable that is already being passed?

Okay so we have to change this program from reading input from a user to reading it off of a file, and so far i have changed a good chunk of the code that will read off of the file but every time i go to run the code i get these 5 errors that I can't figure out so here is my code
// Author:
// Source file:
// Description:
// Compiler used:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct records
{
int code;
double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream&);
void displayBal(records);
records getData(ifstream&);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);
//Global Constants
const double CHARGE = 10,
ATMFEE = 2;
int main()
{
//Variable Declarations
int transCode;
double balance,
transAmt;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
records trans;
ifstream inFile;
inFile.open("c:\\checkIn.dat");
displayTitle();
balance = getBegBal(inFile);
getData(inFile);
while (!inFile.eof())
{
trans = getData(inFile);
switch (trans.code)
{
case 1: balance = processCheck(balance, trans.amount); break;
case 2: balance = processDeposit(balance, trans.amount); break;
case 3: balance = processATM(balance, trans.amount); break;
}
displayBal(trans);
if (balance < 0)
balance = processSvcChg(balance);
getData(inFile);
}
return 0;
}
void displayTitle()
{
cout << "\n Check Register\n\n";
}
double getBegBal(ifstream& inFile)
{
//double bal;
records balance;
cout << " Enter beginning balance ";
inFile >> balance.amount;
return balance.amount;
}
void displayBal(records balance)
{
cout << "\t\tBalance = $" << setw(10) << balance.amount;
}
records getData(ifstream& inFile)
{
records rec;
cout << "\n\n Enter transaction code (0 to exit) ";
inFile >> rec.code;
if (rec.code > 0)
{
cout << "\n Enter transaction amount ";
}
return rec;
}
double processCheck(double bal, double amt)
{
cout << "\n Check = " << setw(10) << amt;
return (bal - amt);
}
double processDeposit(double bal, double amt)
{
cout << "\n Deposit = " << setw(10) << amt;
return (bal + amt);
}
double processATM(double bal, double amt)
{
records trans;
cout << "\n ATM = " << setw(10) << trans.amount;
bal = bal - amt;
displayBal(trans);
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
records trans;
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(trans);
return (bal);
}
error #2-3 are here
int transCode;
double balance,
transAmt;
the error is saying 'transCode': unreferenced local variable and
'transAmt': unreferenced local variable
errors #4-5 are here
double processATM(double bal, double amt)
{
records trans;
cout << "\n ATM = " << setw(10) << trans.amount;
bal = bal - amt;
displayBal(trans);// the error points here saying that the variable trans is uninitialized
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
records trans;
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(trans); // the error points here saying that the variable trans is uninitialized
return (bal);
}
Please and thank you for your help!
You initialized width = 0; and passed it to getWidth(). Therefore, width % 2 != 0 is evaluated as false and the prompt in getWidth() won't be displayed.
getWidth() won't need any arguments unless your assignment requires it because it is intended to just read the width and rethrn it.
do statement is useful to evaluate condition after executing loop body once.
int getWidth()
{
int width = 0;
do {
cout << "Enter width " << endl;
cin >> width;
} while (width % 2 != 0);
return width;
}
Then, use getWidth() in main() function like this:
width = getWidth();
It's working for me.
int displayMenu();
void displaySquare(int width);
void displayTriangle(int width);
int getWidth();
void displayUpsideDownTriangle(int width);
void displayDiamond(int width);
int main()
{
int width, shapes;
do {
shapes = displayMenu();
width = 0;
switch (shapes)
{
case 1:
width = getWidth();
displaySquare(width);
break;
case 2:
width = getWidth();
displayTriangle(width);
break;
case 3:
width = getWidth();
displayUpsideDownTriangle(width);
break;
case 4:
width = getWidth();
displayDiamond(width);
break;
case -9:
cout << "End of Program " << endl;
default:
cout << "Please choose one of the shapes..." << endl;
}
} while (shapes != -9);
system("pause");
return 0;
}
//this function sets up the display for the user
int displayMenu() {
int shapes;
cout << "\n~~ Shape Display menu ~~ " << endl << endl;
cout << " 1....Square\n" <<
" 2....Triangle\n " <<
" 3....Upside Down triangle\n " <<
" 4....Diamond\n\n " <<
" -9....Exit Program " << endl;
cout << endl;
cout << " Make a selection " << endl;
cin >> shapes;
return shapes;
}
int getWidth()
{
int width = 1;
do {
if (width % 2 != 0)
{
cout << "Enter width " << endl;
cin >> width;
}
else
{
cout << "Please enter odd number only. \nEnter width " << endl;
cin >> width;
}
} while (width % 2 == 0);
return width;
}
void displaySquare(int width)
{
int rows, columns;
for (rows = 0; rows < width; ++rows)
{
for (columns = 0; columns < width; ++columns)
{
cout << "# ";
}
cout << endl;
}
}
void displayTriangle(int width)
{
int rows, Spacing, ColHashtag;
for (rows = 1; rows < width; rows++) //controls the rows
{
for (Spacing = (width - rows); Spacing >= 1; Spacing--) // spaces out the rows to make an isoceles triangle
{
cout << " ";
}
for (ColHashtag = 1; ColHashtag <= (rows * 2) - 1; ColHashtag++) //controls the columns
{
cout << "#";
}
cout << endl;
}
}
void displayUpsideDownTriangle(int width)
{
int rows, Columns, spacing;
//sets up the rows for the top
for ((rows = width - 1); rows >= 1; rows--)
{
for (Columns = 1; Columns <= width - rows; Columns++) // sets up the columns
{
cout << " "; // spaces out the symbols to make an isoceles triangle
}
for (spacing = 1; spacing <= 2 * rows - 1; spacing++)
{
cout << "#";
}
cout << endl;
}
}
void displayDiamond(int width)
{
displayTriangle(width);
displayUpsideDownTriangle(width);
}
replace int main() and getWidth() with this
int main()
{
int width, shapes,wt;
do {
cout << "Enter width " << endl;
cin >> width;
wt=getWidth(width);
if(wt!=0)
{
shapes = displayMenu();
}
else
{
shapes=-9;
}
switch (shapes)
{
case 1:
displaySquare(wt);
break;
case 2:
displayTriangle(wt);
break;
case 3:
displayUpsideDownTriangle(wt);
break;
case 4:
displayDiamond(wt);
break;
case -9:
cout << "End of Program " << endl;
break;
default:
cout << "Please choose one of the shapes..." << endl;
}
} while (shapes != -9);
system("pause");
return 0;
}
int getWidth(int width)
{
while (width % 2 != 0) {
cout << "Enter width " << endl;
cin >> width;
}
return width;
}