error shows up on the third line if(ungefarlika(a,b)){
double a = 0.3;
double b = 0.1 + 0.1 + 0.1;
if ( ungefarlika(a,b) ){
cout << "a och b har ungefär samma värden" << endl;
if (a==b)
cout << "De har samma värden" << endl;
else {
cout << "Men bara ungefär" << endl;
}
else cout << "a och b är inte särskilt lika" << 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 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";
I'm trying my best to get through a C++ online class and I'm currently writing a program to help a local restaurant automate its breakfast billing system. The program should do the following:
A. Show the customer the different breakfast items offered by the restaurant.
B. Allow the customer to select more than one item from the menu.
C. Calculate and print the bill.
Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):
Plain Egg 1.45
Bacon & Egg 2.45
Muffin 0.99
French Toast 1.99
Fruit Basket 2.49
Cereal 0.69
Coffee 0.50
Tea 0.75
My code is at the bottom, my problem is with the while loop in the showMenu function. As an example say the user wants to order a Plain Egg. Then it will run line 104 orderTotal = menuList[0].menuPrice + orderTotal. That should mean 0 = 1.45 + 0 right? Afterwards it runs orderTax = orderTotal * 0.05 + orderTax. Which should mean 0 = 1.45 * 0.05 + 0 if I'm correct? My trouble is that both my orderTotal and orderTax remain at 0 when I cout << them. What can I do to fix this? I also know that my menuPrice variable is working because it displays their values on the menu. Thanks for taking the time to read my post.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
void getData(menuItemType menuList[]);
void showMenu(menuItemType menuList[], double orderTotal, double orderTax, int orderCount[]);
void printCheck(menuItemType menuList[], double orderTotal, double orderTax, int orderCount[]);
int main()
{
menuItemType menuList[8];
double orderTotal = 0;
double orderTax = 0;
int orderCount[8]{ 0,0,0,0,0,0,0,0 };
getData(menuList);
showMenu(menuList, orderTotal, orderTax, orderCount);
printCheck(menuList, orderTotal, orderTax, orderCount);
return 0;
system("pause");
}
void getData(menuItemType menuList[]) // This function's purpose is to fill the array menuList[8] with the name and price of each item.
{
menuItemType plainEgg;
menuItemType baconEgg;
menuItemType muffin;
menuItemType frenchToast;
menuItemType fruitBasket;
menuItemType cereal;
menuItemType coffee;
menuItemType tea;
plainEgg.menuItem = "Plain Egg";
plainEgg.menuPrice = 1.45;
baconEgg.menuItem = "Bacon & Egg";
baconEgg.menuPrice = 2.45;
muffin.menuItem = "Muffin";
muffin.menuPrice = 0.99;
frenchToast.menuItem = "French Toast";
frenchToast.menuPrice = 1.99;
fruitBasket.menuItem = "Fruit Basket";
fruitBasket.menuPrice = 2.49;
cereal.menuItem = "Cereal";
cereal.menuPrice = 0.69;
coffee.menuItem = "Coffee";
coffee.menuPrice = 0.50;
tea.menuItem = "Tea";
tea.menuPrice = 0.75;
menuList[0] = plainEgg;
menuList[1] = baconEgg;
menuList[2] = muffin;
menuList[3] = frenchToast;
menuList[4] = fruitBasket;
menuList[5] = cereal;
menuList[6] = coffee;
menuList[7] = tea;
}
void showMenu(menuItemType menuList[], double orderTotal, double orderTax, int orderCount[]) // This function's purpose is to display the menu to the user for orders and calculate the total.
{
//Displays menu
cout << "\t" << "Welcome to Johnny's Restaurant" << endl;
cout << "\t" << "What would you like to order?" << endl;
cout << "\n" << endl;
cout << "\t" << "1. " <<menuList[0].menuItem << setw(10) << "$" << menuList[0].menuPrice << endl;
cout << "\t" << "2. " <<menuList[1].menuItem << setw(8) << "$" << menuList[1].menuPrice << endl;
cout << "\t" << "3. " <<menuList[2].menuItem << setw(13) << "$" << menuList[2].menuPrice << endl;
cout << "\t" << "4. " <<menuList[3].menuItem << setw(7) << "$" << menuList[3].menuPrice << endl;
cout << "\t" << "5. " <<menuList[4].menuItem << setw(7) << "$" << menuList[4].menuPrice << endl;
cout << "\t" << "6. " <<menuList[5].menuItem << setw(13) << "$" << menuList[5].menuPrice << endl;
cout << "\t" << "7. " <<menuList[6].menuItem << setw(13) << "$" << menuList[6].menuPrice << endl;
cout << "\t" << "8. " <<menuList[7].menuItem << setw(16) << "$" << menuList[7].menuPrice << endl;
cout << "\n" << endl;
bool orderActive = true; {};
int itemChoice;
//Prompts the user to pick from the menu
while (orderActive == true)
{
cout << "Select the number pertaining to which item you would like or press [0] to finish: ";
cin >> itemChoice;
if (itemChoice == 1)
{
orderTotal = menuList[0].menuPrice + orderTotal;
orderTax = orderTotal * 0.05 + orderTax;
orderCount[0] = orderCount[0] + 1;
cout << menuList[0].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 2)
{
orderTotal = menuList[1].menuPrice + orderTotal;
orderTax = orderTotal * 0.05 + orderTax;
orderCount[1] = orderCount[1] + 1;
cout << menuList[1].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 3)
{
orderTotal = menuList[2].menuPrice + orderTotal;
orderTax = orderTotal * 0.05 + orderTax;
orderCount[2] = orderCount[2] + 1;
cout << menuList[2].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 4)
{
orderTotal = menuList[3].menuPrice + orderTotal;
orderTax = orderTotal * 0.05 + orderTax;
orderCount[3] = orderCount[3] + 1;
cout << menuList[3].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 5)
{
orderTotal = menuList[4].menuPrice + orderTotal;
orderTotal * 0.05 + orderTax;
orderCount[4] = orderCount[4] + 1;
cout << menuList[4].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 6)
{
orderTotal = menuList[5].menuPrice + orderTotal;
orderTotal * 0.05 + orderTax;
orderCount[5] = orderCount[5] + 1;
cout << menuList[5].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 7)
{
orderTotal = menuList[6].menuPrice + orderTotal;
orderTotal * 0.05 + orderTax;
orderCount[6] = orderCount[6] + 1;
cout << menuList[6].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 8)
{
orderTotal = menuList[7].menuPrice + orderTotal;
orderTotal * 0.05 + orderTax;
orderCount[7] = orderCount[7] + 1;
cout << menuList[7].menuItem << " has been added to your order." << endl;
}
else if (itemChoice == 0)
{
orderActive = false;
}
else
{
cout << "Error Try Again" << endl;
}
}
cout << endl;
}
void printCheck(menuItemType menuList[], double orderTotal, double orderTax, int orderCount[]) // This function's purpose is to display the user's total
{
cout << "\n" << endl;
cout << "\t" << "Welcome to Johnny's Restaurant" << endl;
for (int x = 0; x < 1; x++)
{
if (orderCount[0] > 0)
{
cout << "\t" << orderCount[0] << setw(10) << menuList[0].menuItem << setw(15) << "$" << menuList[0].menuPrice << endl;
}
if (orderCount[1] > 0)
{
cout << "\t" << orderCount[1] << setw(12) << menuList[1].menuItem << setw(13) << "$" << menuList[1].menuPrice << endl;
}
if (orderCount[2] > 0)
{
cout << "\t" << orderCount[2] << setw(7) << menuList[2].menuItem << setw(18) << "$" << menuList[2].menuPrice << endl;
}
if (orderCount[3] > 0)
{
cout << "\t" << orderCount[3] << setw(13) << menuList[3].menuItem << setw(12) << "$" << menuList[3].menuPrice << endl;
}
if (orderCount[4] > 0)
{
cout << "\t" << orderCount[4] << setw(13) << menuList[4].menuItem << setw(12) << "$" << menuList[4].menuPrice << endl;
}
if (orderCount[5] > 0)
{
cout << "\t" << orderCount[5] << setw(7) << menuList[5].menuItem << setw(18)<< "$" << menuList[5].menuPrice << endl;
}
if (orderCount[6] > 0)
{
cout << "\t" << orderCount[6] << setw(7) << menuList[6].menuItem << setw(18) << "$" << menuList[6].menuPrice << endl;
}
if (orderCount[7] > 0)
{
cout << "\t" << orderCount[7] << setw(4) << menuList[7].menuItem << setw(21) << "$" << menuList[7].menuPrice << endl;
}
}
cout << "\t" << "Tax " << setw(22) << "$" << orderTax << endl;
cout << "\t" << "Amount Due: " << setw(14) << "$" << orderTotal << endl;
}
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')
;
}
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.