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;
}
Related
I have an assignment here that I've been working on for the past few hours and have been met with a problem. Whenever I compile and run the program in VS Code it throws me into an infinite loop where the text of the "menu" is printed repeatedly. I imagine this has something to do with the for(;;){ loop at the beginning of the menu.
I've deleted the for(;;){ statement at the beginning of the menu, and the continuous scrolling stopped, but I was unable to input any numbers (cases) and the program, essentially, just printed the menu and that was the end.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student {
string name;
float GPA;
public:
string getName() const
{
return name;
}
float getGPA() const
{
return GPA;
}
void setName(string Name)
{
name = Name;
}
void setGPA(float gpa)
{
GPA = gpa;
}
Student(const string& name, float gpa)
: name(name)
, GPA(gpa)
{
}
Student()
{
}
void printDetails(Student s[], int n)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n; i++) {
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
float calcAverageGPA(Student s[], int n)
{
float avg = 0;
float sum = 0;
for (int i = 0; i <= n; i++) {
sum += s[i].getGPA();
}
avg = sum / n;
return avg;
}
float getGPAbyName(Student s[], int n, string name)
{
for (int i = 0; i <= n - 1; i++) {
if (s[i].getName() == name)
return s[i].getGPA();
}
return -1;
}
void showListByGPA(Student s[], int n, float gpa)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n - 1; i++) {
if (s[i].getGPA() > gpa)
;
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
};
int main()
{
int n = 0;
int ch;
Student s[50];
string name;
float GPA;
float avg = 0;
cout << "======================" << endl;
cout << "(1): Add a student" << endl;
cout << "(2): Print the details of a student" << endl;
cout << "(3): Get the GPA of a Student by name." << endl;
cout << "(4): Get names of students based on GPA." << endl;
cout << "(6): Quit the program." << endl;
cout << "Enter your option" << endl;
switch (ch) {
case '1':
cout << "\nEnter student's name" << endl;
cin >> name;
cout << "Enter GPA" << endl;
cin >> GPA;
s[n] = Student(name, GPA);
n++;
break;
case '2':
s[0].printDetails(s, n);
break;
case '3':
cout << "\nEnter the name of a student" << endl;
cin >> name;
GPA = s[0].getGPAbyName(s, n, name);
if (GPA == -1) {
cout << "\nStudent not found!" << endl;
}
else
cout << "\nGPA is: " << endl;
break;
case '4':
cout << "\nEnter GPA: " << endl;
cin >> GPA;
s[0].showListByGPA(s, n, GPA);
break;
case '5':
avg = s[0].calcAverageGPA(s, n);
cout << "\nAverage GPA: " << avg << endl;
break;
case '6':
exit(0);
}
}
I suspect the problem resides in main(). I included the prior blocks of the program in case they were necessary to provide any suggestions.
You obviously want to read in "ch".
You've also made this an int, and don't zero-initialize, which can cause some undefined behaviour if you don't set it beforehand.
The switch case should be
switch(X){
case 1:
// Code
break;
}
etc..
The difference is "1" or 1. It evaluates an enumeration value, instead of strings.
For safety: you might want to add a case default, which is common practice.
I was wondering if someone could help me on figuring out how to create multiple structs and also counting the number of structs created.
My Code follows three blocks, header file, Television file and then the main file.
Television.h
#include <iostream>
#include <string>
using namespace std;
class Televison {
string Manufacturer;
string Type; //Plasma, LCD
string Model; //Smart, regular
string Connection; //HDMI, VGA
string Power;
double Price;
int Serial_Number;
int Screen_size;
int Resolution; //larger one, so 1920 and etc.
int Channel; //any number
int Volume; //0 - 100
public:
//constructor
Televison(string Manufacturer, string Type, string Model, string Connection, string Power, double Price, int Serial_number, int Screen_size, int Resolution, int Channel, int Volume);
//Destructor
~Televison();
//Accessor Methods
string get_Manufacturer();
string get_Type();
string get_Model();
string get_Connection();
string get_Power();
double get_Price();
int get_Serial_Number();
int get_Screen_size();
int get_Resolution();
int get_Channel();
int get_Volume();
//mutator methods
string input_Manufacturer(string Manufacturer);
string input_Type(string Type);
string input_Model(string Model);
string input_Connection(string Connection);
string input_Power(string Power);
double input_Price(double Price);
int input_Serial_Number(int Serial_Number);
int input_Screen_size(int Screen_size);
int input_Resolution(int Resolution);
int input_Channel(int Channel);
int input_Volume(int Volume);
string change_Power(string Power);
int change_Channel(int Channel);
int change_Volume(int Volume);
};
static int num_tel = 1; //number of Televisons that will be incremented
Television.cpp
#include <iostream>
#include <string>
#include "Televison.h"
using namespace std;
//constructor
Televison::Televison(string Manufacturer, string Type, string Model, string Connection, string Power, double Price, int Serial_number, int Screen_size, int Resolution, int Channel, int Volume) {
this->Manufacturer = Manufacturer;
this->Type = Type;
this->Model = Model;
this->Connection = Connection;
this->Power = Power;
this->Price = Price;
this->Serial_Number = Serial_Number;
this->Screen_size = Screen_size;
this->Resolution = Resolution;
this->Channel = Channel;
this->Volume = Volume;
num_tel++;
// initiliazes the variables to default values
Manufacturer = "Sony";
Type = "Smart";
Model = "Plasma";
Connection = "HDMI";
Power = "ON";
Serial_Number = 1234;
Price = 199.00;
Screen_size = 50;
Resolution = 1440;
Channel = 100;
Volume = 100;
}
//Destructor
Televison::~Televison() {
cout << "The " << Manufacturer << " " << Model << " has finished shutting down." << endl;
num_tel--;
cout << "The inventory of television is now: " << num_tel << endl;
}
//Get Methods
string Televison::get_Manufacturer() {
return Manufacturer;
}
string Televison::get_Model() {
return Model;
}
string Televison::get_Type() {
return Type;
}
int Televison::get_Serial_Number() {
return Serial_Number;
}
string Televison::get_Connection() {
return Connection;
}
string Televison::get_Power() {
return Power;
}
double Televison::get_Price() {
return Price;
}
int Televison::get_Screen_size() {
return Screen_size;
}
int Televison::get_Resolution() {
return Resolution;
}
int Televison::get_Channel() {
return Channel;
}
int Televison::get_Volume() {
return Volume;
}
//mutator methods
int Televison::change_Channel(int Channel1) {
if (Channel > 0)
{
Channel = Channel1;
return Channel;
}
else
{
cout << "Invalid Channel, please enter another number" << endl;
return Channel;
}
}
int Televison::change_Volume(int Volume1) {
if (Volume1 > 0)
{
Volume = Volume1;
return Volume;
}
else
{
cout << "Invalid volume, please enter another number" << endl;
return Volume;
}
}
string Televison::change_Power(string Power1) {
if (Power1 =="Y")
{
Power = Power1;
return Power;
}
else
{
return Power;
}
}
string Televison::input_Connection(string Connection1) {
Connection = Connection1;
return Connection;
}
string Televison::input_Type(string M) {
Type = M;
return Type;
}
string Televison::input_Manufacturer(string M) {
Manufacturer = M;
return Manufacturer;
}
/*
string Televison::Manufacturer_code(string M,string N) {
string J = M.substr(0,4);
string Manufacturer_code = J + N;
return Manufacturer_code;
}
*/
string Televison::input_Model(string M) {
Model = M;
return Model;
}
string Televison::input_Power(string M) {
Power = M;
return Power;
}
double Televison::input_Price(double M) {
Price = M;
return Price;
}
int Televison::input_Serial_Number(int M) {
Serial_Number = M;
return Serial_Number;
}
int Televison::input_Screen_size(int M) {
Screen_size = M;
return Screen_size;
}
int Televison::input_Resolution(int M) {
Resolution = M;
return Resolution;
}
int Televison::input_Channel(int M) {
Channel = M;
return Channel;
}
int Televison::input_Volume(int M) {
Volume = M;
return Volume;
}
Source.cpp
#include <iostream>
#include <string>
#include "Televison.h"
using namespace std;
void displayStatus(Televison& Televison) {
cout << "Inventory of Televisons: " << num_tel << endl;
cout << "The Televison serial number: " << Televison.get_Serial_Number() << " " << Televison.get_Manufacturer() << " " << Televison.get_Model() << " " << Televison.get_Type() << " with " << Televison.get_Connection() << " ";
cout << "\n with " << Televison.get_Power() << " " << Televison.get_Price() << " with " << Televison.get_Screen_size() << " screen size " << Televison.get_Resolution() << "p " << "with " << Televison.get_Channel() << " channel " << Televison.get_Volume() << " Volume " << endl;
}
int main() {
{
Televison Televison_1("Sony", "Smart", "Plasma", "HDMI", "ON", 199.00, 1234, 50, 1440, 100, 100); //default Televison
cout << "This is the Televison program for personal Televison: " << endl;
bool repeat = true;
do {
cout << "Please enter the Manufacturer for the Televison: " << endl;
string Manufacturer;
getline(cin, Manufacturer);
int M1 = Manufacturer.length();
if (M1 < 1)
{
cout << "No input, please try again" << endl;
repeat = false;
}
else
repeat = true;
Televison_1.input_Manufacturer(Manufacturer);
} while (!repeat);
repeat = true;
do {
cout << "Please enter the Type of the Televison: " << endl;
string Type;
getline(cin, Type);
int M3 = Type.length();
if (M3 < 1)
{
cout << "No input, please try again" << endl;
repeat = false;
}
else
repeat = true;
Televison_1.input_Type(Type);
} while (!repeat);
repeat = true;
do {
cout << "Please enter the Model for the Televison: " << endl;
string Model;
getline(cin, Model);
int M2 = Model.length();
if (M2 < 1)
{
cout << "No input, please try again" << endl;
repeat = false;
}
else
repeat = true;
Televison_1.input_Model(Model);
} while (!repeat);
cout << "Please enter the Connection for the Televison: " << endl;
string Connection;
cin >> Connection;
Televison_1.input_Connection(Connection);
cout << "Enter the Serial Number: " << endl;
int inp4;
cin >> inp4;
Televison_1.input_Serial_Number(inp4);
cout << "Enter the Screen size: " << endl;
cin >> inp4;
Televison_1.input_Screen_size(inp4);
cout << "Enter the Resolution: " << endl;
cin >> inp4;
Televison_1.input_Resolution(inp4);
int a = 0;
while (a < 1)
{
cout << "Please enter the Price for the Televison: " << endl;
double Price;
cin >> Price;
if (Price > 0) //address speed must be greater than 0
{
Televison_1.input_Price(Price);
a++;
}
else
cout << "Invalid, enter again." << endl;
}
displayStatus(Televison_1);
int b = 0;
while (b < 1)
{
cout << "Would you like to Power your Televison? (Y/N)" << endl;
char input1;
cin >> input1;
if (input1 == 'Y' || input1 == 'y')
{
Televison_1.change_Power("ON");
b++;
}
else if (input1 == 'N' || input1 == 'n')
{
Televison_1.change_Power("OFF");
b++;
}
else
cout << "Invalid, enter again." << endl;
}
displayStatus(Televison_1);
int n = 0;
int n1 = 0;
while (n < 1)
{
cout << "Would you like to change any of the following attributes of your Televison? (Y/N)";
string input2, input3;
int input4;
double input5;
cin >> input2;
if (input2 == "Y" || input2 == "y")
{
while (n1 < 1) {
cout << "Which Televison attribute would you like to change? (Enter the respective number): " << endl;
cout << "1: Manufacturer" << endl;
cout << "2: Type" << endl;
cout << "3: Model" << endl;
cout << "4: Serial Number" << endl;
cout << "5: Connection" << endl;
cout << "6: Power" << endl;
cout << "7: Channel" << endl;
cout << "8: Screen size" << endl;
cout << "9: Resolution" << endl;
cout << "10: Volume" << endl;
cout << "11: Price" << endl;
int choice;
cin >> choice;
bool repeat1 = true;
bool repeat2 = true;
bool repeat3 = true;
switch (choice)
{
case 1:
cout << "Enter the Manufacturer: " << endl;
cin >> input3;
Televison_1.input_Manufacturer(input3);
break;
case 2:
cout << "Enter the Type: " << endl;
cin >> input3;
Televison_1.input_Type(input3);
break;
case 3:
cout << "Enter the Model: " << endl;
cin >> input3;
Televison_1.input_Model(input3);
break;
case 4:
cout << "Enter the Serial Number: " << endl;
cin >> input4;
Televison_1.input_Serial_Number(input4);
break;
case 5:
cout << "Enter the Connection: " << endl;
cin >> input3;
Televison_1.input_Connection(input3);
break;
case 6:
cout << "Enter the power: " << endl;
cin >> input3;
Televison_1.input_Power(input3);
break;
case 7:
cout << "Enter the Channel " << endl;
cin >> input4;
Televison_1.input_Channel(input4);
break;
case 8:
cout << "Enter the Screen size: " << endl;
cin >> input4;
Televison_1.input_Screen_size(input4);
break;
case 9:
do {
cout << "Enter the Resolution: " << endl;
cin >> input4;
if (input4 < 0)
{
cout << "Please enter a speed above 0" << endl;
repeat1 = false;
}
else
{
Televison_1.input_Resolution(input4);
repeat1 = true;
}
} while (!repeat1);
break;
case 10:
do {
cout << "Enter the Volume: " << endl;
cin >> input4;
if (input4 >= 0) {
cout << "Please enter a valid volume" << endl;
repeat2 = false;
}
else
{
repeat2 = true;
Televison_1.change_Volume(input4);
}
} while (!repeat2);
break;
case 11:
do {
cout << "Enter the Price: " << endl;
cin >> input5;
if (input5 > 0)
{
Televison_1.input_Price(input5);
repeat3 = true;
}
else
{
cout << "Please enter a valid price" << endl;
repeat3 = false;
}
} while (!repeat3);
break;
}
displayStatus(Televison_1);
cout << "Would you like to change another attribute? (Y/N)" << endl;
cin >> input3;
if (input3 == "Y" || input3 == "y")
{
}
else
n1++;
}
n++;
}
else if (input2 == "N" || input2 == "n")
{
cout << "The Televison will begin to shut down." << endl;
n++;
displayStatus(Televison_1);
}
else
cout << "Invalid, enter again." << endl;
}
}
// object goes out of scope - the destructor which shuts down the Televison
system("pause");
return 0;
}
Basically, my code only allows me to create one object and modify it. However, I would like to add the ability to create multiple objects and have a counter for it. I tried adding a counter, but it seems that doesn't work very well... I don't need someone to solve it, but if someone could point me to the right direction, that would be great.
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).
I am trying to create a program that takes input from the user about the length, width, and height of a box, and sends it to the appropriate member function and returns it back to the main. My length, width, and height are not storing and displaying themselves correctly in my OOP using Visual Studio. The code seems to take my last input (which is height) and set it to Length, and set the rest to 1.0. I can't figure out what im doing wrong here.
My GiftWrap.h file
#ifndef GIFTWRAP_H
#define GIFTWRAP_H
using namespace std;
class GiftWrap{
private:
double length;
double width;
double height;
double taxRate;
double pricePerInch;
double subTotal;
double total;
double tax;
public:
GiftWrap();
GiftWrap(double, double);
bool setLength(double);
bool setWidth(double);
bool setHeight(double);
bool setTaxRate(double);
bool setPricePerInch(double);
double getLength() const;
double getWidth() const;
double getHeight() const;
double getPriceperInch() const;
double getTaxRate() const;
double calcSubTotal();
double calcTax();
double calcTotal();
};
#endif
My GiftWrap.cpp file:
#include "GiftWrap.h"
#include <iostream>
using namespace std;
GiftWrap::GiftWrap(){
length = 1.0;
height = 1.0;
width = 1.0;
pricePerInch = 0.0036;
taxRate = 0.08;
}
GiftWrap::GiftWrap(double r, double c){
length = 1.0;
height = 1.0;
width = 1.0;
setPricePerInch(r);
setTaxRate(c);
}
double GiftWrap::getHeight() const{
return height;
}
double GiftWrap::getWidth() const{
return width;
}
double GiftWrap::getLength() const{
return length;
}
double GiftWrap::getPriceperInch() const{
return pricePerInch;
}
double GiftWrap::getTaxRate() const{
return taxRate;
}
double GiftWrap::calcSubTotal(){
subTotal = pricePerInch * ((2 * length * width) + (2 * length * height) + (2 * width * height));
return subTotal;
}
double GiftWrap::calcTax() {
tax = subTotal * taxRate;
return tax;
}
double GiftWrap::calcTotal() {
total = tax + subTotal;
return total;
}
bool GiftWrap::setHeight(double h){
if (h > 0){
height = h;
return true;
}
else{
return false;
}
}
bool GiftWrap::setWidth(double w){
if (w > 0){
width = w;
return true;
}
else{
return false;
}
}
bool GiftWrap::setLength(double l){
if (l > 0){
length = l;
return true;
}
else{
return false;
}
}
bool GiftWrap::setTaxRate(double t){
if (t > 0 && t < 1){
taxRate = t;
return true;
}
else{
return false;
}
}
bool GiftWrap::setPricePerInch(double p){
if (p > 0){
pricePerInch = p;
return true;
}
else{
return false;
}
}
My GiftWrapApp.cpp file:
#include "GiftWrap.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void showInvoice(GiftWrap&);
int main(){
char selection;
double len;
double wid;
double hei;
string storeName = "Sallys Gifts";
GiftWrap sallys(0.0025, 0.925);
do{
cout << "GIFT WRAP INVOICE GENERATOR" << endl
<< "------------------------------" << endl
<< "a)Generate Gift Wrap Invoice" << endl
<< "q)Quit" << endl;
cin >> selection;
if (selection == 'a' || selection == 'A'){
cout << "Please enter the length of your box:" << endl;
cin >> len;
while (!sallys.setLength(len)){
cout << "Invalid Selection, try again" << endl;
cin >> len;
}
cout << "Please enter the width of your box:" << endl;
cin >> wid;
while (!sallys.setLength(wid)){
cout << "Invalid Selection, try again" << endl;
cin >> wid;
}
cout << "Please enter the height of your box:" << endl;
cin >> hei;
while (!sallys.setLength(hei)){
cout << "Invalid Selection, try again" << endl;
cin >> hei;
}
cout << "\nGIFT WRAP INVOICE - " << storeName << endl
<< "----------------------------------" << endl;
showInvoice(sallys);
}
else if (selection == 'q' || selection == 'Q'){
cout << "Thank you for using this program!" << endl;
}
else{
cout << "Invalid Selection, try again" << endl;
}
} while (selection != 'q' && selection != 'Q');
system("PAUSE");
return 0;
}
void showInvoice(GiftWrap& r){
cout << "Box Length: " << fixed << setprecision(2) << r.getLength() << endl;
cout << "Box width: " << fixed << setprecision(2) << r.getWidth() << endl;
cout << "Box Height: " << fixed << setprecision(2) << r.getHeight() << endl;
cout << "Price Per Inch: " << fixed << setprecision(4) << r.getPriceperInch() << "\n" << endl;
cout << "Subtotal: " << fixed << setprecision(2) << r.calcSubTotal() << endl;
cout << "Tax: " << fixed << setprecision(2) << r.calcTax() << endl;
cout << setw(5) << "----------" << endl;
cout << "TOTAL: " << fixed << setprecision(2) << r.calcTotal() << endl;
cout << endl;
}
here is the result that im getting:
http://s10.postimg.org/q97jhgc4p/11111.png
In GiftWrapApp.cpp inside the do while() loop, there a two mistakes:
cout << "Please enter the width of your box:" << endl;
cin >> wid;
while (!sallys.setLength(wid)){ // <----- Should be sallys.setWidthd()!!!
cout << "Invalid Selection, try again" << endl;
cin >> wid;
}
cout << "Please enter the height of your box:" << endl;
cin >> hei;
while (!sallys.setLength(hei)){ // <-- Should be sallys.setHight(hei)!!
cout << "Invalid Selection, try again" << endl;
cin >> hei;
}
Hope this helps!
this is my first time posting here, so be a little gentle ok? Also, I am taking my first programming class, and though I understand everything we are doing in class, a lot of the stuff I read on here is over my head. If you could please try to keep your answers as basic as possible I would appreciate it.
With that said, I am doing C++ programming, using code blocks on a mac.
This is the error I am getting from code blocks
Undefined symbols for architecture x86_64:
"totalBill(double, char, double&, double&, double&, double&)", referenced from:
_main in Watkins-wk6-prog1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I will also post the code so you can take a look and see if that sheds any light.
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
//Global Constants
double const COM_PRICE = 22.91;
double const MID_PRICE = 25.76;
double const FULL_PRICE = 28.87;
double const SUV_PRICE = 98.88;
int const COM_MILES = 20;
int const MID_MILES = 25;
int const FULL_MILES = 30;
string const SUV_MILES = "Unlimited";
double const COM_OVER_MILES = 0.05;
double const MID_OVER_MILES = 0.07;
double const FULL_OVER_MILES = 0.09;
string const SUV_OVER_MILES = "N/A";
int const WEEK = 7;
double const WEEK_RATE = 6.5;
double const TAX = .115;
string const PROMPT1 = "Enter the number of days for the rental";
string const PROMPT2 = "Enter the number of miles driven";
string const PROMPT3 = "Enter the type of car to be rented (C,M,F,S)";
// function prototypes
char carSize(string prompt);
double inputs (string prompt);
double ChargeForCar(char carType, int numDays);
double carPrice (char carType);
double ChargeforMiles(double miles, char carType);
void totalBill(double miles, char carType, double& taxTotal, double& mileCharge, double&
grandTotal, double& carCharge);
string carSize (char carType);
void billOutput(string carTypeName, int numDays, double miles, double carCharge, double
milageCharge, double taxTotal, double grandTotal);
int main()
{
int numDays; // number of days of rental
int weeks ; // number of weeks in rental period
int extraDays; // number of extra days if car rented for more than a week
double mileCharge = 0;
double carCharge = 0;
double taxTotal = 0;
double grandTotal = 0;
char carType;
double miles;
string carTypeName;
cout << "Program to calculate the charge for a rental car." << endl << endl;
carType = carSize(PROMPT3); // calls function to get the type of car from
// the user
numDays = static_cast<int> (inputs(PROMPT1)); // calls function to get number of days in
//the rental period
miles = inputs(PROMPT2); // calls function to get number of mile driven
// during rental period
// calls function to calculate the total rental car bill
totalBill(miles, carType, taxTotal, grandTotal, mileCharge, carCharge);
carTypeName = carSize(carType); // calls function to save car size as string
//calls function to output the bill componenets
billOutput(carTypeName, numDays, miles, carCharge, mileCharge, taxTotal, grandTotal);
return 0;
}
char carSize(string prompt) // function to present car choices and read in user input
{
char carType;
bool valid = false;
cout << "Type of Car" << setw(16) << "Price per day" << setw(15);
cout << "Included Milage" << setw(24) << "Extra charge per mile" << endl;
cout << setw(66) << setfill ('-') << "-" << endl;
cout << setfill(' ') << "C - Compact size" << setw(8) << COM_PRICE;
cout << setw(5) << COM_MILES << setw(5) << COM_OVER_MILES << endl;
cout << "M - Mid-Size" << setw(8) << MID_PRICE;
cout << setw(5) << MID_MILES << setw(5) << MID_OVER_MILES << endl;
cout << "F - Full size" << setw(8) << FULL_PRICE << setw(5) ;
cout << FULL_MILES << setw(5) << FULL_OVER_MILES << endl;
cout << " S - SUV" << setw(8) << SUV_PRICE << setw(5) ;
cout << SUV_MILES << setw(5) << SUV_OVER_MILES << endl;
do
{
cout << prompt ;
cin >> carType;
carType = toupper(carType);
switch(carType)
{
case 'C':
case 'F':
case 'M':
case 'S':
valid = true;
break;
default:
cout << "Invalid entry. Please enter a letter from the list";
break;
}
}
while (valid == false);
return carType;
}
double inputs(string prompt) // general function to read a prompt and output user input
{
double input;
do
{
cout << prompt;
cin >> input;
if (input <= 0)
{
cout << "Invalid input." << endl;
cout << "Please enter a positive, nonzero interger." << endl;
}
}
while (input <= 0);
return input;
}
double ChargeForCar(char carType, int numDays) // function to calculate the charge for the
//rental period
{
double carCharge;
int weeks;
int extraDays;
double carRate;
carRate = carPrice(carType);
if (numDays >= WEEK)
{
weeks = numDays / WEEK;
extraDays = numDays % WEEK;
carCharge = (weeks * WEEK_RATE * carRate) + (extraDays * carRate);
}
else
{
carCharge = carRate * numDays;
}
return carCharge;
}
double carPrice (char carType) // function to determine which price to use for rental car
{
double carRate;
switch (carType)
{
case 'C':
carRate = COM_PRICE;
break;
case 'F':
carRate = FULL_PRICE;
break;
case 'M':
carRate = MID_PRICE;
break;
case 'S':
carRate = SUV_PRICE;
break;
default:
cout << "Unknown car type" << endl;
break;
}
return carRate;
}
double ChargeForMiles(double miles, char carType) // function to calculate the extra milage
//cost
{
double milesCost;
double extraMiles;
switch(carType)
{
case 'C':
if (miles > COM_MILES)
{
extraMiles = miles - COM_MILES;
milesCost = extraMiles * COM_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
case 'F':
if (miles > FULL_MILES)
{
extraMiles = miles - FULL_MILES;
milesCost = extraMiles * FULL_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
case 'M':
if (miles > MID_MILES)
{
extraMiles = miles - MID_MILES;
milesCost = extraMiles * MID_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
}
return milesCost;
}
void totalBill(double miles, double numDays, char carType, double& taxTotal, double& mileCharge,
double& grandTotal, double& carCharge)
// function to calculate the program totals
{
switch(carType)
{
case 'C':
case 'M':
case 'F':
mileCharge = ChargeForMiles(miles, carType);
break;
default:
mileCharge = 0;
break;
}
carCharge = ChargeForCar(carType, numDays); // call to the function to calculate the charge
//for the rental car
taxTotal = (mileCharge + carCharge) * TAX;
grandTotal = mileCharge + carCharge + taxTotal;
}
string carSize(char carType) //function for setting car type name as a string type
{
string typeName;
if (carType == 'C')
{
typeName = "Compact size";
}
else if (carType == 'M')
{
typeName = "Mid size";
}
else if (carType == 'F')
{
typeName = "Full size";
}
else if (carType == 'S')
{
typeName = "SUV size";
}
return typeName;
}
void billOutput(string carTypeName, int numDays, double miles, double carCharge, double
milageCharge, double taxTotal, double grandTotal)
{
cout << "Rent2U Rental Details" << endl << endl;
cout << "Car Size: " << carTypeName << endl;
cout << "Days Rented" << numDays << "days" << endl;
cout << "Miles Driven" << miles << "miles" << endl << endl;
cout << "BILL" << endl;
cout << "Car Charge" << "$" << carCharge << endl;
cout << "Mialge Charge" << "$" << milageCharge << endl;
cout << "Tax" << "$" << taxTotal << endl;
cout << setw(21) << " " << setw(11) << setfill('-') << "-" << endl;
cout << setfill(' ') << "Total Bill" << "$" << grandTotal << endl << endl;
}
It's because your implementation of totalBill has a different signature (i.e. different parameters) from the declaration. One takes two doubles then a char and the other, one double then a char. They need to match.