\a3.cpp(75): error C2563: mismatch in formal parameter list
I'm certain I'm passing the function checkout with 3 doubles, I don't know why I'm getting the error I am. Please help.
#include <iostream>
#include <cstdlib>
using namespace std;
const double peanut_PRICE = 1.80;
const double peanut_SHIP = 0.50;
const double BOOK_PRICE = 9;
const double BOOK_SHIP = 1.06;
const double MOVIE_PRICE = 13.99;
const double MOVIE_SHIP = 0.05;
double checkout (double myamountofbooks, double myamountofmovies, double mypoundsofpeanuts)
{
myamountofbooks = myamountofbooks * (BOOK_PRICE + BOOK_SHIP);
myamountofmovies = myamountofmovies * MOVIE_PRICE * (1 + MOVIE_SHIP);
mypoundsofpeanuts = mypoundsofpeanuts * (peanut_PRICE + peanut_SHIP);
return (myamountofbooks + myamountofmovies + mypoundsofpeanuts);
}
bool validUserImput (int whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
bool validUserImput (double whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
int main()
{
//===========================Declaration Statements==================================
double amountofbooks = 0;
double amountofmovies = 0;
double poundsofpeanuts = 0;
int whereUserWantsToGoNext = 0;
while (! (whereUserWantsToGoNext == 4) )
{
cout << "1. Books\n2. Peanuts\n3. Movies\n4. Checkout\n" << endl;
cin >> whereUserWantsToGoNext;
if (!validUserImput(whereUserWantsToGoNext)) cout << "INVALID IMPUT" << endl;
if (whereUserWantsToGoNext == 1){
cout << "Please enter your number of books";
cin >> amountofbooks;
if (!validUserImput(amountofbooks)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 3){
cout << "Now please enter the number of movies you've selected";
cin >> amountofmovies;
if (!validUserImput(amountofmovies)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 2) {
cout << "Please enter the weight(in pounds) of your peanuts";
cin >> poundsofpeanuts;
if (!validUserImput(poundsofpeanuts)) cout << "INVALID IMPUT" << endl;
}
if (validUserImput == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
}
cin >> amountofbooks;
}
The problem is here:
if (validUserImput == 4) ...
validUserImput is a function, but you are not calling that function, you are trying to compare it to 4.
If you wanted to keep track of the number of valid inputs you received, you could instead add a new variable that you manually increment on every valid input.
The last if - you are comparing function pointer to an integer. try this:
if (validUserImput(3) == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
I assume you want to display the result of the checkout function if the user selects 4. So you probably wanted to write:
if (whereUserWantsToGoNext == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts) << endl;
Related
The search function should get a value from the user to search for it, if the value is found, then it should print it out, and if not found it should print not found.
However, in my code every time I write the number that is in the array is gives me the false option although it is in the array stored.
`
#include <iostream>
using namespace std;
const int size = 100;
int partsmenu(int menu_option);
void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts);
int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification []);
void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts);
int main()
{
const int size = 100;
int menu_option=0, option, displaysearch;
char part_number[size][10];
double price[size];
char classification[size];
int number_of_parts = 0;
char search_target[size];
//using switch statment to make it look like a menu option
do {
switch (option = partsmenu(menu_option))
{
case 1:
readparts(part_number, price, classification, number_of_parts);
break;
case 2:
display_parts(part_number, price, classification, number_of_parts);
break;
case 3:
displaysearch = search(part_number, search_target, number_of_parts, price, classification);
break;
case 4:
break;
default:
cout << "Not valid..." << endl;
break;
}
cout << endl;
} while (option != 4);
return 0;
}
int partsmenu(int menu_option)
{
cout <<"1) Enter new part number\n2) View all part numbers\n3) Search for part\n4) Exit\n\nEnter an option: ";
cin >> menu_option;
return menu_option;
}
void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
// using for loop to store part number, price, and classification in the array
int number;
cout << "Enter number of parts to add:";
cin >> number;
cout << endl;
int i;
for (i = number_of_parts; i < (number_of_parts+number); i++)
{
cout << "Enter part number: ";
cin >> part_number[i];
cout << "Enter price: ";
cin >> price[i];
cout << "Enter classificarion: ";
cin >> classification[i];
//using if statment to check for the classificarion
if (classification[i] == 'A' || classification[i] == 'B' || classification[i] == 'C')
cout << "";
else
{
cout << "Invalid case..." << endl;
cout << "Enter Valid class [A, B, C]: ";
cin >> classification[i];
cout << endl;
}
cout << endl;
}
number_of_parts = i;
}
int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification[])
{
//searching for specific data
bool found = false;
int value;
cout << "Enter part number: ";
for (int j = 0; j < number_of_parts; j++)
{
cin >> search_target;
for (int i = 0; i < number_of_parts && found == false; i++)
{
if (part_number[i] == search_target)
found = true;
value = i;
}
}
if (found == true)
{
for (int i = 0; i < number_of_parts; i++)
{
cout << "Part ID\t\tPrice\t\tClass" << endl;
cout << " --------------------------------------------" << endl;
cout << part_number[value] << "\t\t" <<price[value]<< "\t\t" <<classification[value]<< endl;
}
}
else
{
cout << "No parts found..." << endl;
value = -1;
}
return value;
}
void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
// displaying the data
cout << "Part ID\t\tPrice\t\tClass" << endl;
cout << "--------------------------------------------" << endl;
for (int i = 0; i < number_of_parts; i++)
{
cout << part_number[i] << "\t\t" << price[i] << "\t\t" << classification[i] << endl;
}
cout << endl;
}
`
I am trying to find what is wrong with code but could not find any fault with it. Everything else works fine.
You compare C strings with strcmp not ==. Like this
if (strcmp(part_number[i], search_target) == 0)
If you use == then all you are comparing is the addresses of the strings, the addresses of two strings can be different even if the string contents are the same.
strcmp compares the actual characters in the strings, not the addresses. It returns 0 if the two strings are the same.
I'm having trouble passing the reentered optionPackageCode after the nested while loop completes (cin >> optionPackageCode;) back to the first while loop so that it can check the conditions with the updated code. No clue what's wrong.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//Declaring variables
double basePrice = 0.0;
double subTotal = 0.0;
double associatedCosts = 0.0;
double finalPrice = 0.0;
int sub = 0;
int tries = 0;
string optionPackageCode = "";
//Populating arrays
string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC"};
string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "Custom"};
double packageCostArray[5] = { 1500.00,3250.00,4575.00,7500.00,5220.00 };
//Ask for input
cout << "Welcome! Please enter the base price and option package code for your vehicle!"
<< endl;
cout << fixed << setprecision(2);
cout << "Base Price: ";
cin >> basePrice;
cout << endl << "Option package code: ";
cin >> optionPackageCode;
//While loop
while (tries < 5 && optionPackageCodeArray[sub] != optionPackageCode)
{
tries += 1;
while (optionPackageCodeArray[sub] != optionPackageCode && sub < 5)
sub += 1;
//end while
cout << "Sorry, that code wasn't found in our database! Please try again: " << endl;
cin >> optionPackageCode;
} //end while
//Calculations and final output
if (optionPackageCodeArray[sub] == optionPackageCode)
{
subTotal = basePrice + packageCostArray[sub];
associatedCosts = subTotal * 0.15;
finalPrice = subTotal + associatedCosts;
cout << "The final cost for your vehicle with " << packageNameArray[sub] << " trim is $" << finalPrice << endl;
}
else
cout << "Sorry, you are out of tries...";
system("pause");
return 0;
}
So im working on a project for school and i just cant get this to work. everything else seems to run but when i input the users choice for q, s, z10, z25,z5,z1 it loops indefinetly. i cant figure out why it keeps running the function over and over. im not quite sure if im just typing something wrong or what but this is the only thing im really stuck on currently. Here is the code
#include<iostream>
#include<string>
using namespace std;
struct Inventory
{
int Pack_25oz;
int Pack_10oz;
int Pack_5oz;
int Pack_1oz;
};
void show_Menu();
void initialize (Inventory& I);
void add_25OZ (Inventory& I, int P25);
void add_10OZ (Inventory& I, int P10);
void add_5OZ (Inventory& I, int P5);
void add_1OZ (Inventory& I, int P1);
void Place_Order(Inventory& I, int amount);
void show (Inventory& I);
int main(){
Inventory I;
string choice;
int nop= 0;
initialize(I);
show_Menu();
cout << "Dear User! please make a choice from above menu : " << endl;
getline(cin,choice);
while(choice!="q" or choice != "Q"){
if(choice == "s"|| choice == "S"){
show(I);
}
else if(choice == "z25" || choice == "Z25"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_25OZ(I, nop);
}
else if(choice == "z10" || choice == "Z10"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_10OZ(I, nop);
}
else if(choice == "z5"||choice == "Z5"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_5OZ(I, nop);
}
else if(choice == "z1"|| choice == "Z1"){ // choice equal to "z1" or "Z1"
cout << "Enter the number of packages : \n" ; // prompt for number of packages
cin >> nop ; // accept user input
add_1OZ( I, nop);// call add_1OZ function
}
else if(choice == "o"||choice == "O") {
cout << "Enter the number of ounces : ";// prompt for number of ounces
int noun; // noun stands for number of ounces
cin >> noun;// accept user input
Place_Order( I, noun);
}
else if (choice == "q" || choice == "Q"){ // Output final message that the program has ended
cout << "The program has ended.";
break;
}
else
cout << "invalid comand" << endl;
};
return 0;
}
void show_Menu(){
cout << "S - Show Inventory" << endl;
cout << "Z25 - Add 25 oz packages"<< endl;
cout << "Z10 - Add 10 oz packages"<< endl;
cout << "Z5 - Add 5 oz packages"<< endl;
cout << "Z1 - Add 1 oz packages "<< endl;
cout << "O - Place order"<< endl;
cout << "Q - End"<< endl;
}
void show (Inventory& I)
{
cout << "The Inventory comprises of the following:\n 1. Pack_25oz : " << I.Pack_25oz << "\n 2.
Pack_10oz : " << I.Pack_10oz << "\n 3. Pack_5oz : " << I.Pack_5oz << "\n 4. Pack_1oz : " <<
I.Pack_1oz <<endl ;
}
void initialize (Inventory& I) {
int var = 0 ;
I.Pack_25oz = var;
I.Pack_10oz = var;
I.Pack_5oz = var;
I.Pack_1oz = var;
}
void add_25OZ (Inventory& I, int P25) {
I.Pack_25oz += P25;
}
void add_10OZ (Inventory& I, int P10) {
I.Pack_10oz += P10;
}
void add_5OZ (Inventory& I, int P5) {
I.Pack_5oz += P5;
}
void add_1OZ (Inventory& I, int P1) {
I.Pack_1oz += P1;
}
void Place_Order(Inventory& I, int amount) {
int subtract;
subtract = amount / 25;
if (I.Pack_25oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_25oz -= subtract; }
amount = amount % 25;
subtract = amount / 10;
if (I.Pack_10oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_10oz -= subtract; }
amount = amount % 10;
subtract = amount / 5;
if (I.Pack_5oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_5oz -= subtract; }
amount = amount % 5;
subtract = amount / 1;
if (I.Pack_1oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_1oz -= subtract;
cout << "Order Fufilled" << endl;
}
}
It seems you call getline outside of the loop. so choice is never updated.
I have this code here that works pretty decent except for when we get down to the 'processData' function where I receive the error
"The variable 'totalPay' is being used without being initialized."
I've been working on this for a couple hours and my brain is going numb, any help would be appreciated.
#include <iostream>
#include <string>
#include <climits>
#include <iomanip>
using namespace std;
// Prototypes
int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
void getData(string&, int&, char&, double&);
char getChoice(string prompt, char char1, char char2);
double processData(char service);
void display(string, int, char, double);
// Regular service
const double RCHARGE = 10.00; // Regular service base rate for first REG_MINS.
const double RPERMIN = 0.20; // Regular service cost per minute over REG_MINUTES.
const int REG_MINS = 50; // Regular service free minutes.
// Premium service
const double PCHARGE = 25.00; // Minimum charge for premium service.
const double P_PER_MIN_DAY = 0.10; // Charge per day minutes after 75.
const double P_PER_MIN_NIGHT = 0.05; // Charge per night minutes after 100.
const int P_DAY = 75; // Number of free minutes allowed during the day.
const int P_NIGHT = 100; // Number of free minutes allowed during the night.
int main()
{
string name; // Stores the users name.
int accountNumber; // Stores the users account number.
char serviceType; // Stores the users service type.
double minutes; // Stores the users minutes.
double totalPay; // Recieves the total pay from processData.
bool loopies = true;// Controls the loop while condition is true.
while (loopies == true)
{
getData(name, accountNumber, serviceType, minutes);
totalPay = processData(serviceType);
display(name, accountNumber, serviceType, totalPay);
}
return 0;
}
// Checks ints to make sure they are valid.
int readInt(string errorMsg, int min, int max)
{
int someInt;
bool valid = false;
do
{
// User entering account number
cin >> someInt;
// Verifying the data is valid (0 to 9999)
valid = (someInt >= min && someInt <= max);
// Clearing out invalid data and prompting for re-entry
if (cin.fail() || !valid || !isspace(cin.peek()))
{
cout << "Error! Invalid input." << endl;
cout << errorMsg << " must be whole number between " << min << " and " << max << endl;
cout << "Try Again: ";
cin.clear(); // Clearing the fail state
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
} while (!valid);
// Clears out following incorrect data once correct
cin.ignore(100, '\n');
return someInt;
}
// Takes all user data and checks input.
void getData(string &name, int &account, char &service, double &bill)
{
cout << "Enter customer name or \"Exit\" to end program: ";
getline(cin, name);
if (name == "Exit" || name == "EXIT" || name == "exit")
{
cout << "Program closing..." << endl;
system("pause");
exit(EXIT_SUCCESS);
}
cout << "Enter account number: ";
account = readInt("Account", 0, 10000);
cout << "Enter service type (R for regular or P for premium): ";
service = getChoice("Service", 'r', 'p' );
}
char getChoice(string input, char char1, char char2)
{
char character; // Stores the users character.
bool valid = false; // Controls the loop.
do
{
cin >> character;
character = tolower(character);
valid = (character == char1 || character == char2);
if (!valid || !isspace(cin.peek())) {
cout << "Invalid input!" << endl;
cout << input << " needs to be " << char1 << " or " << char2 << endl;
cout << "Try again: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
} while (!valid);
cin.ignore(100, '\n');
return toupper(character);
}
double processData(char service)
{
int dayMin; // Stores the users day minutes.
int nightMin; // Stores the users night minutes.
double totalPay; // Stores the total pay.
double dayPay; // Stores the pay for day minutes.
double nightPay; // Stores the pay for night minutes.
if (service == 'r')
{
cout << "Enter minutes used: ";
cin >> dayMin;
dayMin = readInt("Minutes ", 0, INT_MAX);
if (dayMin > REG_MINS)
{
dayMin -= REG_MINS;
totalPay = (dayMin * RPERMIN) + RCHARGE;
}
else
{
totalPay = RCHARGE;
}
totalPay = nightPay + dayPay - RCHARGE;
}
else if (service == 'p')
{
cout << "Enter day minutes: ";
cin >> dayMin;
dayMin = readInt("Minutes ", 0, INT_MAX);
if (dayMin > P_DAY)
{
dayMin -= P_DAY;
dayPay = (dayMin * P_PER_MIN_DAY) + PCHARGE;
}
else
{
dayPay = PCHARGE;
}
cout << "Enter night minutes: ";
cin >> nightMin;
nightMin = readInt("Minutes ", 0, INT_MAX);
if (nightMin > P_NIGHT)
{
nightMin -= P_NIGHT;
nightPay = (nightMin * P_PER_MIN_NIGHT) + PCHARGE;
}
else
{
nightPay = PCHARGE;
}
}
return totalPay;
}
void display(string name, int account, char service, double bill)
{
string switchService;
switch (service)
{
case 'R':
switchService = "Regular";
break;
case 'P':
switchService = "Premium";
break;
default:
cout << "Invalid character.";
}
cout << fixed << setprecision(2);
cout << endl;
cout << "Customer Name: " << name << endl;
cout << "Account number: " << account << endl;
cout << "Service type: " << switchService << endl;
cout << "Amount due: $" << bill << endl << endl;
}
In processData(), if service is neither p nor r, totalPay is never assigned before returning.
You may initialize it on declaration like
double totalPay = 0.0;
I am a newby at this and am working on my fist if/else program. I am having trouble getting the first if statement to recognize my input of "r". I tried playing with just one statement at a time I was able to input all the examples of input the teacher gave us with the desired output for residential and business. However when I run the program altogether I have a problem. I select R for residential, 0 for additional connections, 0 for premium channels and instead of output of $18.50 I get the business fee of $75.00. I am sure it is a simple mistake but I can't figure out what I am doing wrong. Can someone who knows how to work an if/else give me some insight on this!
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const float BASIC_RESIDENTIAL = 18.50;
const float BASIC_BUSINESS = 75.00;
const float CONNECT_RESIDENTIAL = 6.50;
const float CONNECT_BUSINESS = 5.00;
const float PREMIUM_RESIDENTIAL = 7.50;
const float PREMIUM_BUSINESS = 50.00;
char customerType;
int numOfConnections;
int numOfPremiumChannels;
float amountCableBill;
cout << fixed << setprecision(2);
cout << "Residential or Business [R or B]? ";
cin >> customerType;
cout << endl << endl;
cout << "How many Additional Connections? ";
cin >> numOfConnections;
cout << endl << endl;
cout << "Total number of Premium Channels: ";
cin >> numOfPremiumChannels;
cout << endl << endl;
if (customerType == 'R' || customerType == 'r')
{
amountCableBill = BASIC_RESIDENTIAL + CONNECT_RESIDENTIAL * numOfConnections + PREMIUM_RESIDENTIAL * numOfPremiumChannels;
}
//else customerType == 'B' || customerType == 'b'; // unnecessary
{
if (numOfConnections <= 9)
amountCableBill = BASIC_BUSINESS + PREMIUM_BUSINESS * numOfPremiumChannels;
else
amountCableBill = BASIC_BUSINESS + (numOfConnections - 9) * CONNECT_BUSINESS + PREMIUM_BUSINESS *numOfPremiumChannels;
}
cout << "Total amount of Cable Bill: " << amountCableBill << endl << endl;
cout << "Press <ENTER> to end..." << endl;
_getch();
return 0;
}
While the condition else if (customerType == 'B' ...) may be redundant, you still have to put an else before the opening brace of the branch.
It's
if (condition) { code } else { code }
You need else in the condition (unless you want "some other code" to be executed every time)
if (customerType == 'R' || customerType == 'r')
{
//Some Code
}
else //<--Notice else
{
//Some other code.
}