Cannot exit a while loop, just continue it - c++
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;
Related
How can I do to keep the value of a variable after a do while loop?
I have been coding a program to simulate a roulette of a casino, thing is that every time I try to repeat the game after is finished I want the game to keep going and the money to be the same, so if you have lost money you start with that certain money, here is the code (It's in Spanish but I think it's pretty clear): #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int num, pri, randum, num2, op, num3 = 10000, col = randum, rep, clear; int main() { do { int num4 = op; cout << "Escoja la opción de la que apostar.\n"; cout << "1 - Apostar a un número. \n2 - Apostar a un color \n"; cout << "Elija opción: "; cin >> pri; cout << " \n"; cout << " \n"; switch (pri) { case 1: { srand(time(0)); randum = rand() % 37 + 1; //si poner 37 + 1 te va cojer números hasta el 37 no? if (num4 != 10000) { cout << "Su saldo actual es " << num3 << " €\n"; } else { cout << "Su saldo actual es 10000 €\n"; } cout << "Ha elegido apostar a un número\n"; cout << "Introduzca el dinero que quiere apostar -->\n"; cin >> num; cout << "Ahora introduzca el número que desee entre el 0 y 36 -->\n"; cin >> num2; if (num2 == randum) { op = num3 + num; cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << op << " €\n"; } else { op = num3 - num; cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << op << " €\n"; cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n"; cin >> clear; if (clear == 1) {} else if (clear == 2) { cout << "Bien, suerte en la próxima tirada.\n\n"; } } break; } case 2: { if (num3 == 10000) { cout << "Su saldo actual es 10000 €\n"; } else { cout << "Su saldo actual es " << num3 << " €\n"; } cout << "Ha elegido apostar a un color\n"; cout << "Introduzca el dinero que quiere apostar -->\n"; cin >> num; srand(time(0)); randum = rand() % 2 + 1; cout << "Ahora escoja rojo (1) o negro (2) -->\n"; cin >> col; if (col == randum) { op = num3 + num; cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << op << " €"; } else { op = num3 - num; cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << op << " €"; } cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n"; cin >> clear; if (clear == 1) {} else if (clear == 2) { cout << "Bien, suerte en la próxima tirada.\n\n"; } } } } while (clear == 1); return 0; }
So, it should be pretty easy to do that. Initialize the starting amount outside the loop before the betting begins. At the end of the loop, ask if user wants to bet more. Would that work for you? Or do you need it to be initialized when you start the code itself? You could use static I am just changing a few things from your code: #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { int money = 10000, bet_amount = 0, clear, pri; cout << "Su saldo inicial es " << money << " €\n"; do { cout << "Escoja la opción de la que apostar.\n"; cout << "1 - Apostar a un número. \n2 - Apostar a un color \n"; cout << "Elija opción: "; cin >> pri; cout << " \n"; cout << " \n"; cout << "Introduzca el dinero que quiere apostar -->\n"; cin >> bet_amount; switch (pri) { case 1: { int number_chosen = -1, randum; cout << "Ahora introduzca el número que desee entre el 0 y 36 -->\n"; cin >> number_chosen; srand(time(0)); randum = rand() % 37; // This will give result in the range 0 - 36 if (randum == number_chosen) { money += bet_amount; cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << money << " €\n"; } else { money -= bet_amount; cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << money << " €\n"; } break; } case 2: { int color = 0, randcol; cout << "Ahora escoja rojo (1) o negro (2) -->\n"; cin >> color; srand(time(0)); randcol = rand() % 2 + 1; if (randcol == color) { money += bet_amount; cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << money << " €\n"; } else { money -= bet_amount; cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << money << " €\n"; } break; } default: break; } cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n"; cin >> clear; if (clear == 2) { cout << "Bien, suerte en la próxima tirada.\n\n"; } } while (clear == 1); cout << "Tu saldo final es " << money << " €\n"; return 0; } It took me a while to figure out the code because I had to use google translate
I suggest you store the money into a file Like this : #include <fstream> ofstream myfile ("money.txt"); if (myfile.is_open()) { myfile << "put the money in the bag here"; myfile.close(); } else cout << "Unable to open file"; And whenever you want to read the value Use this: string line; ifstream myfile ("money.txt"); if (myfile.is_open()) { getline (myfile,line); cout << line << '\n'; myfile.close(); } else cout << "Unable to open file";
While loop not working even when condition is satisfied
I have an assignment to write a C++ program for basic atm functioning. There is a rule to be followed that in one day, a max of 50,000 cash can flow through the atm, that includes all cash transfers, withdrawals or fast cash. I put a while loop over the whole body which uses a "atmlimit" integer to test the condition. Every successful cash transaction gets added into it but the loop is not working for some reason. #include <iostream> using namespace std; int main() { int pincode, pindigit1; int availbalance, userchoice; int transmoney, inputFC, ubinput; int cwmoney, ctmoney, spam; int atmlimit; cout << "\n Please enter your 4-digit pin code \n Enter 'break' at any point to close the process \n" << endl; cin >> pincode; pindigit1 = pincode; for (pindigit1; pindigit1 >= 10; pindigit1 /= 10); availbalance = 10000 * pindigit1; while (atmlimit <= 50000) { menu: cout << "\n *******************************************************************************" << endl; cout << "\n * Please choose the service your require by entering its respective number *" << endl; cout << "\n * [1] Fast Cash *" << endl; cout << "\n * [2] Cash Withdrawl *" << endl; cout << "\n * [3] Balance Inquiry *" << endl; cout << "\n * [4] Cash Transfer *" << endl; cout << "\n ******************************************************************************* \n" << endl; cin >> userchoice; if (userchoice == 1) { ta: cout << "\n Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl; cout << "\n [1] 500Rs [2] 1000Rs" << endl; cout << "\n [3] 2000Rs [4] 3000Rs" << endl; cout << "\n [5] 4000Rs [6] 5000Rs" << endl; cout << "\n [7] 10000Rs [8] 20000Rs \n" << endl; cin >> inputFC; if (inputFC == 8) { transmoney = 20000; } if (inputFC == 7) { transmoney = 10000; } if (inputFC == 6) { transmoney = 5000; } if (inputFC == 5) { transmoney = 4000; } if (inputFC == 4) { transmoney = 3000; } if (inputFC == 3) { transmoney = 2000; } if (inputFC == 2) { transmoney = 1000; } if (inputFC == 1) { transmoney = 500; } if (inputFC == 0) { goto menu; } if (transmoney <= availbalance) { cout << "\n Your transaction completed successfully" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += transmoney; availbalance -= transmoney; goto menu; } if (transmoney > availbalance) { cout << "\n You have insufficient balance to perform this transaction. Enter '0' to go back the menu or '1' to change the transaction amout" << endl; cin >> ubinput; } if (ubinput == 0) { goto menu; } if (ubinput == 1) { goto ta; } } if (userchoice == 2) { cwmenu: cout << "\n Please enter the amount of cash you want to withdraw from your bank account \n Enter 0 if you want to return to the menu \n" << endl; cin >> cwmoney; cout << cwmoney; //TBR if (cwmoney == 0) { goto menu; } if (cwmoney <= availbalance) { cout << "\n Your transaction bas been processed, Thank you!\n" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += cwmoney; availbalance -= cwmoney; goto menu; } else if (cwmoney > availbalance) { cout << "\n You have insufficient balance for this transaction. \n" << endl; goto cwmenu; } } if (userchoice == 3) { cout << "\n Your current bank balance is " << availbalance << "Rs" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; goto menu; } if (userchoice == 4) { ctmenu: cout << "\n Enter the receiver's 6 digit bank account number \n Enter 0 to return to the menu" << endl; cin >> spam; if (spam == 0) { goto menu; } cout << "\n Please enter the amount of cash you want to transfer/send \n Enter 0 if you want to return to the menu \n" << endl; cin >> ctmoney; if (ctmoney == 0) { goto menu; } cout << "\n" << endl; cout << ctmoney << endl; //TBD if (ctmoney <= availbalance) { cout << "\n Your transaction bas been processed, Thank you!\n" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += availbalance; availbalance -= ctmoney; goto menu; } else if (ctmoney > availbalance) { cout << "\n You have insufficient balance for this transaction. \n" << endl; goto ctmenu; } } } cout << "\n You have reached the maximum amount of cash flow through your account, Please try later \n" << endl; } After all the suggestions, I re-wrote the code like this: #include <iostream> using namespace std; int main() { int pincode, pindigit1; int availbalance, userchoice; int transmoney, inputFC; int cwmoney, ctmoney; int atmlimit = 0; cout << "\n Please enter your 4-digit pin code \n Enter 'break' at any point to close the process \n" << endl; cin >> pincode; pindigit1 = pincode; for (pindigit1 = pincode; pindigit1 >= 10; pindigit1 /= 10) ; cout << pindigit1 << endl; //TBR availbalance = 10000 * pindigit1; cout << availbalance << endl; //TBR while (atmlimit <= 50000) { cout << "\n *******************************************************************************" << endl; cout << "\n * Please choose the service you require by entering its respective number *" << endl; cout << "\n * [1] Fast Cash *" << endl; cout << "\n * [2] Cash Withdrawl *" << endl; cout << "\n * [3] Balance Inquiry *" << endl; cout << "\n * [4] Cash Transfer *" << endl; cout << "\n ******************************************************************************* \n" << endl; cin >> userchoice; switch (userchoice) { case 1: cout << "\n Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl; cout << "\n [1] 500Rs [2] 1000Rs" << endl; cout << "\n [3] 2000Rs [4] 3000Rs" << endl; cout << "\n [5] 4000Rs [6] 5000Rs" << endl; cout << "\n [7] 10000Rs [8] 20000Rs \n" << endl; cin >> inputFC; if (inputFC == 8) { transmoney = 20000; } if (inputFC == 7) { transmoney = 10000; } if (inputFC == 6) { transmoney = 5000; } if (inputFC == 5) { transmoney = 4000; } if (inputFC == 4) { transmoney = 3000; } if (inputFC == 3) { transmoney = 2000; } if (inputFC == 2) { transmoney = 1000; } if (inputFC == 1) { transmoney = 500; } if (inputFC == 0) { continue; } if (transmoney <= availbalance) { cout << "\n Your transaction completed successfully" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += transmoney; availbalance -= transmoney; cout << atmlimit << endl; //TBD cout << availbalance; //TBD continue; } if (transmoney > availbalance) { cout << "\n You have insufficient balance to perform this transaction" << endl; continue; } break; case 2: cout << "\n Please enter the amount of cash you want to withdraw from your bank account \n Enter 0 if you want to return to the menu \n" << endl; cin >> cwmoney; cout << cwmoney; //TBR if (cwmoney == 0) { continue; } if (cwmoney <= availbalance) { cout << "\n Your transaction bas been processed, Thank you!\n" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += cwmoney; availbalance -= cwmoney; cout << atmlimit << endl; cout << availbalance; continue; } else if (cwmoney > availbalance) { cout << "\n You have insufficient balance for this transaction. \n" << endl; continue; } break; case 3: cout << "\n Your current bank balance is idk" << availbalance << "Rs" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; break; case 4: cout << "\n Please enter the amount of cash you want to transfer/send \n Enter 0 if you want to return to the menu \n" << endl; cin >> ctmoney; if (ctmoney == 0) { continue; } cout << "\n" << endl; cout << ctmoney << endl; //TBD if (ctmoney <= availbalance) { cout << "\n Your transaction bas been processed, Thank you!\n" << endl; cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl; atmlimit += ctmoney; availbalance -= ctmoney; cout << atmlimit << endl; //TBD cout << availbalance; continue; } else if (ctmoney > availbalance) { cout << "\n You have insufficient balance for this transaction. \n" << endl; continue; } break; } } cout << "\n You have reached the maximum amount of cash flow through your account, Please try later \n" << endl; }
Fixed the issue by replacing goto statements with break/continue and some other small tweaks as mentioned in the comments.
Display error message in a program I made in c++
Newbie in programming. Want to add a message that say something like wrong input .. but not sure how to do it. The program works but want to add error message so the user can't press wrong button have tried like an if statement inside the while loop like if (input != "A" && input != "a" && input != "S" && input != "s") cout << "not valid options << endl; and please tell if I can make the code better in any way , always good to know if i am doing things in a good way or not. here is my code to the program #include <cmath> #include "stdafx.h" #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { locale swedish("swedish"); // tar in svenska tecken locale::global(swedish); int marker = 100; // marker som man börjar med int num1, num2, num3; // numren som ska slumpas senare string input; cout << "Välkommen till mitt slumptalspel!!"<< endl ; cout << " Detta spel går ut på att man ska få mer än 450" << endl; cout << "så får man mer marker och får man mindre än 450 så förlorar man marker" << endl; cout << " får man mer än 200 marker så vinner man !!" << endl; cout << " och tar markerna slut så har man förlorat" << endl; while (input != "A" && input != "a") { cout << "Du har " << marker << " marker. spela tryck [S] avsluta tryck [A]"<< endl; cin >>input; // här bestämmer sig spelaren för om han vill spela eller inte med s eller a if (input == "S" || input == "s") { num1 = rand() % 350 + 1; // här slumpas talen ut num2 = rand() % 350 + 1; num3 = rand() % 350 + 1; cout << "[" << num1 << "][" << num2 << "][" << num3 << "]" << endl; // här skrivs num1 till 3 ut vad dom får för värden srand(time(0)); if (num1 + num2 + num3 >= 450) // om väret tillsammans blir mer än 450 { marker += 12; // lägg till 12 marker cout << "du vann 12 marker =) " << endl; } else { cout << " Du förlorade 50 marker" << endl; // annars ta bort 50 marker marker -= 50; } if (marker <= 0) // om markerna är 0 eller mindre { // så är spelet slut cout << " Spelet är slut du har inga marker kvar" << endl; return 0; } if (marker >= 200 ) { cout << " du vann GRATTIS!!" << endl; // om man får 200 eller mer marker så vinner man och spelet avslutas sedan return 0; } } } cout << "Spelet avslutas " << endl; // om man avbryter med A så avslutas det så här med detta meddelande return 0; } Did try an switch as someone here said to me, then the problem is that it never stops, when the marker is 0 or less or 200 it should stop but it doesent, I did the code like this #include "stdafx.h" #include <cmath> #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { locale swedish("swedish"); // tar in svenska tecken locale::global(swedish); int marker = 100; // marker som man börjar med int num1, num2, num3; // numren som ska slumpas senare char input; int menu(); cout << "Välkommen till mitt slumptalspel!!" << endl; cout << " Detta spel går ut på att man ska få mer än 450" << endl; cout << "så får man mer marker och får man mindre än 450 så förlorar man marker" << endl; cout << " får man mer än 200 marker så vinner man !!" << endl; cout << " och tar markerna slut så har man förlorat" << endl; do { cout << "Du har " << marker << " marker. spela tryck [S] avsluta tryck [A]" << endl; cin >> input; switch (input) { case 'A': cout << " spelet avslutas" << endl; return 0; case 'S': num1 = rand() % 350 + 1; // här slumpas talen ut num2 = rand() % 350 + 1; num3 = rand() % 350 + 1; cout << "[" << num1 << "][" << num2 << "][" << num3 << "]" << endl; // här skrivs num1 till 3 ut vad dom får för värden srand(time(0)); if (num1 + num2 + num3 >= 450) // om väret tillsammans blir mer än 450 { marker += 12; // lägg till 12 marker cout << "du vann 12 marker =) " << endl; break; } else { cout << " Du förlorade 50 marker" << endl; // annars ta bort 50 marker marker -= 50; break; } if (marker <= 0) // om markerna är 0 eller mindre { // så är spelet slut cout << " Spelet är slut du har inga marker kvar" << endl; return 0; } if (marker >= 200) { cout << " du vann GRATTIS!!" << endl; // om man får 200 eller mer marker så vinner man och spelet avslutas sedan return 0; } default: cout << " nope inget giltigt val " << endl; // Om man väljer ett alternativ som inte finns så kommer detta meddelande fram break; } } while (input != 'q' ) ; }
You can use switch statements to make it better, and in the default case, you can print your desired output: switch(input) { case 'A': //Do something and break. case 'a': //Do something and break. . . . default: cout<<"not valid options" << endl; }
I fixed it , #include "stdafx.h" #include <cmath> #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { locale swedish("swedish"); // tar in svenska tecken locale::global(swedish); int marker = 100; // marker som man börjar med int num1, num2, num3; // numren som ska slumpas senare char input; cout << "Välkommen till mitt slumptalspel!!" << endl; cout << " Detta spel går ut på att man ska få mer än 450" << endl; cout << "så får man mer marker och får man mindre än 450 så förlorar man marker" << endl; cout << " får man mer än 200 marker så vinner man !!" << endl; cout << " och tar markerna slut så har man förlorat" << endl; cout << " tre tal kommer att slumpas och varje tal kan vara mellan 1 och 350" << endl; cout << " LYCKA TILL!!!" << endl; do { if (marker <= 0) // om markerna är 0 eller mindre { // så är spelet slut cout << " Spelet är slut du har inga marker kvar" << endl; return 0; } if (marker >= 200) { cout << " du vann GRATTIS!!" << endl; // om man får 200 eller mer marker så vinner man och spelet avslutas sedan return 0; } cout << "Du har " << marker << " marker. spela tryck [S] avsluta tryck [A]" << endl; cin >> input; switch (input) { case 'A': cout << " spelet avslutas" << endl; return 0; case 'a': cout << " spelet avslutas" << endl; return 0; case 'S': num1 = rand() % 350 + 1; // här slumpas talen ut num2 = rand() % 350 + 1; num3 = rand() % 350 + 1; cout << "[" << num1 << "][" << num2 << "][" << num3 << "]" << endl; // här skrivs num1 till 3 ut vad dom får för värden srand(time(0)); if (num1 + num2 + num3 >= 450) // om väret tillsammans blir mer än 450 { marker += 12; // lägg till 12 marker cout << "du vann 12 marker =) " << endl; break; } else { cout << " Du förlorade 50 marker" << endl; // annars ta bort 50 marker marker -= 50; break; } case 's': num1 = rand() % 350 + 1; // här slumpas talen ut num2 = rand() % 350 + 1; num3 = rand() % 350 + 1; cout << "[" << num1 << "][" << num2 << "][" << num3 << "]" << endl; // här skrivs num1 till 3 ut vad dom får för värden srand(time(0)); if (num1 + num2 + num3 >= 450) // om väret tillsammans blir mer än 450 { marker += 12; // lägg till 12 marker cout << "du vann 12 marker =) " << endl; break; } else { cout << " Du förlorade 50 marker" << endl; // annars ta bort 50 marker marker -= 50; break; } default: cout << " nope inget giltigt val försök igen " << endl; // Om man väljer ett alternativ som inte finns så kommer detta meddelande fram break; } } while (input != 'QK') ; }
else if expected expression c++ / Xcode
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; }
Need help searchfunction for my moviedatabase C++
I'm having some trouble with my moviedatabase, my searchfunction isn't working. I'm a total beginner and have gotten the searchfunction of the net, but it wont search. Here's my code. #include <iostream> #include <vector> #include <sstream> // För att kunna använda sig av getline() #include <string> // För att kunna använda sig av getline() #include <algorithm> using namespace std; class Film { public: string Titel; string Typ; // FUNKTION: Skriver ut filmens titel och typ void skrivUtFilm() { cout << endl; cout << "Titel: " << Titel << endl; // Skriver ut filmens titel cout << "Typ: " << Typ << endl; // Skriver ut filmens typ } }; int main() { vector<Film> Databas; // Skapa en vektor av typen Film Film nyFilm; // För att kunna skapa en film behöver vi kunna spara den while (true) { // Skriver ut menyn: cout << " === FILMDATABAS === " << endl; cout << "1: Registera en ny film" << endl; cout << "2: S\x94k efter en film" << endl; cout << "3: Skriv ut filmlistan" << endl; cout << "0: Avsluta" << endl; cout << "Ange ditt val: "; char menySelection; // Användarens val cin >> menySelection; if (menySelection == '1') // Registerar nya filmer { cin.get(); cout << "\n\n1: Registrera en ny film\n"; cout << "Vad \x84r filmens Titel: "; getline(cin, nyFilm.Titel); cout << "\x8er filmen p\x86 DVD eller BluRay? "; getline(cin, nyFilm.Typ); cout << endl; // Vi lägger vårt filmobjekt i vektorn Databas.push_back( nyFilm ); } else if (menySelection == '2') // Söker efter filmer som användaren har lagt till { cin.get(); cout << "\nS\x94k efter filmer i databasen: \n\n"; string objekt; getline(cin, objekt); cout << endl; vector<Film>::iterator it; it = find_if(Databas.begin(), Databas.end(),[&] ( const Film &f ) { return ( f.Titel == objekt); }); } else if (menySelection == '3') // Skrivet ut alla filmer i Filmarkivet { cout << "\n=========\n"; cout << "Filmlistan\n"; for(int i=0; i<Databas.size(); i++) { Databas[i].skrivUtFilm(); // Skriver ut dem med hjälp av skrivUtFilm(); } cout << "\n=========\n"; cout << "\n\nTryck p\x86 ENTER f\x94r att \x86terv\x84nda till menyn.\n\n"; cin.get(); } else if (menySelection == '0') // Avslutar programmet { break; } else { cout << "Ogiltigt val!\n." << endl; } } cin.get(); return 0; }
When you find the film, you don't do anything with it. After the search function, you should add something like: if (it != Databas.end()) { it->skrivUtFilm(); } else { cout << "No film found" << endl; } to print info about the film that you have found, or a message that no film was found.