#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");
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
We are supposed to make a retail sales cashier but I just can't figure out the loops at all. We have only learned simple selection and repetition statements so far and I know that's all I need but I just can't seem to figure it out.
Project Overview
Starter Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
// Loop for repeat sales
// Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
// Get next Product ID
// Print properly formatted output for each sale
// Another sale?
// Display how much is in the cash drawer at the end
}
What I have so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subTotal = 0.0; // for receipt purposes
double salesTax = 0.0; // for receipt purposes
double totalSale = 0.0; // for receipt purposes
int anotherSale = 1;
double taxRate = 0.075; // default tax rate
// Loop for repeat sales
while ()
{
// Enter the first Product ID for the first sale (-1 to exit)
cout << "Enter the first Product ID: ";
cin >> productID;
// Main loop for each sale
while (productID > 0)
{
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
switch (productID)
{
case 101:
price = 65.00;
taxRate = 0.075;
break;
case 102:
price = 12.50;
taxRate = 0;
break;
case 103:
price = 24.50;
taxRate = 0.00;
break;
case 104:
price = 38.75;
taxRate = 0.075;
break;
case 105:
price = 17.80;
taxRate = 0.075;
break;
case 106:
price = 16.50;
taxRate = 0;
break;
case 107:
price = 42.85;
taxRate = 0.075;
break;
case 108:
price = 32.99;
taxRate = 0.075;
break;
case 109:
price = 28.75;
taxRate = 0.075;
break;
case 110:
price = 51.55;
taxRate = 0;
break;
default:
cout << "INVALID PRODUCT ID: Product ID not found." << endl;
}
cout << "Enter the quantity: ";
cin >> quantity;
subTotal += price * quantity;
salesTax += price * quantity * taxRate;
totalSale = subTotal + salesTax;
// Get next Product ID
cout << "Enter the next Product ID: ";
cin >> productID;
}
// Print properly formatted output for each sale
// Another sale?
}
// Display how much is in the cash drawer at the end
}
Any help is appreciated, thank you guys in advance.
The basic aspect of a loop statement is to repeat the same set of instructions until a specified condition is met. You have successfully figured it out for the inner loop of your code where you are checking if productID>0. You need to do the same for the outer loop and impose a similar condition on anotherSale i.e. while(anotherSale!=0). Every time the inner loop finish, just ask the user for the value of anotherSale; if the user enters 0, the loop should break.
I'm having problem with the string input, the program stops running whenever i enter a string and also if possible kindly fix my statements on the records because they don't seem to work well. Also, how do i delete a string? I tried to replace it with 'none' here for a sample. Thank you in advance!
Here is my code:
#include <iostream>
using namespace std;
int main(){
//Initializing
char choice;
int i, j, record=0, totalrecord;
int id[record];
string name[record];
double price[record];
bool back=true;
string none;
//Menu
while(back=true){
cout<<"*******************************"<<endl;
cout<<"[A] Add Record"<<endl;
cout<<"[V] View Record"<<endl;
cout<<"[E] Edit Record"<<endl;
cout<<"[D] Delete Record"<<endl;
cout<<"[L] View All Record"<<endl;
cout<<"Enter your choice and press return: "<<endl;
cin >> choice;
switch (choice){
//Add Record
case 'a':
case 'A':
record++;
cout<<"Input ID: ";
cin>>id[record];
cout<<"Input Name: ";
cin>>name[record];
cout<<"Input Price: ";
cin>>price[record];
break;
//View Record
case 'v':
case 'V':
cout<<"Enter ID you wish to view: ";
cin>>id[record];
cout<<"ID Name Price"<<endl;
cout<<id[record]<<" "<<name[record]<<" "<<price[record]<<endl;
break;
//Edit Record
case 'e':
case 'E':
cout << "Enter ID you wish to edit: ";
cin>>id[record];
cout<<"Input NEW name: ";
cin>>name[record];
cout<<"Input NEW price: ";
cin>>price[record];
break;
//Delete Record
case 'd':
case 'D':
cout << "Enter ID you wish to delete";
cin>>id[record];
id[record]=0;
name[record]=none;
price[record]=0;
break;
//View All Records
case 'l':
case 'L':
cout<<"ID Name Price"<<endl;
for(i=1; i<totalrecord+1; i++){
cout<<id[i]<<" "<<name[i]<<" "<<price[i]<<endl;
}
break;
//Exit Program if invalid input
default:
back=false;
break;
}
}
return 0;
}
Well, one problem is that here:
int i, j, record=0, totalrecord;
int id[record];
string name[record];
double price[record];
You are trying to create variable length arrays, which isn't supported in C++. Not only that, but even if it were supported you would be creating an array of size 0, since record = 0.
Thus, you need a compile-time constant value for the size of the array that isn't 0.
constexpr std::size_t RECORD_SIZE = 100; // make this any other that isn't less than or equal to 0
Then, you can create your array like this
int id[RECORD_SIZE];
string name[RECORD_SIZE];
double price[RECORD_SIZE];
However, one problem with this strategy is that if you want have an amount of records that exceed the RECORD_SIZE since you can't resize the array. Thus, I recommend that you see my final point.
Your logic when viewing, editing, and deleting a record is also incorrect since you always access an invalid index as the value of record will point to an empty slot so long as the array isn't full. Not only that, but there is no error checking.
Nonetheless, I assume you wanted to do those operations based on an index.
std::size_t index = 0;
std::cin >> index;
Then for the viewing and editing operations you would use that index to manipulate the records.
For the deleting operation, you would have to shift the records that are to the right of the deletion point left.
However, for these tasks I'd ultimately recommend that you use std::vector because it has support for all the operations you require and because it has a much greater dynamic capacity.
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.
Ok so I'm having trouble with this C++ program I am supposed to do for school and I need some help with an error I keep getting. I have the basics but I need help with classes and objects.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
double accountBalance;
string name;
public:
BankAccount();
BankAccount(string,double);
double deposit(double);
double withdraw(double);
void checkBalance();
};
BankAccount::BankAccount()
{
accountBalance=0;
name="";
}
BankAccount::BankAccount(string name,double money)
{
name=name;
accountBalance=accountBalance;
}
double BankAccount::deposit(double money)
{
accountBalance+=money;
return accountBalance;
}
double BankAccount::withdraw(double money)
{
accountBalance-=money;
return accountBalance;
};
void BankAccount::checkBalance()
{
cout<<"The balance on the account is $"<<accountBalance<<"!!"<<endl;
};
int main(int argc, char *argv[])
{
int c;
double m;
string n;
cout<<"==================Bank======="<<endl;
cout<<"[1] Open a new Bank Account |"<<endl;
cout<<"[2] Deposit money |"<<endl;
cout<<"[3] Withdraw money |"<<endl;
cout<<"[4] Check balance |"<<endl;
cout<<"============================="<<endl;
cout<<endl;
cout<<"What would you like to do :";
cin>>c;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
BankAccount::BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
BankAccount.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
BankAccount.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
BankAccount.checkBalance();
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
So if you could help me that would be very much appreciated.
BankAccount is a type name, not a variable name. You cannot invoke instance methods on a type.
Create a variable of type BankAccount, assign it an instance, and then call methods on the instance using the same notation that you already have:
BankAccount acct;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
acct = BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
acct.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
acct.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
acct.checkBalance();
break;
}
BankAccount is a class you defined, you cannot use :: operator to access non-static members of that class. In this case, you need to first create an object of BankAccount, then using dot operator to access the deposit and other member functions since those non-static member functions are associated with instance of the class.
If deposit and other relevant functions are static member function, you can do that.
I can't seem to figure out why this while loop stopped looping. It was doing fine before I moved some code around. Now I got something else working and it just doesn't loop. I've also tried making quit a bool set to true and tried to have it loop while it was true until the user hit 4 to exit in which case it would turn it to false but that didn't work. I also tried adding a while loop to the function of showMenu but that also didn't work. I know it must be something simple I just can't catch it. gggrrrr.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};
int showMenu(double balance);
double transaction(double amount, double balance, transType trans);
int menuSwitch;
int quit=0;
int _tmain(int argc, _TCHAR* argv[]){
int amount=0,balance=0;
while(quit!=4){
showMenu(balance);
switch (menuSwitch){
case DEPOSITE:
cout<<"Enter the amount of deposit: ";
cin>>amount;
cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
break;
case WITHDRAW:
cout<<"Enter the amount of withdraw: ";
cin>>amount;
if(amount>balance){
cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
}
else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
break;
case EXIT:
cout<<"Have a Nice Day."<<endl;
quit=4;
break;
}
return 0;
}
}
int showMenu(double balance){
// while(quit==true){
cout<<"Your Online Checking Account System"<<endl;
cout<<"-------------------------------------------"<<endl;
cout<<"Select an option:"<<endl<<endl;
cout<<" 1. Set up the account."<<endl;
cout<<" 2. Deposit Funds into your Account."<<endl;
cout<<" 3. Withdraw Funds out of your Account."<<endl;
cout<<" 4. Exit"<<endl;
cout<<endl<<">>";
cin>>menuSwitch;
switch (menuSwitch){
case SETUP:
cout<<"Enter the balance: ";
cin>>balance;
cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
break;
}
return balance;
// }
}
double transaction(double amount, double balance, transType trans){
double withdraw = balance-amount;
double deposite = balance+amount;
if(trans=DEPOSITE){
return deposite;
}
else
return withdraw;
}
//return balance;
You return 0 within the switch brackets, ie inside the while loop. Change it so that you return 0 outside of the while loop.