I have some problem with my homework. So this is how it looks like
#include<stdio.h>
int main()
{
char code;
int price,discount;
float total;
printf("Please input price: ");
scanf("%d",&price);
printf("Please input discount code: ");
scanf(" %c",&code);
switch(code)
{
case 'a': printf("Your discount code is 25 percent\n");
discount = 25;
break;
case 'b': printf("Your discount code is 15 percent\n");
discount = 15;
break;
case 'c': printf("Your discount code is 5 percent\n");
discount = 5;
break;
default: printf("Wrong code,Your discount is 0 percent\n");
discount = 0;
break;
}
total = (price*((100-discount)/100));
printf("Your price is = %.2f\n",total);
}
I have 2 questions to ask
My task is I have to input both of uppercase and lowercase letter for discount code (there are only three codes: a, b, c) so how can I put both of them in case command? (in this I only do the lowercase letter)
I have run this. But it seems like the discount value is 0 when I try to used it for calculate in the end. When I print the discount only, it works normally. How can I fix that?
Sorry for my poor English and Thank you for your help!
There would be different possibilities.
scanf(" %c",&code);
switch(code)
{
case 'a':
case 'A':
printf("Your discount code is 25 percent\n");
discount = 25;
break;
or you change the input to lower case before or in the switch!
switch( code | 0x60 ) // make it to lower case
with this you don't have to change the following code.
Related
void menu() {
char mode = ' ';
cout << "Gebe einen Modus an. 1 Addition, 2 Subtraktion, 3 Multiplikation, 4 Division: ";
cin >> mode;
switch (mode) {
case '1':
addition();
break;
case '2':
subtraktion();
break;
case '3':
multiplikation();
break;
case '4':
division();
break;
default:
cout << "Ungueltige Eingabe, versuch es nochmal\n";
menu();
break;
}
}
Hey! I have the problem, that If I input more than one character, which are not valid, the default statement will be executed just as often as the length of my input. But shouldnt It be like that:
If I input to a char more than one letter, everything after the letter will be cut off, since a char can only save one character. So why does it get executed multiple times? Could anyone explain this, detailed? Thanks in Advance!
I'm trying to make a loop statement to determine the prices according to their respective packages but I can't seem to get a grasp of the do and while statements.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
double totaladult,totalchild,totalsenior, total;
int noadult,nochild,nosenior;
char option;
//char choice = 'y'; looping choice
cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nChoose your package A/B/C: "<<endl;
cin>>option;
cout<<"Enter the no of adult: "<<endl;
cin>>noadult;
cout<<"Enter the no of child: "<<endl;
cin>>nochild;
cout<<"Enter the no of senior citizen: "<<endl;
cin>>nosenior;
switch(option)
{
case 'A':
case 'a':
totaladult=W_adult*noadult;
totalchild=W_child*nochild;
totalsenior=W_senior*nosenior;
break;
case 'B':
case 'b':
totaladult=A_adult*noadult;
totalchild=A_child*nochild;
totalsenior=A_senior*nosenior;
break;
case 'C':
case 'c':
totaladult=P_adult*noadult;
totalchild=P_child*nochild;
totalsenior=P_senior*nosenior;
break;
}
cout<<"Total no of adult is: "<<noadult<<endl;
cout<<"Total no of child is: "<<nochild<<endl;
cout<<"Total no of senior citizen is: "<<nosenior<<endl;
cout<<"Total price of adult is: "<<totaladult<<endl;
cout<<"Total price of child is: "<<totalchild<<endl;
cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;
return 0;
}
Is there something else I'm missing here? Currently it only calculates the price of one category. I would like know where should I include the loop statements.
you should add an option to quit, and while loop as long as the user keep selecting packages:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
double totaladult = 0,totalchild = 0,totalsenior = 0, total = 0;
int noadult = 0,nochild = 0,nosenior = 0;
char option;
bool run = true;
//char choice = 'y'; looping choice
cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
cout<<setw(80)<<setfill('*')<<"*";
while(run)
{
cout<<"\nChoose your package A/B/C --- or Q to quit: "<<endl;
cin>>option;
cout<<"Enter the no of adult: "<<endl;
cin>>noadult;
cout<<"Enter the no of child: "<<endl;
cin>>nochild;
cout<<"Enter the no of senior citizen: "<<endl;
cin>>nosenior;
switch(option)
{
case 'A':
case 'a':
totaladult+=W_adult*noadult;
totalchild+=W_child*nochild;
totalsenior+=W_senior*nosenior;
break;
case 'B':
case 'b':
totaladult+=A_adult*noadult;
totalchild+=A_child*nochild;
totalsenior+=A_senior*nosenior;
break;
case 'C':
case 'c':
totaladult+=P_adult*noadult;
totalchild+=P_child*nochild;
totalsenior+=P_senior*nosenior;
break;
case 'q':
case 'Q':
run = false;
break;
default:
// do nothing
break;
break;
}
cout<<"Total no of adult is: "<<noadult<<endl;
cout<<"Total no of child is: "<<nochild<<endl;
cout<<"Total no of senior citizen is: "<<nosenior<<endl;
cout<<"Total price of adult is: "<<totaladult<<endl;
cout<<"Total price of child is: "<<totalchild<<endl;
cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;
}
return 0;
}
I have a school project in which I have to create a program which makes use of C++ file handling and classes.
But when I am storing data for month of April and reading it back again in first program run, it shows the data.
But in second program run, when I enter data for the month of may and try to read data for month of April it does not show the values I entered in first program run.
Here is my code:
#include <fstream>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
class money
{
long double gpay=0.0,bpay=0.0,savings=0.0,groceries=0.0,pf=0.0,npay=0.0,hrent=0.0,
ins=0.0,edu=0.0,misc=0.0,texp=0.0,med=0.0;
public:
char name_month[15];
void getdata()
{
cout<<endl<<endl<<"Enter your basic pay : ";
cin>>bpay;
cout<<"Enter your gross pay : ";
cin>>gpay;
cout<<"Enter your net pay : ";
cin>>npay;
cout<<endl<<"Enter house rent (inclusive of water and electricity) : ";
cin>>hrent;
cout<<endl<<"Enter expenditure on groceries : ";
cin>>groceries;
cout<<endl<<"Enter insurance premium amt (if any) (monthly) : ";
cin>>ins;
cout<<endl<<"Enter monthly expense on education (if any) : ";
cin>>edu;
cout<<endl<<"Enter medical expense (if any) : ";
cin>>med;
cout<<endl<<"Enter any miscellaneous expenditure (bills , transport, etc.) : ";
cin>>misc;
pf=0.12*bpay;
texp=hrent+ins+groceries+edu+med+misc;
savings=pf+(npay-texp);
}
void putdata()
{
cout<<endl<<endl<<"Your basic pay : "<<bpay<<endl;
cout<<"Your gross pay : "<<gpay<<endl;
cout<<"Your net pay : "<<npay<<endl;
cout<<endl<<"House rent (inclusive of water and electricity) : "<<hrent<<endl;
cout<<endl<<"Expenditure on groceries : "<<groceries<<endl;
cout<<endl<<"Insurance premium amt (monthly) : "<<ins<<endl;
cout<<endl<<"Monthly expense on education : "<<edu<<endl;
cout<<endl<<"Medical expense : "<<med<<endl;
cout<<endl<<"Miscellaneous expenditure (bills , transport, etc.) : "<<misc<<endl<<endl;
cout<<"Your expenditure : "<<texp<<endl;
cout<<"Your savings from this month : "<<savings<<endl;
}
float getexp()
{
return texp;
}
float getsaving()
{
return savings;
}
}u[12];
int main()
{
system("color f0");
int s = sizeof(money),cho=0,x,ans,a,b;
char choice;
cout<<endl<<"Enter the number for month whose record you want to enter =>"<<endl<<endl<<"1. April"<<endl
<<"2. May"<<endl<<"3. June"<<endl<<"4. July"<<endl<<"5. August"<<endl<<"6. September"<<endl<<"7. October"<<endl
<<"8. November"<<endl<<"9. December"<<endl<<"10. January"<<endl<<"11. February"<<endl<<"12. March"<<endl<<endl;
cin>>x;
cout<<endl;
switch (x)
{
case 1 :cout<<"Entering data for month of April";
break;
case 2 :cout<<"Entering data for month of May";
break;
case 3 :cout<<"Entering data for month of June";
break;
case 4 :cout<<"Entering data for month of July";
break;
case 5 :cout<<"Entering data for month of August";
break;
case 6 :cout<<"Entering data for month of September";
break;
case 7 :cout<<"Entering data for month of October";
break;
case 8 :cout<<"Entering data for month of November";
break;
case 9 :cout<<"Entering data for month of December";
break;
case 10 :cout<<"Entering data for month of January";
break;
case 11 :cout<<"Entering data for month of February";
break;
case 12 :cout<<"Entering data for month of March";
break;
}
u[x-1].getdata();
ofstream file;
file.open("calc.dat",ios::out|ios::binary|ios::ate);
int q=x-1;
file.seekp(160*q);
long pos=file.tellp();
cout<<"before write"<<pos;getch();
file.write((char*)& u[x-1], sizeof(u[x-1]));
pos=file.tellp();
cout<<" after write"<<pos;getch();
file.close();
cout<<endl<<"Your total expenditure is : "<<u[x-1].getexp();
cout<<endl<<endl<<"Your savings are : "<<u[x-1].getsaving()<<" (Total savings include pf amount also)";
cout<<endl<<endl<<"Do you want to see records of any month ? (y/n)"<<endl;
cin>>choice;
do
{
if(choice=='y')
{
cout<<endl<<"Enter the number for month whose record you want to see =>"<<endl
<<"1. April"<<endl<<"2. May"<<endl<<"3. June"<<endl<<"4. July"<<endl<<"5. August"<<endl<<"6. September"<<endl<<
"7. October"<<endl<<"8. November"<<endl<<"9. December"<<endl<<"10. January"<<endl<<"11. February"<<endl<<"12. March"<<endl<<endl;
cin>>cho;
switch (cho)
{
case 1 :cout<<"Showing data for month of April";
break;
case 2 :cout<<"Showing data for month of May";
break;
case 3 :cout<<"Showing data for month of June";
break;
case 4 :cout<<"Showing data for month of July";
break;
case 5 :cout<<"Showing data for month of August";
break;
case 6 :cout<<"Showing data for month of September";
break;
case 7 :cout<<"Showing data for month of October";
break;
case 8 :cout<<"Showing data for month of November";
break;
case 9 :cout<<"Showing data for month of December";
break;
case 10 :cout<<"Showing data for month of January";
break;
case 11 :cout<<"Showing data for month of February";
break;
case 12 :cout<<"Showing data for month of March";
break;
}
ifstream ifile;
int l=cho-1;
cout<<l;
ifile.open("calc.dat",ios::in|ios::binary);
ifile.seekg(160*l);
long tellsop=ifile.tellg();
cout<<"before read"<<tellsop<<endl;getch();
ifile.read((char*)& u[cho-1],sizeof(u[cho-1]));
tellsop=ifile.tellg();
cout<<"after read"<<tellsop<<endl;
u[cho-1].putdata();
}
if(choice!='y')
{
break;
}
cout<<endl<<"Do you want to see more records ? (y/n)";
cin>>choice;
}while(choice=='y');
file.close();
return 0;
}
I entered data for the month of April in first run and when I tried to display it got displayed correctly.
My first run shows data for April
but on my second run when I entered data for the month of May and tried to read data for the month of April which I stored in the previous program run, the value shown were the default values i.e. 0
My second run when I entered data for May
I entered data for month of may and on checking for April it shows 0.
use ios::app mode during writing to file.
fstream f("file.bin",ios::binary|ios::app);
string msg="my sample msg";
f<<msg;
f.close();
string line;
ifstream fin("file.bin",ios::binary);
if (fin.is_open())
while ( getline (fin,line) )
cout << line << '\n';
fin.close();
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int salary;
float deduction,netpayable;
switch(salary/10000){
cout<<"enter salary amount :";
cin>>salary;
case 0:
deduction=0;
netpayable = salary;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
case 1:
deduction=1000;
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
default:
deduction=salary*(7/100);
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
}
system("pause");
}
i have employs and i want to make a simple program in which i am using switch statement to deduct the salary of different employees whom having above 10,000Rs so on... but compiler have shown no error however program is not running and giving a output as shown in image i am little confuse in it.
You have added a switch on salary without giving any value to the variable. This results in salary having a garbage value.
Just put these lines outside switch:
cout<<"enter salary amount :";
cin>>salary;
// now start the switch statement here:
switch(...)
{
....
}
This way, you are first prompting the user to enter the salary, and later doing desired operations on it.
I see 3 errors in your code. I corrected your code and wrote comments to highlight them.
Please see below:
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
// 1) Declare all variables of same type to avoid implicit casting errors.
// In this case we need float or double types.
float salary;
float deduction;
float netpayable;
// 2) This block must be out of switch instruction!
cout<<"enter salary amount :";
cin>>salary;
// 1.1) The switch will do the expected job
// only if it works on a int variable, so I put an explicit cast
// to (int).
switch((int)(salary/10000)){
case 0:
deduction=0;
netpayable = salary;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
case 1:
deduction=1000;
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
default:
// 3) (7/100) = 0 because compiler interprets it as integer.
// Use (7./100.) instead.
deduction=salary*(7./100.);
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
}
system("pause");
}
I am trying to keep a track of total amount of bought groceries.
In my program, every time I buy apples, cheese, or bread, the program should continue with displaying the menu again.
But it keeps asking "How many apples?" after the program has already calculated the total for the apples instead of going back to the menu to choose another item.
Perhaps it has something to do with the type of loop I have used.
I am stuck on trying to figure this out. Any help would be appreciated.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
double BUDGET;
const double apple= .60;
const double lb_cheese= 1.60;
const double loaf_bread = 2.50;
double total;
int count;
char choice;
double amount_left;
cout <<"Welcome! What is the budget for your picnic lunch?"<< endl;
cin>> BUDGET;
cout<<"Choose one of the following"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<" MENU \n "<<endl;
cout<<"A-Apple B-Cheese C-Bread"<<endl;
cout<<" $0.60 $1.50 $2.50 "<<endl;
cout<<"-------------------------------------"<<endl;
cin>> choice;
while ((choice != 'Q') && (total <BUDGET)) //Q is the sentinel value to "quit" the program
{
switch(choice)
{
case 'A':
case 'a':
cout<<"How many apples?";
cin>> count;
total+= (count *apple);
break;
case 'B':
case 'b':
cout<<"How many pounds of cheese ?";
cin>> count;
total+= (count* lb_cheese);
break;
case 'C':
case 'c':
cout<<"How many loafs of bread?";
cin>> count;
total+= (count * loaf_bread);
break;
default:
cout<<"The entry you have entered is not valid, please try again."<<endl;
}
if( total > BUDGET)
{ cout<<"You have exceeded your budget please check your cart.\n\n";
break;
}
cout<<"Your total is: $"<<setprecision((2))<<fixed<<total<<endl;
amount_left= BUDGET-total;
cout<<"You have $"<<setprecision(2)<<fixed<<amount_left<<" left to spend."<<endl;
}
return 0;
}
Displaying menu is out of the loop:
display menu
read option
while (option != quit) {
do some calculations
}
and the menu is therefore displayed only once. You could change it to infinite loop:
while (true) {
display menu
read option
if (choice == 'Q' || total >= BUDGET)
break;
do some calculations
}
Also try to avoid writing functions that are longer than 50 lines, place some logic into some different function and just call this function, decompose it to smaller parts, it will be much easier to read and also much easier to understand.
Yes, get the menu in a loop to display it the number of times you desire and also please remember to initialize your variables as good practice.
Double total=0.00 // initialize.