hello, what is my mistake in writing this code? its not displaying the last 3 bullet points properly - percentage

Some insurance companies offer discounts on car insurance premiums depending on the number of years the driver has had a driver's licence and the number of claims the driver has made in the last five years.
In this program, the user inputs (as integers)
•years the number of years the driver has had a driver's license
•claims the number of insurance claims made in the last five years
The (integer) percentage of the "standard premium"that will be charged is initially calculated as follows:
•if years< 5 then percentage = 100 –10 * years + 20 * claims
•otherwise ("else") percentage = 50 + 20 * claims
The percentage calculated in this way is then further adjusted as follows:
•if percentage> 150 then insurance is refused
•otherwise,if the percentage is between 100% and 150% then set percentage = 100
•(otherwise,the percentage isn't adjusted)
cout << "enter years licenced: " << endl;
cin >> years;
cout << "enter number of claims: " << endl;
cin >> claims;
if (years < 5)
{
percentage = 100 - (10 * years) + (20 * claims);
cout << "percentage from 0 to 5:" << percentage << endl;
}
else (years > 5);
{
percentage = 50 + (20 * claims);
cout << "percentage higher than 5:" << percentage << endl;
}
if (percentage > 150)
{
cout << "insurance is refused." << percentage << endl;
}
else if (100 <= percentage <= 150)
{
cout << "percentage = 100." << endl;
}
else;
{
cout << "insurance is refused." << endl;
}
return 0;

Replace
else if (100 <= percentage <= 150)
with
else if (100 <= percentage && percentage <= 150)
In C++, chaining of comparison operators works differently than in math. To get the math meaning, don't use chaining, and connect individual comparisons with logical and (&&).
Replace
else (years > 5);
with
else if (years > 5)
The ; is equivalent to {}, thus it makes the else branch empty, and the code below (the multiline {...}) would be executed unconditionally.
Replace
else;
with
else

Related

How to set minimum and maximum values for an integer?

How do i add minimum and maximum values for an integer? I want an integer to never go down below zero like negative and never goes above 100
Here is the example:
int hp = 100;
std::cout << "You cast healing magic to yourself!" << std::endl;
hp += 20;
mp -= 25;
For example the health is 100 but when a healing magic is cast it became 120. The thing i want is i want it to stay as 100 no matter how many healing magic are cast upon.
You can use std::clamp:
hp = std::clamp(hp + 20, 0, 100);
mp = std::clamp(mp - 25, 0, 100);
You can use std::clamp as suggested by #TedLyngmo if you are using a compiler which supports C++ 17. If not, then you can write a simple function to manage the limits for hp and mp:
void change(int& orig, int val)
{
int temp = orig + val;
if (temp <= 0)
temp = 0;
else if (temp >= 100)
temp = 100;
orig = temp;
}
int main()
{
int hp = 40, mp = 40;
std::cout << "You cast healing magic to yourself!" << std::endl;
change(hp, 50);
change(mp, -25);
std::cout << hp << " " << mp << std::endl;
}
I believe what you are saying is whatever the healing magic is you want to display 100 or your hp the way it is. If so you can store the 20 and 25 as variables and create another var with the same value as ur original one and play around with that. Don't change the value of ur original one and so you get to display that.

trouble with output of recursive function

i'm having trouble getting the correct output of the function. The function output should show the expression. For example, if the input is "1234", then the output should be 1 + 2 + 3 + 4 = 10.
i can get the function to output the first part of the expression, but i'm not sure how to get it to output the sum as well.
heres what i have so far:
void sumDigits(int num, int &sum){
sum += num % 10;
if(num < 10)
cout << num;
else {
sumDigits(num/10, sum);
cout << " + " << num % 10;
}
}
why don't you do it in caller?
you could pass the remaining digits number to process to a function and output sum when it's 0, but I doing it in the caller is better..
<< can be chained. Try
cout << " + " << num % 10 << " = " << sum << endl;

I have created a dice betting game. I cannot get the loop to run correctly could someone help me with finding the issue?

Okay so I need help with getting the bank to NOT reset to 100 after each time the loop runs. I have tried many ways but can't seem to get it to work. Could you please help me with a few explanations and examples?
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int displaystats(int gamesplayed, int wins, int losses, int bank);
int main()
{
int bank = 100;//intital bank value
int bet = 0;//desired wager
int wins = 0;//games won
int losses = 0;//games lost
int gamesplayed = 0;//how many rounds you played
int compdice1 = 0;//first rolled dice for computer
int compdice2 = 0;//second rolled dice for computer
int playdice1 = 0;//first rolled dice for player
int playdice2 = 0;//seconds rolled dice for player
int newdice = 0;//the dice to risk your wager
int comproll = 0;//the sum of the computers roll
int playroll = 0;//the sum of the players roll
do
{
if (bank < 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "I am sorry you are out of money." << endl;
displaystats(gamesplayed, wins, losses, bank);
break;
}
else if (bank > 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
compdice1 = (rand() + time(0)) % 6 + 1;//computer dice
compdice2 = (rand() + time(0)) % 6 + 1;//computer second dice
playdice1 = (rand() + time(0)) % 6 + 1;//player dice
playdice2 = (rand() + time(0)) % 6 + 1;//player second dice
comproll = compdice1 + compdice2;//computer sum
playroll = playdice1 + playdice2;//player sume
cout << "Your roll was " << playdice1 << " and " << playdice2 << " with a sume of " << playroll << endl;
if (playroll < comproll)
{
char option;//option to roll another dice
cout << "You win!" << endl;
cout << "Would you like to roll a third dice to earn 1.5 times your bet, yes or no? ";
cin >> option;
if (option == 'yes')
{
int newroll;//the new sum of the three dice
int newdice;//the extra roll
newdice = (rand() + time(0)) % 6 + 1;
newroll = playroll + newdice;//the value of players roll
if (newroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You now rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (newroll < comproll)
{
cout << "You win!" << endl;
cout << "Your bank now equals " << bank + (1.5 * bet) << endl;
wins++;
gamesplayed++;
}
}
else if (option == 'no')
{
cout << "Your bank now equals " << bank + bet << endl;
wins++;
gamesplayed++;
}
}
else if (playroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (playroll = comproll)
{
cout << "The computer also rolled " << comproll << endl;
cout << "I am sorry you now lose double your bet!" << endl;
cout << "Your bank now equals " << bank - (2 * bet) << endl;
losses++;
gamesplayed++;
}
}
} while (bank > 0);
int stats = displaystats(gamesplayed, wins, losses, bank);
cout << "Your stats are " << stats << endl;
return 0;
}
int displaystats(int gamesplayed, int wins, int losses, int bank)
{
cout << "Games Played: " << gamesplayed << endl;
cout << "Wins: " << wins << endl;
cout << "Losses: " << losses << endl;
cout << "Bank Total: " << bank << endl;
return (gamesplayed, wins, losses, bank);
}
Your problem with the bank "resetting" constantly was you never actually subtracted the bet from the bank. See the following code, I hope this helps.
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
//This is the line that you forgot.
bank = bank - bet
If you then win, then you may want to later on add some money back into the bank. (But this is up to you.) I hope this helps.
EDIT:
Here is a paste-bin for the full code as requested: http://pastebin.com/HzvRxjXL
Also, if this solves your problem, I would appreciate it if you would mark it as the answer so others don't spend time answering a problem that has been solved.
EDIT 2:
This has a (As far as I can tell) fixed and commentated version of the code. I hope this helps: http://pastebin.com/miQjy4B5

Types using XTEA encryption funct

I'm very interested in cryptography, and since I like programming too, I decided to make a little program to encrypt files using XTEA encryption algorithm.
I got inspired from Wikipedia, and so I wrote this function to do the encryption (To save space, I won't post the deciphering function, as it is almost the same):
void encipher(long *v, long *k)
{
long v0 = v[0], v1 = v[1];
long sum = 0;
long delta = 0x9e3779b9;
short rounds = 32;
for(uint32 i = 0; i<rounds; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
}
v[0] = v1;
v[1] = v1;
}
Now when I want to use it, I wrote this code:
long data[2]; // v0 and v1, 64bits
data[0] = 1;
data[1] = 1;
long key[4]; // 4 * 4 bytes = 16bytes = 128bits
*key = 123; // sets the key
cout << "READ: \t\t" << data[0] << endl << "\t\t" << data[1] << endl;
encipher(data, key);
cout << "ENCIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
decipher(data, key);
cout << "DECIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
I always get either run-time crash or wrong decipher text:
I do understand the basics of the program, but I don't really know what is wrong with my code. Why is the enciphered data[0] and data1 the same? And why is deciphered data completely different from the starting data? Am I using the types wrong?
I hope you can help me solving my problem :) .
Jan
The problem is here:
v[0] = v1; // should be v[0] = v0
v[1] = v1;
Also, you only set the first 4 bytes of the key. The remaining 12 bytes are uninitialized.
Try something like this:
key[0] = 0x12345678;
key[1] = 0x90ABCDEF;
key[2] = 0xFEDCBA09;
key[3] = 0x87654321;
The fixed code gives me this output:
READ: 1
1
ENCIPHERED: -303182565
-1255815002
DECIPHERED: 1
1

Run-time error - Vector iterator not dereferencable?

For class, Im making a program that manages a hotel. Im getting a run-time error when my program gets to this function:Vector iterator not dereferencable. I used the debugger to find the problem area, but I cant figure out what is wrong with it. Any Suggestions?
Customer & ListOfCustomers::getByID(int id)
{
if(!sortedByID)sortByID();
vector<Customer>::iterator iter;
Customer cus;
cus.customerID=id;
iter = lower_bound(customers.begin(),customers.end(),cus,compareCustomersByID);
if( (*iter).customerID == id ) // <---DEBUGGER SAYS ERROR HERE IN THIS LINE
{
return *iter;
}
else
{
return NullCustomer();
}
}
Here is the lower_bound function. It is Inside #include algorithm
template<class _FwdIt,
class _Ty,
class _Pr> inline
_FwdIt lower_bound(_FwdIt _First, _FwdIt _Last,
const _Ty& _Val, _Pr _Pred)
{// find first element not before _Val, using _Pred
// _DEBUG_ORDER_PRED(_First, _Last, _Pred);
return (_Rechecked(_First,
_Lower_bound(_Unchecked(_First), _Unchecked(_Last), _Val, _Pred,
_Dist_type(_First))));
}
EDIT: added a space so that the lower_bound function would be formatted correctly as code.
You are using the lower_bound function for searching. Its purpose is a little different than that. This is what lower_bound does:
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value.
And another definition from here:
Specifically, it returns the first position where value could be inserted without violating the ordering.
So for example, if the thing you are looking for is not in the vector, it will return an iterator that points after the last item in the vector, and that iterator can't be dereferenced because it does not exist.
Take a look at this example:
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector<int>::iterator low;
sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
low=lower_bound (v.begin(), v.end(), 60); // ^it will point here
cout << "lower_bound at position " << int(low- v.begin()) << endl;
As you can see from the output, the iterator will point to the 9th element in the vector (index 8). But the vector only has 8 elements (indexed 0-7). The explanation for this is that you can insert the new item in the vector at index 8 without violating the ordering.
I think that what you really want is the find function. Here is an example:
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector<int>::iterator find_it1 = find(v.begin(), v.end(), 30);
vector<int>::iterator find_it2 = find(v.begin(), v.end(), 80);
if(find_it1 == v.end())
cout << "30 not found" << endl;
else
cout << "30 found at position " << int(find_it1 - v.begin()) << endl;
if(find_it2 == v.end())
cout << "80 not found" << endl;
else
cout << "80 found at position " << int(find_it2 - v.begin()) << endl;
Here is the output:
30 found at position 2
80 not found

Resources