I have seen this problem around the forum but I have not been able to fix this issue. After all "else if" Xcode can't compile, it tells me that it is a "parse issue" and that requieres an "expected expression".
I know that it is a beginner's question but I truly want to understand what is wrong with my code, why it keeps telling me expected expression after each esle if...
if (TypeStrat == 1)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K,T,r,v) + BSPrixPut(S,K,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K,T,r,v) + BSDeltaPut(S,K,T,r,v)) << endl;
cout << "Vega " << (2*BSVega(S,K,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K,T,r,v) + BSRhoPut(S,K,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K,T,r,v)/365.0) + (BSThetaPut(S,K,T,r,v)/365.0)
<< "journalier" << endl;
else if (TypeStrat == 2)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) + BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) + BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) + BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) + BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) + (BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 3)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du short Call ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) - BSPrixCall(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) - BSDeltaCall(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) - BSRhoCall(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) - (BSThetaCall(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 4)
{
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du short Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixPut(S,K1,T,r,v) - BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaPut(S,K1,T,r,v) - BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoPut(S,K1,T,r,v) - BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaPut(S,K1,T,r,v)/365.0) - (BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 5)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice des deux short Call ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) - 2*BSPrixCall(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) - 2*BSDeltaCall(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - 2*BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) - 2*BSRhoCall(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) - 2*(BSThetaCall(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 6)
{
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice des deux short Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixPut(S,K1,T,r,v) - 2*BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaPut(S,K1,T,r,v) - 2*BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - 2*BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoPut(S,K1,T,r,v) - 2*BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaPut(S,K1,T,r,v)/365.0) - 2*(BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
}
An else if requires a preceding if statement. The block of if statement that precedes your first else if is not closed, i.e. an ending curly brace } is missing. Also, there is an extra } in the end.
Missing curly brace:
if (TypeStrat == 1)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K,T,r,v) + BSPrixPut(S,K,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K,T,r,v) + BSDeltaPut(S,K,T,r,v)) << endl;
cout << "Vega " << (2*BSVega(S,K,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K,T,r,v) + BSRhoPut(S,K,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K,T,r,v)/365.0) + (BSThetaPut(S,K,T,r,v)/365.0)
<< "journalier" << endl;
// } <-- Insert ending curly brace here
else if (TypeStrat == 2)
Extra curly brace:
else if (TypeStrat == 6)
{
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice des deux short Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixPut(S,K1,T,r,v) - 2*BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaPut(S,K1,T,r,v) - 2*BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - 2*BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoPut(S,K1,T,r,v) - 2*BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaPut(S,K1,T,r,v)/365.0) - 2*(BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
} // <-- Remove this extra curly brace
Looks like have a } in the wrong place. Is the following what you intended. Removed } from end and added before else if (TypeStrat == 2)
if (TypeStrat == 1)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K,T,r,v) + BSPrixPut(S,K,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K,T,r,v) + BSDeltaPut(S,K,T,r,v)) << endl;
cout << "Vega " << (2*BSVega(S,K,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K,T,r,v) + BSRhoPut(S,K,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K,T,r,v)/365.0) + (BSThetaPut(S,K,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 2)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) + BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) + BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) + BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) + BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) + (BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 3)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du short Call ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) - BSPrixCall(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) - BSDeltaCall(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) - BSRhoCall(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) - (BSThetaCall(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 4)
{
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice du short Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixPut(S,K1,T,r,v) - BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaPut(S,K1,T,r,v) - BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoPut(S,K1,T,r,v) - BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaPut(S,K1,T,r,v)/365.0) - (BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 5)
{
cout << "Quel est le prix d'exercice du long Call ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice des deux short Call ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixCall(S,K1,T,r,v) - 2*BSPrixCall(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaCall(S,K1,T,r,v) - 2*BSDeltaCall(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - 2*BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoCall(S,K1,T,r,v) - 2*BSRhoCall(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaCall(S,K1,T,r,v)/365.0) - 2*(BSThetaCall(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
else if (TypeStrat == 6)
{
cout << "Quel est le prix d'exercice du long Put ?" << endl;
cin >> K1;
cout << "Quel est le prix d'exercice des deux short Put ?" << endl;
cin >> K2;
cout << "Prix de la stratégie " << (BSPrixPut(S,K1,T,r,v) - 2*BSPrixPut(S,K2,T,r,v)) << endl;
cout << "Delta " << (BSDeltaPut(S,K1,T,r,v) - 2*BSDeltaPut(S,K2,T,r,v)) << endl;
cout << "Vega " << (BSVega(S,K1,T,r,v) - 2*BSVega(S,K2,T,r,v)) << endl;
cout << "Rho " << (BSRhoPut(S,K1,T,r,v) - 2*BSRhoPut(S,K2,T,r,v)) << endl;
cout << "Theta " << (BSThetaPut(S,K1,T,r,v)/365.0) - 2*(BSThetaPut(S,K2,T,r,v)/365.0)
<< "journalier" << endl;
}
Related
I got this code and the problem is that I can only continue the loop forever but i can't exit it. I have been struggling with this for quite some hours and figure out where i do wrong. I think that its mostly about the do while in the end but since I am new to this there might be somewhere else in the code.
#include <iostream>
using namespace std;
int main() {
locale swedish("swedish");
locale::global(swedish);
int saldo = 1000;
int bet;
int vinst;
char val;
int nummer = 0;
char spelaigen;
char spelainteigen;
/*Här "presenteras" programmet med lite cout's som mest välkomnar dig till roulett och ger dig ett saldo*/
cout << "Username: Användare" << endl;
cout << "Password: ********" << endl;
cout << "Logging in: " << endl;
cout << "Searching for a live room: " << endl;
cout << "Initializing mainframe: \n" << endl;
cout << "Välkommen till Save7heCasino" << endl;
cout << "Här spelar du Roulette " << endl;
cout << "Du har " << saldo << " att spela för." << endl;
while (true)
{
cout << "Du får välja mellan 100, 300 eller 500 att spela för." << endl;
cin >> bet;
if (saldo < bet)
{
cout << "Ditt saldo är för lågt." << endl;
break;
}
if (bet == 100)
{
cout << "Du valde insatsen 100:-" << endl;
cout << "Ditt saldo är " << saldo - bet << endl;
}
else if (bet == 300)
{
cout << "Du valde insatsen 300:-" << endl;
cout << "Ditt saldo är " << saldo - bet << endl;
}
else if (bet == 500)
{
cout << "Du valde insatsen 500:-" << endl;
cout << "Ditt saldo är " << saldo - bet << endl;
}
else
{
cout << "Getfucl :)" << endl;
break;
}
srand(time(0));
int vinst = rand() % 36 + 1;
cout << "\n";
cout << "-Vinsttabell: " << endl;
cout << "-Vinst: Färg ger 2x din insats." << endl;
cout << "-Vinst: Nummer ger 10x din insats." << endl;
cout << "\n";
cout << "Vill spela på färg [1] eller nummer [2]: " << endl;
cin >> val;
/*Här väljer jag 1 eller 2 och deklarerar "val" till det nummret
Om jag väljer 1 så kommer if-satsen under att köras och väljer jag 2 så kommer else if-satsen att köras
Om jag väljer 1 så kommer den att deklarera om "val" för den nya input som blir, i detta fall "r" eller "b"
och fortsätta att köras i en ny if-sats som hanterar inmatning 1 */
/*Här kör jag en if-sats för svaret 1 som är färg, identifieraren "val" deklareras då ett nytt värde som
matas in utav användaren som nu i sin tur får välja "r" eller "b". Det finns 2 if-satser för val 1, en för respektive färg, "r" och "b" som avgörs omÄ
random funktionen är delbar eller inte vilket talar om utifall svaret blev rött eller svart.*/
if (val == '1')
{
cout << "Vilken färg vill du spela? [r] för Röd och [b] för Svart " << endl;
cin >> val;
if (val == 'b')
{
if (vinst % 2 == 0)
{
cout << "Resultatet blev Svart: " << vinst << endl;
saldo = saldo + bet * 2 - bet;
cout << "Du vann: " << bet + bet << endl;
cout << "Ditt nya saldo är: " << saldo << endl;
}
else
{
cout << "Du förlorade resultatet blev Rött: " << vinst << endl;
saldo = saldo - bet;
cout << "Ditt nya saldo är: " << saldo << endl;
}
}
if (val == 'r')
{
if (vinst % 2 != 0)
{
cout << "Resultatet blev Rött: " << vinst << endl;
saldo = saldo + bet * 2 - bet;
cout << "Du vann: " << bet + bet << endl;
cout << "Ditt nya saldo är: " << saldo << endl;
}
else
{
cout << "Du förlorade resultatet blev Svart: " << vinst << endl;
cout << "Du förlorade: " << endl;
saldo = saldo - bet;
cout << "Ditt nya saldo är: " << saldo << endl;
}
}
}
/*Här hamnar du om du valde "2" i det första steget efter du valt insats. Även i denna if-sats finns det 2 andra if-satser ,*/
if (val == '2')
{
cout << "Vilket nummer?: ";
cin >> val;
if (vinst == val)
{
cout << "Grattis du vann: " << vinst << endl;
cout << "Du vann: " << bet * 10 << endl;
saldo = saldo + bet * 10 - bet;
cout << "Ditt nya saldo är " << saldo << endl;
}
else if (vinst!=val)
{
saldo = saldo - bet;
cout << "Fel nummer, det rätta nummret var: " << vinst << " - Ditt nya saldo är: " << saldo << endl;
}
}
do
{
cout << "Vill du spela igen?: j/n?: " << endl;
cin >> spelaigen;
} while (spelaigen == 'j' || spelaigen == 'J');
}
return 0;
}
This code seems wrong to me (but apologies I don't read Swedish)
do
{
cout << "Vill du spela igen?: j/n?: " << endl;
cin >> spelaigen;
} while (spelaigen == 'j' || spelaigen == 'J');
I think what you are doing is asking whether the user want to try again, yes or no? If that's right then all you need is an if statement which breaks out of the main loop if the answer is no.
cout << "Vill du spela igen?: j/n?: " << endl;
cin >> spelaigen;
if (spelaigen == 'n' || spelaigen == 'N')
break;
Now that code works but you could also check if the answer is yes or no and ask again if it is neither. That does require an extra loop and maybe that is what you were trying to do in your code. The correct code looks like this
do
{
cout << "Vill du spela igen?: j/n?: " << endl;
cin >> spelaigen;
}
while (spelaigen != 'j' && spelaigen != 'J' && spelaigen != 'n' && spelaigen != 'N');
if (spelaigen == 'n' || spelaigen == 'N')
break;
I usually find answers for my problems on this forum, but this time I can't. This is why I am asking you. (Sorry in advance for the mistakes I'm doing in english or c++)
So here is my void function, it is just the start of a little game I'm making in the console. It is just showing some informations. By the way it's a french game, but I don't think that a traduction is needed to solve my problem.
void showIntro (Personnage a, Personnage b, Personnage c)
{
cout << endl;
cout << "VOUS ENTREZ DANS LES MINES DE LA MORIA" << endl << endl;
cout << "ENNEMIS PRESENTS : " << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
a.afficherEtatIntro ();
cout << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
b.afficherEtatIntro ();
cout << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
c.afficherEtatIntro ();
cout << endl << endl;
cout << "Le combat va commencer";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << "." << endl << endl << endl;
cout << "Gimli : ";
string phraseGimli ("Je vengerai mes freres!");
for (int f = 0; f <= phraseGimli.size(); f++) // Just writing a sentence
{
cout << phraseGimli[f];
Sleep(100);
}
cout << endl << "Legolas : ";
string phraseLegolas ("Nous sommes avec toi mon ami");
for (int g = 0; g <= phraseLegolas.size(); g++) // Writing a sentence
{
cout << phraseLegolas[g];
Sleep(100);
}
cout << endl << "Aragorn : ";
string phraseAragorn ("POUR LE GONDOR");
for (int i = 0; i <= phraseAragorn.size(); i++) // Writing a sentence
{
cout << phraseAragorn[i];
Sleep(100);
}
}
And here is the other one.
void choixAttaque (Personnage joueur, Personnage a, Personnage b, Personnage c, string nom)
{
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << "Qui souhaitez vous attaquer ?" << endl;
cout << endl;
cout << endl;
cout << " " << nom << endl << endl << endl;
a.afficherEtatIntro ();
cout << endl << "Tapez 1" << endl;
cout << " " << nom << endl << endl << endl;
b.afficherEtatIntro ();
cout << endl << "Tapez 2" << endl;
cout << " " << nom << endl << endl << endl;
c.afficherEtatIntro ();
cout << endl << "Tapez 3" << endl;
int choix;
do
{
cin >> choix;
} while (choix <= 0 || choix >= 4);
switch (choix)
{
case 1:
joueur.attaquer(a);
case 2:
joueur.attaquer(b);
case 3:
joueur.attaquer(c);
}
}
afficherEtatIntro ()
void Personnage::afficherEtatIntro () const
{
cout << " " << "Vie : " << m_vie << endl;
cout << " ";
m_arme->afficher();
}
m_arme means m_weapon, I used "->" because it is from a pointer. I'm new to all of this, so it can be really possible that the mistake comes from here.
When I'm using only one, it works, but when I'm using both in a row, it does that.
first void function showing properly
And then.
Path of things in my computer
I hope I gave the informations needed to solve the problem, and I hope it can be solved. Thanks in advance for everything, and sorry for all the mistakes I may have made in english, c++ or by linking my lines of code. ^^'
(Btw, all the "cout << endl;" looks ugly I guess, but that is just until I learn how to use Qt..)
From debugger: "Program received signal SIGSEGV, Segmentation fault."
And a picture in comment. (Need more reputation for 3 links..)
I am trying to keep a running total of cups of coffee sold and I have to use a user defined function to do it. I have tried numerous variations of the attached code but nothing seems to work. What am I doing wrong? Also I am a newb to C++ so that is why it looks amateurish!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
void amtSold(int &smtCup, int &mdtCup, int &lgtCup);
int main()
{
int selection;
int smCup;
int mdCup;
int lgCup;
int smtCup;
int mdtCup;
int lgtCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
do
{
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
amtSold(smtCup, mdtCup, lgtCup);
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smCup << endl;
cout << "Medium: " << setw(17) << mdCup << endl;
cout << "Large: " << setw(18) << lgCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << smCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << mdCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << lgCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smCup*SM_OZ) + (mdCup*MD_OZ) + (lgCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE)) + (((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
} while (selection != 0);
system("pause");
return 0;
}
void amtSold(int &smtCup, int &mdtCup, int &lgtCup)
{
int smCup;
int mdCup;
int lgCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
}
So as you probably know, you're not keeping track of the number of coffee cups of each size that you're selling (i.e. smtCup, mdtCup, and lgtCup).
I'm assuming that these variables mean the total number of cups for each size, you might want to put some comments during the variable declaration step. You'll want to initialise the variables to 0:
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
As this is a fairly simple program, you can perform accumulation without using your amtSold function, so you can delete that.
Then, in case 1 of your switch statement, you'll want to update smtCup, mdtCup, and lgtCup every time you update the values. Please be aware that smCup, mdCup, and lgCup are used only for input in this program.
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
From here on out, you can print out the total number of small, medium and large cups by calling smtCup, mdtCup, and lgtCup in the other cases! Change smCup, mdCup, and lgCup to smtCup, mdtCup, and lgtCup in cases 2-4. Hope this helps!
Edit: Can't comment, so I'll just say you're welcome here!
Thanks KTing! It is disappointing to know I was much closer to a correct answer with an earlier version of my code. I could not figure out why it would not initialize and so I started to get desperate and try things I was 95% sure would not work. I ended up going with the following solution.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Constant for size of cup of coffee
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
//Constant for price of cup of coffee and Tax
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
int main()
{
//declare and initialize the variables for the individual cups of coffee
int selection;
int smCup = 0;
int mdCup = 0;
int lgCup = 0;
//declare and initialize the variables for the total cups of coffee
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
do
{
//get input from user as to what they want to do
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
//get the number of cups of coffee from the user
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
//get the total cups of coffee and store it as a variable
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smtCup << endl;
cout << "Medium: " << setw(17) << mdtCup << endl;
cout << "Large: " << setw(18) << lgtCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << smtCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << mdtCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << lgtCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smtCup*SM_OZ) + (mdtCup*MD_OZ) + (lgtCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << SM_PRICE << setw(18) << smtCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << MD_PRICE << setw(18) << mdtCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << LG_PRICE << setw(18) << lgtCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE)) + (((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
//loop through if the user is still making a valid selection
} while (selection != 0);
system("pause");
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm working on a school project, my project is a Cypher and Decypher program that works with Caesar Algorithm.
My program has to have the following characteristics:
The program must give the user the option to initiate again. (Done.)
If the user inputs something wrong, the program must ask the user to imput again. (Done with a bug.)
The user can only input letters and - por spaces. (I only need to add the space thing.)
The letter must be moved 5 places (ie. A = F) (Done.)
The program must consider capital letters. (Done.)
Each time you input a letter the screen must clear. (Done.)
At the end of the program you must be able to see the encrypted or decrypted text. (Needs to finish.)
What I need to know is how to make the program show me the encrypted text, counting all the inputs I have made so far while the program has been running.
And an extra thing is how can I input a whole text sentence and apply the Caesar Encryption, obviously I'd have to change almost all my code but I'd like how to do it.
This is the code I have so far, sorry if the sentences are in Spanish but I am living in Mexico for the moment and my classes are in Spanish. If you need me to translate the text I can gladly do it for you.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int, char**) {
// Variables.
char l;
int x = 0;
char y = x + l;
bool volverainiciar;
volverainiciar = 1;
while (volverainiciar == 1) {
cout << "Favor de introducir la letra del mensaje que desea codificar."
<< endl;
cin >> l;
cout << " " << endl;
cout << " " << endl;
if ((l >= 'a' && l <= 'u') || (l >= 'A' && l <= 'U')) {
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra " << l << " tiene un codigo ASCII de: " << x + l
<< endl;
y = (l + 5);
cout << "La letra " << l << " encriptado tiene un valor de: " << y
<< endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver a "
"iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
} else if ((l >= 'v' && l <= 'z') || (l >= 'V' && l <= 'Z')) {
switch (l) {
case 'v':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: a" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'w':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: b" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'x':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: c" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'y':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: d" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'z':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: e" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'V':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: A" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'W':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: B" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'X':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: C" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'Y':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: D" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
case 'Z':
cout << "El mensaje a codificar es: " << l;
cout << " " << endl;
cout << " " << endl;
cout << "La letra u tiene un codigo ASCII de: " << x + l
<< endl;
cout << "La letra " << l
<< " encriptado tiene un valor de: E" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Se limpiara la pantalla..." << endl;
system("pause");
system("cls");
cout << " Desea volver a iniciar?" << endl;
cout << " " << endl;
cout << " Para volver a iniciar (1) , para no volver "
"a iniciar (0)" << endl;
cout << " " << endl;
cout << "Volver a iniciar: ";
cin >> volverainiciar;
cout << " " << endl;
cout << " " << endl;
break;
}
} else {
cout << "Ese simbolo no es valido, favor de introducir uno valido."
<< endl;
cin >> l;
cout << " " << endl;
cout << " " << endl;
continue;
}
}
cout << "Gracias por usar el encriptador/desencriptador." << endl;
exit;
return 0;
}
If you have any suggestions I'll gladly read them too :)
And sorry for any misspellings my english is not perfect because almost my whole life I've been living here...
I hate to trash your really long program, but it was annoying.
Try this:
// Given the input character is in x.
if (std::isalpha(x))
{
if (std::islower(x))
{
y = (x - 'a'); // Convert to a number.
y = y + 5; // Left shift by 5
y = y % 26; // Modulo arithmetic for all letters in alphabet
y = y + 'a'; // Convert back to character.
}
else
{
y = (x - 'A'); // Convert to a number.
y = y + 5; // Left shift by 5
y = y % 26; // Modulo arithmetic for all letters in alphabet
y = y + 'A'; // Convert back to character.
}
}
One of my pet peeves is duplicate code. Although there is duplicate code above, I've reduced the amount of duplication in your code. Feel free to add all your cout statements in the code above.
By the way, you can "block write" your data to cout, if the data doesn't use variables.
Example:
static const char answer_text[] =
"\n"
"\n"
"La letra u tiene un codigo ASCII de: ";
//...
cout.write(answer_text, sizeof(answer_text) - 1);
This will allow you to use the cout.write statement everywhere you need to use the same text. Less typing, less lines, less probability of injecting a defect.
Hello I am working on an assignment. I have spent numerous hours and cannot figure out why at the end of my program the "Total Commission" and "Total Due" are not adding up. I am a beginner so anything that could lead me in the right direction would be greatly appreciated. Thanks.
original code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
PC_COMMISSION = 0.12,
PRINTER_BASE_SALES = 2500.00,
PRINTER_COMMISSION = 0.10,
ACCESSORIE_BASE_SALES = 2000.00,
ACCESSORIE_COMMISSION = 0.10,
MAINTENANCE_BASE_SALES = 1500.00,
MAINTENANCE_COMMISSION = 0.06;
double baseSalary = 0.00,
pcSales = 0.00,
printerSales = 0.00,
accessorieSales = 0.00,
maintenanceSales = 0.00,
totalCommission = 0.00,
totalDue = 0.00,
noCommission = 0.00,
pcCommission = baseSalary * PC_COMMISSION,
printerCommission = baseSalary * PRINTER_COMMISSION,
accessorieCommission = baseSalary * ACCESSORIE_COMMISSION,
maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
int idNumber; //salesman identification number
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
cout << "Please Enter Personal Computer Sales\t\t\t ";
cin >> pcSales;
cout << "Please Enter Printer Sales\t\t\t\t ";
cin >> printerSales;
cout << "Please Enter Accessories Sales\t\t\t\t ";
cin >> accessorieSales;
cout << "Please Enter Maintenance Sales\t\t\t\t ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t ********************************\n";
cout << endl;
cout << fixed;
cout << setprecision(2);
cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;
cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;
while (pcSales > PC_BASE_SALES)
{
double pcCommission = baseSalary * PC_COMMISSION;
cout << right << setw(20) << pcCommission;
cout << "\n" << endl;
pcCommission += totalCommission;
break;
}
while (pcSales < PC_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
while (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
break;
}
while (printerSales < PRINTER_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
while (accessorieSales > ACCESSORIE_BASE_SALES)
{
double accessorieCommission = baseSalary * ACCESSORIE_COMMISSION;
cout << right << setw(20) << accessorieCommission;
cout << "\n" << endl;
accessorieCommission += totalCommission;
break;
}
while (accessorieSales < ACCESSORIE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
while (maintenanceSales > MAINTENANCE_BASE_SALES)
{
double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
cout << right << setw(20) << maintenanceCommission;
cout << "\n" << endl;
maintenanceCommission += totalCommission;
break;
}
while (maintenanceSales < MAINTENANCE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
cout << endl;
break;
}
cout << endl;
totalCommission = pcCommission + printerCommission + accessorieCommission +
maintenanceCommission;
totalDue = baseSalary + totalCommission;
cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;
cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;
cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;
} while ( idNumber != -999);
system ("PAUSE");
return 0;
updated code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
PC_COMMISSION = 0.12,
PRINTER_BASE_SALES = 2500.00,
PRINTER_COMMISSION = 0.10,
ACCESSORIE_BASE_SALES = 2000.00,
ACCESSORIE_COMMISSION = 0.10,
MAINTENANCE_BASE_SALES = 1500.00,
MAINTENANCE_COMMISSION = 0.06;
double baseSalary = 0.00,
pcSales = 0.00,
printerSales = 0.00,
accessorieSales = 0.00,
maintenanceSales = 0.00,
totalCommission = 0.00,
totalDue = 0.00,
noCommission = 0.00,
pcCommission = baseSalary * PC_COMMISSION,
printerCommission = baseSalary * PRINTER_COMMISSION,
accessorieCommission = baseSalary * ACCESSORIE_COMMISSION,
maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
int idNumber; //salesman identification number
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
cout << "Please Enter Personal Computer Sales\t\t\t ";
cin >> pcSales;
cout << "Please Enter Printer Sales\t\t\t\t ";
cin >> printerSales;
cout << "Please Enter Accessories Sales\t\t\t\t ";
cin >> accessorieSales;
cout << "Please Enter Maintenance Sales\t\t\t\t ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t ********************************\n";
cout << endl;
cout << fixed;
cout << setprecision(2);
cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;
cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;
if (pcSales > PC_BASE_SALES)
{
double pcCommission = baseSalary * PC_COMMISSION;
cout << right << setw(20) << pcCommission;
cout << "\n" << endl;
totalCommission += pcCommission;
}
if (pcSales < PC_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
if (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
totalCommission += printerCommission;
}
if (printerSales < PRINTER_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
if (accessorieSales > ACCESSORIE_BASE_SALES)
{
double accessorieCommission = baseSalary * ACCESSORIE_COMMISSION;
cout << right << setw(20) << accessorieCommission;
cout << "\n" << endl;
totalCommission += accessorieCommission;
}
if (accessorieSales < ACCESSORIE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
if (maintenanceSales > MAINTENANCE_BASE_SALES)
{
double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
cout << right << setw(20) << maintenanceCommission;
cout << "\n" << endl;
totalCommission += maintenanceCommission;
}
if (maintenanceSales < MAINTENANCE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
cout << endl;
}
cout << endl;
totalCommission = pcCommission + printerCommission + accessorieCommission + maintenanceCommission;
totalDue = baseSalary + totalCommission;
cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;
cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;
cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;
} while ( idNumber != -999);
system ("PAUSE");
return 0;
}
Are you intentionally writing code for a obfuscated C++ competition?
This (as an example of several similar ones):
while (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
break;
}
is exactly the same as :
if (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
}
since the break is unconditional inside the loop, and thus makes the loop only ever run once. This is possibly what you wanted in the first place.
Also, since printerCommission is a local variable inside the loop, this will not do anything useful.
printerCommission += totalCommission;
did you mean:
totalCommission += printerCommission;
Also, as the comments say, using double or float to calculate money will eventually go wrong, because floating point values will sometimes round off incorrectly. For example, in binary floating point representation, the values 0.1, 0.2 and 0.6 and 0.7 would be 0.099999999999999, 0.199999999999999, 0.59999999999999 and 0.699999999999 respectively. It's the same as trying to express 1/3 in decimals, 0.333333333 - no matter how many digits are used, it can not be "exactly 1/3". If you do enough math using such values, it will end up "wrong". But you should get approximately the right value, give or take a cent or whatever the small part of the currency is called.
Here is an issue I found:
An else statement without { and } will only execute the next statement:
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else // <--- This else will cause only the next line to be executed
// since there is no '{' following it.
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
Have you single stepped through the code with a debugger?