I'm trying to making it so that if the planet name is not found in the array, it will set the code variable to -1, but it seems to always make the variable -1 regardless. If I don't have the else statement, the program works fine, but then the switch's default statement doesn't work. How do I make it so that the code variable gets assign the proper value if the planet name is entered incorrectly.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
//declare arrays
string planetNames[8] = { "MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE" };
double gravity[8] = { 0.37, 0.78, 1, 0.38, 2.64, 1.16, 1.07, 1.21 };
//declare variable
string name = " ";
double weight = 0.0;
string planet = " ";
double finalWeight = 0.0;
int code = 0;
//explain program to user
cout << "In this program you will enter your first and last name, then your weight, and finally the planet that you want to know your weight on" << endl << endl;
//get input
cout << "What is your first and last name: ";
getline(cin, name);
cout << "How much do you weigh: ";
cin >> weight;
cout << "Which planet would you like to know how much you weigh on: ";
cin >> planet;
//capitalize planet name
transform(planet.begin(), planet.end(), planet.begin(), toupper);
while (weight != -1)
{
for (int x = 0; x < 8; x++)
{
if (planet == planetNames[x])
code = x; //THIS IS WHERE THE PROBLEM IS!!!!!!!!!
else
code = -1;
//end if
}//end for
//calculate and display weights
switch (code)
{
case 0:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 1:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 2:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 3:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 4:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 5:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 6:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
case 7:
finalWeight = weight * gravity[code];
cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
break;
default:
cout << "Invalid planet name" << endl;
}//end switch
cout << endl;
cout << "How much do you weigh(-1 to end the program): ";
cin >> weight;
cout << "Which planet would you like to know how much you weigh on: ";
cin >> planet;
transform(planet.begin(), planet.end(), planet.begin(), toupper);
}//end while
system("pause");
return 0;
}
You need to break the loop after you have found the matching planet name, otherwise the loop keeps searching, and the next name won't match so code is set to -1.
Also: for efficiency and clarity: set code = -1 before the loop and only set to x on a match:
code = -1;
for (int x = 0; x < 8; x++)
{
if (planet == planetNames[x])
{
code = x; //THIS IS WHERE THE PROBLEM IS!!!!!!!!!
break; // And now it's gone ;-)
}
}
this if is fine.
for example if you type mars, code will get value 3.
But then in next step of loop you will set it again to -1 ;)
Break the loop when result is found.
Advice:
Split this part of code to new function.
Related
I'm just starting in studying C++, and I am doing a simple challenge which is GWA Calculator, but I am having a problem finding out how to store the multiple strings input (which is the Subjects/Course) and displaying it after together with the Units and Grades. I am really sorry, but I tried finding out how and I couldn't find an answer. Hope you can help me out.
#include <stdlib.h>
using namespace std;
void calculateGWA();
int main()
{
system("cls");
int input;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| GWA Calculator |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| MENU:\t\t\t\t\t\t\t " << "|" << endl;
cout << "\t\t| 1. Calculate GWA (General Weighted Average)\t\t " << "|" << endl;
cout << "\t\t| 2. Calculate CGWA (Cummulative Weighted Average) " << "|" << endl;
cout << "\t\t| 4. Exit Application\t\t\t\t\t " << "|" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
sub:
cout << "\t\tEnter your choice: ";
cin >> input;
switch(input)
{
case 1:
calculateGWA();
break;
case 2:
//calculateCGPA();
break;
case 3:
main();
break;
case 4:
exit(EXIT_SUCCESS);
break;
default:
cout << "You have entered wrong input.Try again!\n" << endl;
goto sub;
break;
}
}
void calculateGWA()
{
int q;
system("cls");
cout << "-------------- GWA Calculator -----------------"<<endl;
cout << " How many course(s)?: ";
cin >> q;
char c_name[50];
float unit [q];
float grade [q];
cout << endl;
for(int i = 0; i < q; i++)
{
cout << "Enter the Course Name " << i+1 << ": ";
cin >> c_name;
cout << "Enter the Unit " << c_name << ": ";
cin >> unit[i];
cout << "Enter the Grade " << c_name << ": ";
cin >> grade[i];
cout << "-----------------------------------\n\n" << endl;
}
float sum = 0;
float tot;
for(int j = 0; j < q; j++)
{
tot = unit[j] * grade[j];
sum = sum + tot;
}
float totCr = 0;
for(int k = 0; k < q; k++)
{
totCr = totCr + unit[k];
}
system("cls");
// PRINTS OUT THE COURSES - UNITS - GRADES AND GWA //
cout << "\t\t =============================================================== " << endl;
cout << "\t\t| COURSE | UNIT | GRADE |" << endl;
cout << "\t\t =============================================================== " << endl;
cout << "Total Points: " << sum << " \n Total Credits: " << totCr << " \nTotal GPA: " << sum/totCr << " ." << endl;
cout << c_name << "\n" << endl;
cout << "===================================" << endl;
sub:
int inmenu;
cout << "\n\n\n1. Calculate Again" << endl;
cout << "2. Go Back to Main Menu" << endl;
cout << "3. Exit This App \n\n" << endl;
cout << "Your Input: " << endl;
cin >> inmenu;
switch(inmenu)
{
case 1:
calculateGPA();
break;
case 2:
main();
break;
case 3:
exit(EXIT_SUCCESS);
default:
cout << "\n\nYou have Entered Wrong Input!Please Choose Again!" << endl;
goto sub;
}
}
I need to be able to update these variables.
Base Damage
Accuracy
Rate of fire
Range
I need to be able to add to the base damage or accuracy or the rate of fire or the range, once I have already added I need to be able to
continuously update the variable by adding more points to the variable if I so desire to do so.
The points total needs to be subtracted but when the variable is first subtracted if you continue to buy within the menu the variable does not change at all.
Once you have the variables updated you should be able to then look at the total of the points value and the weapon stats and see a changes after repetitive buying.
The variables after 1 purchase continue to remain stagnant.
I am sorry for this large amount of text I am just not sure what to take away from these segments of code.
The main question is how to modify the value of a variable after it had been added or subtracted to.
#include <iostream>
#include <string>
using namespace std;
int i;
int x;
int g;
class shotgun
{
private:
int a = 40, b = 15, c = 10, d = 6, j = 10, p = 10, v = 5, n = 2, o = 5, k = 5;
int pointstotal = p -= v;
int baseDamage = a += j;
int accuracy = b += k;
int range = c += o;
int rateOffire = d += k;
public:
shotgun(){
do {
cout << "You have 10 points at your disposal" << endl;
cout << "Your shotgun stats are.." << endl;
cout << "Base Damage: " << a << endl;
cout << "Accuracy:" << b << endl;
cout << "Range:" << c << endl;
cout << "Rate of Fire:" << d << "s" << endl;
cout << "1)Would you like to choose something that
upgrades your damage output?" << endl;
cout << "2)Would you like something for accuracy?" << endl;
cout << "3)Would you like something for range?" << endl;
cout << "4)Would you like some thing for your rate of fire?" << endl;
cout << "5)Would you like to see your new weapon's stats and your points." << endl;
cout << "6)Would you like to exit the program?" << endl;
cin >> i;
switch (i) {
case 1:
cout << "Here are some slugs for your damage output.." << endl;
a + j;
p - v;
break;
case 2:
cout << "Here is a longer stock for your shotgun." << endl;
b + k;
p - v;
break;
case 3:
cout << "Here is a longer barrel for your shotgun." << endl;
c + o;
p - v;
break;
case 4:
cout << "Here is better break action barrel for your shotgun." << endl;
d + n;
p - v;
break;
case 5:
cout << "You now have" << pointstotal << "points at your disposal." << endl;
cout << "Your shotgun stats are now.." << endl;
cout << "Base Damage:" << baseDamage << endl;
cout << "Accuracy:" << accuracy << endl;
cout << "Range:" << range << endl;
cout << "Rate of Fire:" << rateOffire << "s" << endl;
break;
case 6:
cout << "Exiting Program" << endl;
g = 5;
}
} while (g != 5);
}
};
int main()
{
cout << "Welcome to the weapon customization system!" << endl;
cout << "Choose your Weapon to customize you have ten points" << endl;
cout << "*************************************************" << endl;
cout << "1)Choose the double barreled shotgun?" << endl;
cout << "2)Choose the assault rifle?" << endl;
cout << "3)Choose the 44. Magnum?" << endl;
cout << "4)Choose the combat shotgun?" << endl;
cin >> x;
switch (x)
{
case 1:
shotgun e;
break;
}
cout << "\n\n";
system("Pause");
}
The variables will stay the same even after you have used the previous options to modify the stats within the switch case if you press 5 as an option after upgrading the weapon.
Your problem is here:
switch (x)
{
case 1:
shotgun e;
break;
}
... you're declaring an object of type shotgun inside the case statement, which means that a new shotgun object is created whenever case 1 is entered, and destroyed when that scope is exited (i.e. when the break command is executed).
If you want the state of your shotgun object to persist, you'll need to declare it at an earlier/bigger scope (e.g. at the beginning of main() would be a good spot for it)
Also, the attempted-variable-modifications inside your do...while() loop are wrong; instead of:
case 1:
cout << "Here are some slugs for your damage output.." << endl;
a + j;
p - v;
break;
... you probably meant to do:
case 1:
cout << "Here are some slugs for your damage output.." << endl;
a += j;
p -= v;
break;
.... note the += means "increase by" and the -= means "decrease by" (whereas a+j and p-v merely compute a value that gets immediately discarded, so they have no effect)
As an aside, you should probably move the do...while() loop out of the shotgun constructor and into a separate method that you can call later on; otherwise it will be executed every time a shotgun object is created, which is probably not what you want.
HERE IS THE FINAL RESULT.
I completed this code with the advice given thank you for the advice.
For those who need extra help. :)
#include <iostream>
#include <string>
using namespace std;
int i;
int x;
int g;
int j = 5;
class assaultrifle
{
private:
int baseassaultPoints = 10, baseassaultDamage = 35, baseassaultAccuracy = 56, baseassaultRange = 72, baseassaultROF = 78;
int assaultPoints = 10, assaultDamage = 35, assaultAccuracy = 56, assaultRange = 72, assaultROF = 78;
public:
int v = 5, n = 2, o = 5, k = 5, j = 5;
assaultrifle()
{
do {
cout << "These are your base stats for your gun.." << endl;
cout << "Your weapon has a base damage of:" << baseassaultDamage << endl;
cout << "Your weapon has an accuracy of: " << baseassaultAccuracy << endl;
cout << "Your weapon has a range of:" << baseassaultRange << endl;
cout << "Your weapon has a rate of fire of:" << baseassaultROF << endl;
cout << "**************************************" << endl;
cout << "You have " << assaultPoints << "assaultpoints.." << endl;
cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
cout << "2)Would you like something for accuracy?" << endl;
cout << "3)Would you like something for range?" << endl;
cout << "4)Would you like some thing for your rate of fire?" << endl;
cout << "5)Would you like to exit the program?" << endl;
cin >> i;
system("Pause");
system("cls");
if (assaultPoints == 0) {
cout << "You have" << assaultPoints << "points at your disposal" << endl;
cout << "Your upgraded M16 now has better stats." << endl;
cout << "Your final assault rifle stats are.." << endl;
cout << "Base Damage: " << assaultDamage << endl;
cout << "Accuracy:" << assaultAccuracy << endl;
cout << "Range:" << assaultRange << "meters" << endl;
cout << "Rate of Fire:" << assaultRange << "s" << endl;
cout << "**************************************" << endl;
}
switch (i) {
case 1:
cout << "Here are some armor piercing rounds for your damage output.." << endl;
assaultDamage += j;
assaultPoints -= v;
break;
case 2:
cout << "Here is a foregrip for your assault rifle." << endl;
assaultAccuracy += k;
assaultPoints -= v;
break;
case 3:
cout << "Here is a heavy barrel for your assault rifle." << endl;
assaultRange += o;
assaultPoints -= v;
break;
case 4:
cout << "Here is taped magazine for your assault rifle." << endl;
assaultROF += n;
assaultPoints -= v;
break;
case 5:
cout << "Exiting Program" << endl;
g = 5;
}
} while (g != 5);
}
};
class shotgun {
private:
int baseShotgunDamage = 50, baseShotgunAccuracy = 15, baseShotgunRange = 10, baseShotgunROF = 6, baseShotgunPoints = 10;
int shotgunDam = 50, shotgunAccuracy = 15, shotgunRange = 10, shotgunROF = 6, j = 10, shotgunPoints = 10;
int pointstotal = shotgunPoints - v;
int baseDamage = shotgunDam + j;
int accuracy = shotgunAccuracy + k;
int range = shotgunRange + o;
int rateOffire = shotgunROF + k;
public:
int v = 5, n = 2, o = 5, k = 5;
shotgun()
{
do {
cout << "These are your base stats for your gun.." << endl;
cout << "Your weapon has a base damage of:" << baseShotgunDamage << endl;
cout << "Your weapon has an accuracy of: " << baseShotgunAccuracy << endl;
cout << "Your weapon has a range of:" << baseShotgunRange << endl;
cout << "Your weapon has a rate of fire of:" << baseShotgunROF << endl;
cout << "**************************************" << endl;
cout << "You have " << shotgunPoints << "sawed off shotgun points.." << endl;
cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
cout << "2)Would you like something for accuracy?" << endl;
cout << "3)Would you like something for range?" << endl;
cout << "4)Would you like some thing for your rate of fire?" << endl;
cout << "5)Would you like to exit the program?" << endl;
cout << "**************************************" << endl;
cin >> i;
system("Pause");
system("cls");
if (shotgunPoints == 0) {
cout << "You have" << shotgunPoints << "points at your disposal" << endl;
cout << "Your final double barreled shotgun stats are.." << endl;
cout << "Base Damage: " << shotgunDam << endl;
cout << "Accuracy:" << shotgunAccuracy << endl;
cout << "Range:" << shotgunRange << "meters" << endl;
cout << "Rate of Fire:" << shotgunROF << "s" << endl;
cout << "**************************************" << endl;
}
switch (i) {
case 1:
cout << "Here are some slugs for your damage output.." << endl;
shotgunDam += j;
shotgunPoints -= v;
break;
case 2:
cout << "Here is a longer stock for your shotgun." << endl;
shotgunAccuracy += k;
shotgunPoints -= v;
break;
case 3:
cout << "Here is a longer barrel for your shotgun." << endl;
shotgunRange += o;
shotgunPoints -= v;
break;
case 4:
cout << "Here is better break action barrel for your shotgun." << endl;
shotgunROF += n;
shotgunPoints -= v;
break;
case 5:
cout << "Exiting Program" << endl;
g = 5;
}
} while (g != 5);
}
};
class handgun
{
private:
int basehandgunDam = 15, basehandgunAccuracy = 55, basehandgunRange = 25, basehandgunROF = 19, j = 10, basehandgunPoints = 10;
int handgunDam = 15, handgunAccuracy = 55, handgunRange = 25, handgunROF = 19, handgunPoints = 10;
int pointstotal = handgunPoints - v;
int baseDamage = handgunDam + j;
int accuracy = handgunAccuracy + k;
int range = handgunRange + o;
int rateOffire = handgunROF + k;
public:
int v = 5, n = 2, o = 5, k = 5;
handgun()
{
do {
cout << "These are your base stats for your gun.." << endl;
cout << "Your weapon has a base damage of:" << basehandgunDam << endl;
cout << "Your weapon has an accuracy of: " << basehandgunAccuracy << endl;
cout << "Your weapon has a range of:" << basehandgunRange << endl;
cout << "Your weapon has a rate of fire of:" << basehandgunROF << "s" << endl;
cout << "**************************************" << endl;
cout << "You have " << handgunPoints << "glock points.." << endl;
cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
cout << "2)Would you like something for accuracy?" << endl;
cout << "3)Would you like something for range?" << endl;
cout << "4)Would you like some thing for your rate of fire?" << endl;
cout << "5)Would you like to exit the program?" << endl;
cout << "**************************************" << endl;
cin >> i;
system("Pause");
system("cls");
if (handgunPoints == 0) {
cout << "You have" << handgunPoints << "points at your disposal" << endl;
cout << "Your final Glock 17 stats are.." << endl;
cout << "Base Damage: " << handgunDam << endl;
cout << "Accuracy:" << handgunAccuracy << endl;
cout << "Range:" << handgunRange << "meter" << endl;
cout << "Rate of Fire:" << handgunROF << "s" << endl;
cout << "**************************************" << endl;
}
switch (i) {
case 1:
cout << "Here are some hollow points for your damage output.." << endl;
handgunDam += j;
handgunPoints -= v;
break;
case 2:
cout << "Here is a foldable stock for your pistol." << endl;
handgunAccuracy += k;
handgunPoints -= v;
break;
case 3:
cout << "Here is a longer muzzle for your glock 17." << endl;
handgunRange += o;
handgunRange -= v;
break;
case 4:
cout << "Here is a full auto function for you glock 17." << endl;
handgunROF += n;
handgunROF -= v;
break;
case 5:
cout << "Exiting Program" << endl;
g = 5;
}
} while (g != 5);
}
};
class combatshotgun
{
private:
int basecombatDam = 40, basecombatAccuracy = 55, basecombatRange = 25, basecombatRecoil = 20, j = 10, basecombatPoints = 10;
int combatDam = 40, combatAccuracy = 55, combatRange = 25, combatRecoil = 20, combatPoints = 10;
int pointstotal = combatPoints - v;
int baseDamage = combatDam + j;
int accuracy = combatAccuracy + k;
int range = combatRange + o;
int recoil = combatRecoil + k;
public:
int v = 5, n = 2, o = 5, k = 5;
combatshotgun()
{
do {
cout << "These are your base stats for your gun.." << endl;
cout << "Your weapon has a base damage of:" << basecombatDam << endl;
cout << "Your weapon has an accuracy of: " << basecombatAccuracy << endl;
cout << "Your weapon has a range of:" << basecombatRange << endl;
cout << "Your weapon has a rate of fire of:" << basecombatRecoil << endl;
cout << "**************************************" << endl;
cout << "You have " << combatPoints << "combat shotgun points.." << endl;
cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
cout << "2)Would you like something for accuracy?" << endl;
cout << "3)Would you like something for range?" << endl;
cout << "4)Would you like something for the recoil?" << endl;
cout << "5)Would you like to exit the program?" << endl;
cout << "**************************************" << endl;
cin >> i;
system("Pause");
system("cls");
if (combatPoints == 0) {
cout << "You have" << combatPoints << "points at your disposal" << endl;
cout << "Your final shotgun stats are.." << endl;
cout << "Base Damage: " << combatDam << endl;
cout << "Accuracy:" << combatAccuracy << endl;
cout << "Range:" << combatRange << "meters" << endl;
cout << "Recoil:" << combatRecoil << endl;
cout << "**************************************" << endl;
}
switch (i) {
case 1:
cout << "Here are some dragons breath for your Remington 12 gauge.." << endl;
combatDam += j;
combatPoints -= v;
break;
case 2:
cout << "Here is a longer stock for your Remingonton 12 guage." << endl;
combatAccuracy += k;
combatPoints-= v;
break;
case 3:
cout << "Here is a longer barrel for your Remington." << endl;
combatRange += o;
combatPoints -= v;
break;
case 4:
cout << "Here is a foregrip for your remington 12 gauge." << endl;
combatRecoil -= n;
combatPoints -= v;
break;
case 5:
cout << "Exiting Program" << endl;
g = 5;
}
} while (g != 5);
}
};
int main()
{
cout << "Welcome to the weapon customization system!" << endl;
cout << "Choose your Weapon to customize you have ten points" << endl;
cout << "*************************************************" << endl;
cout << "1)Choose the double barreled shotgun?" << endl;
cout << "2)Choose the M16?" << endl;
cout << "3)Choose the Glock 17?" << endl;
cout << "4)Choose the Remington 12 guage?" << endl;
cout << "**************************************" << endl;
cin >> x;
switch (x)
{
case 1:
shotgun();
break;
case 2:
assaultrifle();
break;
case 3:
handgun();
break;
case 4:
combatshotgun();
break;
}
cout << "\n\n";
system("Pause");
}
I'm Using Visual studio (C++) in a class that I am taking, I've had to teach myself functions, and I've hit a small snag in the road that I'd appreciate some advice on.
What I'm having trouble with, is the part of the assignment that states i must
"Utilize a function that prints (not find) the largest/average/smallest commissions"
The way I read this, I'm assuming she only wants it to print, and not do calculations in the function.
A friend suggested I try void print(etc) However I'm unsure how to grab the calculation in main and give it to the function I'm trying to print, or am i going at it all wrong?
I've commented out the portion of code I was trying to function. You can also find it at the very bottom of the code.
Any suggestions/help is greatly appreciated, as most of what I've looked up haven't really dealt with this problem (that I've found)
#include <iostream>
#include <iomanip>
using namespace std;
#define TITLE "Alva's"
#define STANDARD 0.05
#define HYBRID 0.10
#define ELECTRIC 0.15
#define HOLD 50
void dashLine();
int getSalesId();
int runProgram();
char vehicleType();
double sellingPrice();
void print(double largeSmallAverage);
int main()
{
int tot_count,
id_num[HOLD], //* ID
r_ay = 0, //* array
s_count, //*standard
h_count, //*Hybrid
e_count, //*electric
hold_id, //*array Id
compare, //*Pass
change, //*change made when change has value
yesno;
double tot_standard,
tot_hybrid,
tot_electric,
tot_price,
price[HOLD],
hold_price, //*Array hold
comm_l, //* Large
comm_s, //* Small
hold_comm, //*Array hold
avg_comm,
tot_commission,
commission[HOLD];
char car[HOLD],
temp_car;
tot_count = 0;
s_count = 0;
h_count = 0;
e_count = 0;
tot_price = 0;
tot_standard = 0;
tot_hybrid = 0;
tot_electric = 0;
cout << "\n" << TITLE << " Commission Calculator";
yesno = runProgram();
while (yesno == 1)
{
dashLine();
id_num[r_ay] = getSalesId();
car[r_ay] = vehicleType();
price[r_ay] = sellingPrice();
tot_price += price[r_ay];
switch (car[r_ay])
{
case 'S':
case 's':
commission[r_ay] = (price[r_ay] * STANDARD);
tot_standard += commission[r_ay];
s_count++;
break;
case 'H':
case 'h':
commission[r_ay] = (price[r_ay] * HYBRID);
tot_hybrid += commission[r_ay];
h_count++;
break;
case 'E':
case 'e':
commission[r_ay] = (price[r_ay] * ELECTRIC);
tot_electric += commission[r_ay];
e_count++;
break;
}
cout << "\n The commission for this sale, for Employee ID: " << fixed << setprecision(0) << id_num[r_ay] << " is:$ " << fixed << setw(5) << setprecision(2) << commission[r_ay];
cout << "\n";
yesno = runProgram();
r_ay++;
if (r_ay >= HOLD)
yesno = 0;
}
tot_count = (s_count + h_count + e_count);
tot_commission = (tot_standard + tot_hybrid + tot_electric);
{
cout << "\n Number of standard vehicle commissions calculated = " << fixed << setw(8) << setprecision(0) << s_count;
cout << "\n Number of hybrid vehicle commissions calculated = " << fixed << setw(8) << h_count;
cout << "\n Number of electric vehicle commissions calculated = " << fixed << setw(8) << e_count;
cout << "\n Number of vehicle commissions calculated = " << fixed << setw(8) << tot_count;
cout << "\n Total Overall price calculated =$ " << fixed << setw(8) << setprecision(2) << tot_price;
cout << "\n Total amount of standard vehicle commissions =$ " << fixed << setw(8) << tot_standard;
cout << "\n Total amount of hybrid vehicle commissions =$ " << fixed << setw(8) << tot_hybrid;
cout << "\n Total amount of electric vehicle commissions =$ " << fixed << setw(8) << tot_electric;
cout << "\n Total amount of all commissions paid out =$ " << fixed << setw(8) << tot_commission;
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
}
if (tot_count > 0)
{
avg_comm = (tot_commission / tot_count);
comm_s = commission[0];
comm_l = commission[0];
for (r_ay = 1; r_ay < tot_count; r_ay++)
{
if (commission[r_ay] < comm_s)
comm_s = commission[r_ay];
if (commission[r_ay] > comm_l)
comm_l = commission[r_ay];
}
void print(double largeSmallAverage);
//{
// cout << "\n ";
// cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
// cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
// cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
//}
}
cout << "\n";
change = 1;
compare = tot_count - 1;
do
{
change = 0;
for (r_ay = 0; r_ay < compare; r_ay++)
{
if (commission[r_ay] > commission[r_ay + 1])
{
temp_car = car[r_ay];
hold_id = id_num[r_ay];
hold_price = price[r_ay];
hold_comm = commission[r_ay];
commission[r_ay] = commission[r_ay + 1];
commission[r_ay + 1] = hold_comm;
id_num[r_ay] = id_num[r_ay + 1];
id_num[r_ay + 1] = hold_id;
car[r_ay] = car[r_ay + 1];
car[r_ay + 1] = temp_car;
price[r_ay] = price[r_ay + 1];
price[r_ay + 1] = hold_price;
change = 1;
}
}
compare--;
} while ((compare > 0) && (change == 1));
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
cout << "\n ";
}
system("pause");
return 0;
}
}
void dashLine()
{
cout << "\n -----------------------------------";
}
int getSalesId()
{
int id_num;
cout << "\n Please enter Employee ID: ";
cin >> id_num;
while (id_num < 10000 || id_num > 99999)
{
cout << "\n Invalid Employee ID, Please enter a 5 digit ID ";
cin >> id_num;
}
return id_num;
}
int runProgram()
{
int rp_yesno;
cout << "\n Is there a customer? 1 = yes, 0 = no ";
cin >> rp_yesno;
while ((rp_yesno != 1) && (rp_yesno != 0))
{
cout << "\n Invalid Entry Please enter 1/0 ";
cout << "\n Is there a customer? 1 = yes 0 = no ";
cin >> rp_yesno;
}
return rp_yesno;
}
char vehicleType()
{
char car;
cout << "\n Please enter type of vehicle sold";
cout << "\n (S=standard, H=hybrid, E=electric): ";
cin >> car;
while (!((car == 'S') || (car == 's') || (car == 'h') || (car == 'H') || (car == 'E') || (car == 'e')))
{
cout << "\n Invalid input Please enter S/E/H ";
cin >> car;
}
return car;
}
double sellingPrice()
{
double price;
cout << "\n Please enter the selling price of the car:$ " << fixed << setprecision(2);
cin >> price;
while (price < 1)
{
cout << "\n Invalid entry, Please enter an amount greater than 0 ";
cin >> price;
}
return price;
}
void print(double largeSmallAverage)
{
double comm_s,
comm_l,
avg_comm;
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
}
}
You would have a function called print that takes has three double parameters: void print(double small, double large, double average);.
Then later on you would call it with print(comm_s, comm_l, avg_comm);.
You need to change the definition of print:
void print(double small, double large, double average)
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << small;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << large;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << average;
}
Hello guys I'm not an expert on the subject so please excuse my pour skills. I finished my program and it works fine (calculator). The problem is that now I don't know where to locate the while loop in conjunct with the Boolean function to repeat the process once it is done with a task (once the program completes a math operation). Any help, comment or suggestion will be greatly appreciated. Thank you.!!
#include <iostream>
#include <math.h>
#include <cmath>
int main()
{
double a=0.0;
double b=0.0;
double c=0.0;
bool repeat = true;
do {
using namespace std;
int x;
cout << "**********************************" << endl;
cout << "| |" << endl;
cout << "| 0 - Quit |" << endl;
cout << "| 1 - Add |" << endl;
cout << "| 2 - Subtract |" << endl;
cout << "| 3 - Divide |" << endl;
cout << "| 4 - Multiply |" << endl;
cout << "| 5 - Raise X to the power Y |" << endl;
cout << "| 6 - Sine ( x ) |" << endl;
cout << "| 7 - Cosine ( x ) |" << endl;
cout << "| 8 - Tangent ( x ) |" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl;
cin >> x;
switch (x)
{
{
case 1:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter second value " << endl;
cin >> b;
c=a+b;
cout << "The addition of " << a << " and "<< b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 2:
cout << " Enter the first value" << endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a-b;
cout << "The subtraction of " << a << " and " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 3:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a/b;
cout << " The division os " << a << " and " << b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 4:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a*b;
cout << " The product of " << a << " times " << b << " is " << c << endl;
break;
bool repeat = true;
}
{
case 5:
cout << " Enter the value to be exponentiated " <<endl;
cin >> a ;
cout << " Enter the exponent" << endl;
cin >> b;
c= pow(a,b);
cout << a << " Rased to the power of " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 6:
cout << " Enter the value that you want the sine to be taken of" <<endl;
cin >> a ;
c=sin(a);
cout << " The sine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 7:
cout << " Enter the value that you want the cosine to be taken of" <<endl;
cin >> a ;
c=cos(a);
cout << " The cosine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 8:
cout << " Enter the value that you want the tangent to be taken of" <<endl;
cin >> a ;
c=tan(a);
cout << " The tangent of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
return 0;
}
So here is few moments.
Call
using namespace std;
just believe - is bad idea;
In conditions like if() or while() use operator == instead of =. Because "=" - is assigne operator, and return value depended on success of operation. And "==" is compare operator.
Ow and figure one more missunderstanding. Using bool rezult = true; is wrong. You should use rezult = true; Because every time when you write type specifer you create local variable in context of case, and this don`t affect rezult declared in main
My opinion for your question is little change:
from:
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
to
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
repeat = false;}
break;
}
} while (repeat == true);
and if you need a bit more calculations you can wrapped it to new cicle something like:
while(new_condtion == true) {
do {
...
} while(repeat == true);
//change new_condtion
}
Don't redefine repeat within switch case. This creates a different variable named repeat which, although it has the same name, is not the variable named repeat defined before the loop. This is what you get when you copy a definition of the form bool repeat = true; into multiple places.
The continuation condition for the loop (repeat = true) will also loop forever. Comparison is two = signs, not one.
I'm writing a program that asks the user to enter their name, weight, and planet to find out what their weight would be on the given planet. The program works fine, except it skips over the name input the second time. I need some suggestions on how to fix this.
#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
//declare variables and arrays
string planets[8] = {"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune"};
double gravities[8] = {0.37, 0.78, 1.00, 0.38, 2.64, 1.16, 1.07, 1.21};
double weight = 0.0;
string planetChoice = " ";
string name = " ";
//give the user information and instructions
cout << "~~~~~~~~~~~~~~~~~~~~~Find your weight on different planets!~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "This program calculates what your weight would be on various planets in " << endl;
cout << "our solar system. To use this program, enter your full name, weight, and the " << endl;
cout << "planet when prompted." << endl;
cout << endl;
cout << "Choice of planets:" << endl;
cout << "Mercury, Venus, Earth, Mars, Jupiter," << endl;
cout << "Saturn, Uranus, and Neptune" << endl;
cout << endl;
cout << "Full name (first and last) (\"exit\" to stop): ";
getline (cin, name);
while (name != "exit")
{
cout << "Enter your weight: ";
cin >> weight;
cout << "Choose your planet: ";
cin >> planetChoice;
transform(planetChoice.begin(), planetChoice.end(), planetChoice.begin(), tolower);
for (int x = 0; x < 8; x++)
{
if (planetChoice == planets[x])
{
weight = weight * gravities[x];
switch(x)
{
case 0:
cout << name << ", Your weight on the plannet closest to the sun (Mercury) would be: " << weight << "You'd be light as a feather!" <<endl;
break;
case 1:
cout << name << ", your weight on Venus would be: " << weight << "You'd be a bit lighter!" << endl;
break;
case 2:
cout << name << ", your weight on your home planet (Earth) is: " << weight << endl;
break;
case 3:
cout << name << ", your weight on the red planet (Mars) would be: " << weight << endl;
break;
case 4:
cout << name << ", your weight on the largest planet (Jupiter) would be: " << weight << endl;
break;
case 5:
cout << name << ", your weight on the planet with rings (Saturn) would be: " << weight << endl;
break;
case 6:
cout << name << ", your weight on The Bull's Eye Planet (Uranus) would be: " << weight << "Your weight would be close to normal" << endl;
break;
case 7:
cout << name << ", your weight on the ice cold planet of Neptune would be: " << weight << endl;
break;
default:
cout << "Invalid planet name" << endl;
break;
}//end switch
}//end if
}//end for
cout << "Full name (first and last) (\"exit\" to stop): ";
getline (cin, name);
}//end while
system("pause");
return 0;
}//end of main function