cout<<"========================================="<<endl<<endl;
cout<<"The amount you need to pay is RM "<<total<<endl;
cout<<"=========================================="<<endl<<endl;
cout<<"You can pay using ($0.10 [1] $0.20 [2] $0.50 [3] $1 [4] $5 [5] $10 [6] $20 [7] $50 [8] )"<<endl;
cin>>choice2;
switch(choice2){
case 1:
total = total - 0.10;
break;
case 2:
total = total - 0.20;
break;
case 3:
total = total - 0.50;
break;
case 4:
total = total - 1;
break;
case 5:
total = total - 5;
break;
case 6:
total = total - 10;
break;
case 7:
total = total - 20;
break;
case 8:
total = total - 50;
break;
default:
cout<<"inavalid!"<<endl;
}
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
cin>>choice2;
}
Extra information: My total is $5
I am trying to let it loop until the total amount is paid, say I choose case 4 which is $1. It's suppose to let me insert the remaining amount I am supposed to pay, which is $4, but the program ends after I insert another switch case option.
This part, isn't it supposed to be looping until my total is 0?
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
cin>>choice2;
}
Thanks in advance for any help, I am also happy to learn any shorter way of writing this program if there is any, also is there anyway I can implement array into this program?
No, neither the switch nor the if will cause your program to loop.
You're probably looking for something along the lines of
while(total > 0)
{
cin>>choice2;
switch(choice2){
// left out for clarity
}
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
//Instead of getting the input in 2 different locations, just get it again at the start of the next loop.
}
}
Related
so basically im trying to do a small program that let the user enter a value then if the value equal or greater than x make discount something like 12%
here is an example
15% discount, if sales are greater than or equal to 1000
10% discount, if sales are greater than or equal to 500
5% discount, if sales are greater than or equal to 250
0, otherwise.
i know how to do it using if statement, but in switch i have no idea, there could be million cases about the entered value, so i need help if its possible
Thanks
One way would be
int sales; // ToDo - needs a value
double discount;
switch (std::max(sales / 250, 4)){
case 4:
discount = 0.15;
break;
case 3: case 2:
discount = 0.10;
break;
case 1:
discount = 0.05;
break;
default:
discount = 0.0;
}
but just because it's possible doesn't mean it's the right thing to do. It most certainly is not. My solution would be a pain in the neck to tweak if the thresholds change - that is poor program design indeed since you don't really want to have to change the program control flow if parameters change.
Use an if block, or some kind of data structure.
I have written a program in c++ to convert number into words. The output for 0-19 is fine but it is giving wrong output for numbers between 20-99.
//This program converts numbers from 0-99 into words
#include<iostream>
using namespace std;
int main()
{
int number,unit,ten;
cout<<"Please enter any number between 0-99: ";
cin >>number;
ten=number/10;
unit=number%10;
if(number<0 | number>99)
cout<<"Number is out of range";
if(number>=11 & number <=19)
{
if(number==11) cout<<"eleven";
if(number==12) cout<<"twelve";
if(number==13) cout<<"thirteen";
if(number==14) cout<<"fourteen";
if(number==15) cout<<"fifteen";
if(number==16) cout<<"sixteen";
if(number==17) cout<<"seventeen";
if(number==18) cout<<"eighteen";
if(number==19) cout<<"ninteen";
}
else
{
if(unit==0) cout<<"zero";
if(unit==1) cout<<"one";
if(unit==2) cout<<"two";
if(unit==3) cout<<"three";
if(unit==4) cout<<"four";
if(unit==5) cout<<"five";
if(unit==6) cout<<"six";
if(unit==7) cout<<"seven";
if(unit==8) cout<<"eight";
if(unit==9) cout<<"nine";
if(ten==10) cout<<"Ten";
if(ten==20) cout<<"twenty";
if(ten==30) cout<<"thirty";
if(ten==40) cout<<"fourty";
if(ten==50) cout<<"fifty";
if(ten==60) cout<<"sixty";
if(ten==70) cout<<"seventy";
if(ten==80) cout<<"eighty";
if(ten==90) cout<<"ninty";
}
}
Output:
Please enter any number between 0-99: 25
five
Actually, your program is working correctly. For input 25 it should give five because you are doing unit = number%10 which returns 5 and if(unit==5) cout<<"five"; right from your own code.
If you want to get twenty five in return you should change the code.
Instead of making direct matching, store numbers in map and check the value of it. and another approch is to store them in 2 different lists like: d1 = ['zero','one' ...] and d10 = ['ten', 'twenty', 'thirty', ...]. By this, you will not repeat anything. Furthermore, you can check the length of the input and know if it one decimal number or two and if 2 decimal number you can get the first one with number/10 and the second one with number%10 and concatenate the result.
Let' take 25 you will search 2 in the d10 list and 5 in the d1 list and at the end, you end up 'twenty' + 'five'
n1 = number/10
n2 = number%10
for i in d10:
if i == n1:
for j in d1:
if j == n2:
print(i+' ' +j)
else:
break
else:
break
NOTE: the code is in python 3.x
There are only two places where you need to just do minor changes and you will get you desired result
first when you are separing the ten part, multiply it with 10 aftwards to checking for multiple of 10 in your if conditions of multiple of 10 i.e. 10 , 20 ,..
and then you need to just change the position of all the ten position comparison to above all other one's digit number comparisons
I copied your code and did changes,
I have commented below where you need to change
//This program converts numbers from 0-99 into words
#include<iostream>
using namespace std;
int main()
{
int number,unit,ten;
cout<<"Please enter any number between 0-99: ";
cin >>number;
ten=number/10;
unit=number%10;
ten=ten*10;// multiply again it make it power of 10 so that you can check for if for multiple of 10
if(number<0 | number>99)
cout<<"Number is out of range";
if(number>=11 & number <=19)
{
if(number==11) cout<<"eleven";
if(number==12) cout<<"twelve";
if(number==13) cout<<"thirteen";
if(number==14) cout<<"fourteen";
if(number==15) cout<<"fifteen";
if(number==16) cout<<"sixteen";
if(number==17) cout<<"seventeen";
if(number==18) cout<<"eighteen";
if(number==19) cout<<"ninteen";
}
else
{
if(ten==10) cout<<"Ten";
if(ten==20) cout<<"twenty";
if(ten==30) cout<<"thirty";
if(ten==40) cout<<"fourty";
if(ten==50) cout<<"fifty";
if(ten==60) cout<<"sixty";
if(ten==70) cout<<"seventy";
if(ten==80) cout<<"eighty";
if(ten==90) cout<<"ninty";
//all ten position comparisons has been shifted before all one's position comparisons
if(unit==0) cout<<"zero";
if(unit==1) cout<<"one";
if(unit==2) cout<<"two";
if(unit==3) cout<<"three";
if(unit==4) cout<<"four";
if(unit==5) cout<<"five";
if(unit==6) cout<<"six";
if(unit==7) cout<<"seven";
if(unit==8) cout<<"eight";
if(unit==9) cout<<"nine"; }
}
What must I do to get the right Level?
example:
int gXP = globalDoHandle::PlayerStats_XP(p);
ostringstream sXP; sXP << "XP(RP): " << gXP;
ostringstream sLEVEL; sLEVEL << "Level: " << gLEVEL;
I want use the XP value to get the right Level then.
If I get the the value 24450 should give me it then "10" back
I know I can use something like this, but that are 8000 Level in the Game!
if (gXP < 800) { Rank = "1"; }
else if (gXP < 2100) { Rank = "2"; }
else if (gXP < 3800) { Rank = "3"; }
...
LEVEL: XP
Level 1: 0
Level 2: 800
Level 3: 2100
Level 4: 3800
Level 5: 6100
Level 6: 9500
Level 7: 12500
Level 8: 16000
Level 9: 19800
Level 10: 24000
Level 11: 28500
Level 12: 33400
Level 13: 38700
Level 14: 44200
Level 15: 50200
Level 16: 56400
Level 17: 63000
Level 18: 69900
Level 19: 77100
Level 20: 84700
...Level 8000: 1787576850
To do such a job, you need std::lower_bound. std::lower_bound(l, h, v) returns an iterator it inside the range [l, h) for which the value on the range is the smallest verifying *it > v.
constexpr std::array<unsigned, 10> levels = { /* ... */ }; // xp needed for each level
unsigned level(unsigned xp)
{
auto it = std::lower_bound(cbegin(levels), cend(levels), xp);
return std::distance(begin(levels), it);
}
level(xp) returns the level reached with xp experience points with respect to the values of levels.
Se a full demo online
Use a std::array with your exp values in.
std::array<int, 8000> exp_table = { 0, 800, 2100... };
int level=0;
for(; level < exp_table.size() && exp_table[level] < gXP; ++level);
Rank = std::to_string(level); // If this is needed?
After this level will be set correctly. The only thing left is how you generate your exp_table. Read it from a file. Use some math formula.
Heys.
I have this code, which sets a table for some mystery reason. Size is 6x60. Which means SIZEY defined as 6, and SIZEX as 60.
void set_table(char** table)
{
int i,j,k=0;
for(i=0;i<SIZEY;i+=3){
for(j=0;j<SIZEX;j++){
switch(k++%5){
case 0:
table[i][j]='|';
break;
case 1:
table[i][j]=' ';
break;
case 2:
table[i][j]=(char)((((k-2)/50)%10)+48);
break;
case 3:
table[i][j]=(char)((((k-3)/5)%10)+48);
break;
case 4:
table[i][j]=' ';
break;
default:
continue;
}
}
}
}
I am doing this with 3 variables, as you can see. Question is, can i do that with 2 variables, or even with only 1 ?
Thanks in advance.
Here's a simplification for you:
switch(k++%5){
case 0:
table[i][j]='|';
break;
case 1:
case 4:
table[i][j]=' ';
break;
case 2:
case 3:
table[i][j]= '0';
break;
default:
continue;
}
With C++, one case can fall into another, such as with cases 1 and 2 above.
Your expressions for case 2 and 3 can be simplified. I'll use case 2 as an example:
((((k-2)/50)%10)+48)
Substituting 2 for k yields
((((2-2)/50)%10)+48)
Simplify:
((((0)/50)%10)+48)
Zero divided by anything is zero, simplifying again:
(((0)%10)+48)
Zero mod anything is zero, since it involves division:
((0)+48)
Simplifying:
(48)
Replacing with the equivalent character (since your array is char):
'0'
This question already has answers here:
How can I convert seconds since the epoch to hours/minutes/seconds in Java?
(6 answers)
Closed 9 years ago.
Im required as part of a lab to devise a way to calculate the current month day and year. I'll be using the gettimeofday() function which gives me the number of seconds since january 1 1970.
I know that there are functions that will do the conversoins for me, however the design requirement is that i create my own algorithm for converting the seconds to months days and years. The manner in which I want to implement my design is with a lookup table for each of the twelve months and the corresponding number of days. The logic is a little befuddling to me right now.
The tricky part is handling the leap years. I know that 1972 is the first leap year since 1970. And a leap year occurs every 4 years since that date. The hint given to me in this assignment is that the next largest cycle after days is 4 years. So if I modulus the number of days since 1970 by 1461 (number of days in 4 years) I know I can get the number of days left over. Its at this point my train of logic gets lost. If I DIVIDE it by 1461 it just tells me the how many 4 year periods have gone by.
The table I want to implement will look something like this ( i know the coding isnt completely right but just to show what im getting at):
struct Monthdays
{
int days;
char* Monthname[]
};
Monthdays lookupMonths[]
{
{31,"January"}
{28,"February"}
.
.
.
};
Im trying to figure out how to create a proper index using the number of days or something to walk through this "table".........I hope asking this here is okay. I've been struggling with the logic or a couple of days right now....
Here is the code for this problem i have now which is very inefficient.
ExpandedTime* localTime(
struct timeval* tv, // Pointer to timeval struct
ExpandedTime* etime // '' '' to expandedtime strct
)
{
tzset(); // Corrects for timezone
int epochT = (tv->tv_sec) - timezone; // Epoch seconds with
int epochUT = tv->tv_usec; // epochtime microseconds
int edays; // Days since epochtime
etime->et_usec = (epochUT/milli) % milli; // Find the milliseconds
etime->et_sec = epochT % 60;
epochT /= 60; // Turn into minutes
etime->et_min = epochT % 60;
epochT /= 60; // Turn into hours
if (localtime(&tv->tv_sec)->tm_isdst !=0)
etime->et_hour = (epochT % 24) + daylight; // Hours with DST correc
else
etime->et_hour = (epochT % 24);
edays = epochT /= 24; // Turn into days
etime->et_day = epochT; // Delete up to here
etime->et_year = (epochT/365) + epochyear; // Get the current year
int trackyear; // Counter for years
int trackdays = -1; // Subtracting janurary 1st
// from days
// This will determine if it is a leapyear and adjust days accordingly
// from 1970 to current year (2013)
for (trackyear = epochyear; trackyear < etime->et_year; trackyear++)
{
if (trackyear % leapy == 0)
{
trackdays = trackdays + 366;
}
else
{
trackdays = trackdays + 365;
}
}
etime->et_day = edays - trackdays;
int trackmonth = -1; // Counter for months
// with offset to make
// january = 0
// This will give me the number of months for the buffer
do
{
switch (trackmonth)
{
// Months with 31 days
case 0:
etime->et_day = (etime->et_day) - 31;
break;
case 2:
etime->et_day = (etime->et_day) - 31;
break;
case 4:
etime->et_day = (etime->et_day) - 31;
break;
case 6:
etime->et_day = (etime->et_day) - 31;
break;
case 7:
etime->et_day = (etime->et_day) - 31;
break;
case 9:
etime->et_day = (etime->et_day) - 31;
break;
case 11:
etime->et_day = (etime->et_day) - 31;
break;
// Months with only 30 days
case 3:
etime->et_day = (etime->et_day) - 30;
break;
case 5:
etime->et_day = (etime->et_day) - 30;
break;
case 8:
etime->et_day = (etime->et_day) - 30;
break;
case 10:
etime->et_day = (etime->et_day) - 30;
break;
// Leap year month a.k.a Febuary
case 1:
if (trackyear % leapy)
{
etime->et_day = (etime->et_day) - 28;
}
else
{
etime->et_day = (etime->et_day) - 29;
}
break;
}
trackmonth++;
}
while(etime->et_day > 0);
etime->et_mon = trackmonth - 1;
// Reverts day offset from previous switch to
// accurately represent the current day
switch (etime->et_mon)
{
// Months with 31 days
case 0:
etime->et_day = (etime->et_day) + 31;
break;
case 2:
etime->et_day = (etime->et_day) + 31;
break;
case 4:
etime->et_day = (etime->et_day) + 31;
break;
case 6:
etime->et_day = (etime->et_day) + 31;
break;
case 7:
etime->et_day = (etime->et_day) + 31;
break;
case 9:
etime->et_day = (etime->et_day) + 31;
break;
case 11:
etime->et_day = (etime->et_day) + 31;
break;
// Months with only 30 days
case 3:
etime->et_day = (etime->et_day) + 30;
break;
case 5:
etime->et_day = (etime->et_day) + 30;
break;
case 8:
etime->et_day = (etime->et_day) + 30;
break;
case 10:
etime->et_day = (etime->et_day) + 30;
break;
// Leap year month a.k.a Febuary
case 1:
if (trackyear % leapy)
{
etime->et_day = (etime->et_day) + 28;
}
else
{
etime->et_day = (etime->et_day) + 29;
}
break;
}
return etime;
}
Do a little web surfing for information on how to calculate a Julian Date (or more precisely a Julian Day Number) ... that will solve your problem or get you well on your way.
Other than that, it would be unethical to do someone's homework for them... though... I do have a PayPal account ~lol~
if(year % 4 == 0 && year % 100 == 0 && year % 25 != 0)
this is a leap year.
This was actually the hardest kind of homework i had on college... all in all, depending on how accurate this needs to be, i'll tell you to [not] look into timezones and when different zones enter daylight savings...
Sorry if the answer isn't terribly helpful, but this is the formula for leap years. If you keep going the route you're headed, you'll need to bring in the Chinese theorem into this...
But if you can do gettimeofday() it returns the number of milliseconds since 1st January 1970. So you can just put a little for in it that will 'waste' the seconds and simulate passage of time up until now, and run that loop until you run out of time. When you stop you should be able to see at which date you stopped ;)