Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is mine Full code. Am trying to Include inpatient if the case is one(1) but some highlight show that is wrong. is there anyhow to fix this if not can you please tell me another way to do it as long as it include inpatient if one(1) is Entered
void selection(int &);
void processor(int &);
void inPatient(double &, double &, double &, double &);
int main()
{
int selected, include;
double numberOfDays, dailyRate, chargesForHospital, hospitalMedicationCharge;
selection(selected);
validate(selected, selected);
processor(selected);
system("pause");
return(0);
}
void selection(int & selectedOption)
{
cout << "\nEnter Selection: ";
cin >> selectedOption;
}
void processor(int & selectedOption)
{
switch(selectedOption)
{
case 1:
inPatient(umberOfDays, dailyRate, chargesForHospital, hospitalMedicationCharge);
break;
case 2:
cout << "out-Pat" << endl;
break;
default :
cout << "Nothing Selected" << endl;
break;
}
}
void inPatient(double & numberOfDays, double & dailyRate, double & chargesForHospital, double & hospitalMedicationCharge)
{
cout << "The number of days spent in the hospital: ";
cin >> numberOfDays;
cout << "The daily rate: ";
cin >> dailyRate;
cout << "Charges for hospital services (lab tests, etc.): ";
cin >> chargesForHospital;
cout << "Hospital medication charges: ";
cin >> hospitalMedicationCharge;
}
There are a number of errors in the code that you posted, but I'll try to address your immediate problem. You're trying to call a function like this:
patric(int & gender, int & age)
but that's more like a function declaration. To actually call the function, you pass in arguments but omit the type, like this:
patric(someGender, someAge);
The int & in the declaration says that the parameters are references to values of type int, so the values you pass when you call patric should be of type int.
Also, you say that patric is overloaded. That means that there are several versions of the function, each with different parameter lists. So, maybe there's the one above as well as one that doesn't take any values -- maybe their declarations look like this, respectively:
void patric(int &gender, int &age);
void patric(void);
Given that, if you wanted to call the second version, you'd just call it:
patric();
(The void in the parameter list in the second version means that the function doesn't take any parameters. The void before the function name means that it doesn't return anything.)
Note also that you need a semicolon (;) following the function call.
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 9 months ago.
Improve this question
I'm having an issue with modifying a member of a class. I overloaded the operators and I think that I am calling the member correctly to modify it but am getting the issue that the "expression must be a modifiable l-value.
Any help would be appreciated
.h file
public:
account& operator+= (float x);
account& operator-= (float y);
float set_balance();
.cpp file
account& account::operator+=(float x)
{
this->acct_balance += x;
return *this;
}
account& account::operator+=(float y)
{
this->acct_balance -= y;
return *this;
}
float account::set_balance()
{
return this->acct_balance;
}
main file
//deposit
else if (imput == 2)
{
float deposit;
cout << "Please enter the amount to deposit: ";
cin >> deposit;
user1.set_balance() += deposit;
}
//withdrawl
else if (imput == 3)
{
float withdraw;
cout << "Please enter the amount to deposit: ";
cin >> withdraw;
user1.set_balance() += withdraw;
}
Your set_balance function doesn't set anything. You probably want this:
float& account::get_balance()
{
return this->acct_balance;
}
Then you can do user1.get_balance() += withdraw;.
This get_balance function gets the balance as a modifiable l-value, which is what you need.
Since you have an operator+=, you could also just do user1 += withdraw;.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a school task that I need to complete. I'm an entry level programmer, really just for some school stuff so it isn't that complicated. I just need to find out, what's the easiest way to cover all of the answers. Here is my code.
float e,d, m,n,d, no;
cout << "Enter the numerator of the first radian or if it doesn't have one, type no: ";
cin >> e;
cout << "Enter the denominator of the first radian or if it doesn't have one, type no: ";
cin >> m;
cout << "Enter the numerator of the second radian or if it doesn't have one, type no: ";
cin >> d;
cout << "Enter the denominator of the second radian or if it doesn't have one, type no: ";
cin >> n;
Then I need to solve an equation with them.
I would like it to work in every possible way but it's diffcult to cover all of the possible answers. Any tips, how should I start. I know it's probably confusing, I don't know if it's correct or not.
How to read number or some text from stream.
When you read a number, but something else is encounter, stream is set int invalid state (failbit flag is set). So to handle case first you have to clear error flag and then read a string.
Since you are expecting "no" text so if something else is encounter then failbit flag can be restored.
std::istream& read_value_or_NO(std::istream& in, std::optional<double>& x)
{
double y;
if (in >> y) {
x = y;
return in;
}
std::string s;
in.clear();
x = {};
if (in >> s && s == "no") {
return in;
}
in.setstate(std::ios::failbit); // error if it is not "no" string
return in;
}
https://godbolt.org/z/rPdTTMbvT
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I have a program whose purpose is to calculate the total bill for medical expenses using overloaded functions. There is a problem though when I try to call the function which is inside the if/else statement block.
Before I compile it there's really no indicator that lets me know there's an issue and I'm stuck, I'd appreciate some help. This is the complete error message I get: In function main': main.cpp:(.text+0x14d): undefined reference to bill(float, float)'
main.cpp:(.text+0x25b): undefined reference to `bill(float, float, float)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
here's the code:
#include <iostream>
#include <iomanip>
using namespace std;
float choice, service_charge, test_charge, medicine_charge;
float bill(float, float);
float bill(float, float, float);
int main()
{
cout << "Please input 1 if you are a member of"
<< " the dental plan" << ", Input any other number if you are not: " << endl;
cin >> choice;
if (choice == 1)
{
cout << "Please input the service charge: " << endl;
cin >> service_charge;
cout << "Please input the test charge: " << endl;
cin >> test_charge;
bill(service_charge, test_charge);
}
else
{
cout << "Please input the service charge: " << endl;
cin >> service_charge;
cout << "Please input the test charges: " << endl;
cin >> test_charge;
cout << "Please input the medicine charges: " << endl;
cin >> medicine_charge;
bill(service_charge, test_charge, medicine_charge);
}
return 0;
}
float bill(float &refservice, float &reftest)
{
cout << "The total bill is: $" << endl;
return refservice + reftest;
}
float bill(float &refservice, float &reftest, float &refmed)
{
cout << "The total bill is: $" << endl;
return refservice + reftest + refmed;
}
The signature of the prototype, float bill(float, float);, is not equivalent to the actual function definition's signature, which is float bill(float &refservice, float &reftest). Your other prototype and function have the same issue. Thus, the compiler doesn't recognize that you've already defined that function. You have to change the signature of your prototype to match. Your prototypes in that case would look like:
float bill(float&, float&);
float bill(float&, float&, float&);
One thing to note is that it's not clear why you have to pass those floats by reference, since you're not modifying them in any way.
For my homework assignment I'm supposed to make a create-your-own-adventure story. There are certain words in the text that are in all caps to represent boolean values that I need to display at the end if the player got them, like a status effect or something. I'm having trouble figuring out how to pass the booleans to the functions so that it makes it to the end of the program where I can display it. My program has functions within functions.
I've tried making the function that sets the boolean to true a boolean itself, then returning the boolean but that just ends the program it seems. I've also tried passing it through the first function call to see if it reaches the second but it doesn't seem like it wants to.
void A1();
bool A100(bool INTIM);
void A167();
void A232();
void A290();
void A13();
void A212();
void A173();
void A159();
void A161();
int main() {
bool INTIM;
A1();
cout << INTIM << endl;
return 0;
}
void A1()
{
int choice;
cout << "Well, Mr Artanon, ...\n 1. ’It’s you who’ll get a rare cut
across that corpulent neck of yours if you don’t speed things along, you
feckless blob of festering lard.’\n 2. ’Surely in such an industrious
kitchen, there must be a starter or two ready to send along and sate His
Abhorentness’s appetite?’\n (enter a menu option): ";
cin >> choice;
while (choice != 1 && choice != 2)
{
cout << "Enter in a valid choice (1 or 2)";
cin >> choice;
}
if (choice == 1)
{
A100();
}
if (choice == 2)
{
A167();
}
}
bool A100(bool INTIM)
{
int choice;
INTIM = true;
cout << " Repugnis turns a paler...\n 1. Onwards, Mr Artanon.\n (enter
in a menu option): ";
cin >> choice;
while (choice != 1)
{
cout << "Enter in a valid option (1)";
}
return INTIM;
A232();
}
What I'm wanting to happen is, the bool INTIM to be passed along so i can display it back in main with the cout statement. I know it will just be a 1 or 0 at the end but I'm just trying to get it to show up at least in the end when I display it. Again there are functions within functions in this program and that might be my problem but I wouldn't think so. There is also functions that come after this, this is not the end of the program and if I need to post the whole thing I will
Calling A100 as written, you need to pass in INTIM and accept the return value
INTIM = A100(INTIM);
But... The initiqal state of INTIM is never used, so you could
INTIM = A100();
and change A100 to look more like
bool A100()
{
int choice;
cout << " Repugnis turns a paler...\n 1. Onwards, Mr Artanon.\n (enter in a menu option): ";
cin >> choice;
while (choice != 1)
{
cout << "Enter in a valid option (1)";
cin >> choice; // added here because otherwise choice never changes
// and this loop will go on for a long, long time.
}
A232(); // moved ahead of return. Code after a return is not run
return true;
}
But since A232 is called and may set additional flags you cannot return, you have a design flaw: What if A232 also modifies a boolean? You can only return one thing from a function. You could pass A232's boolean in by reference, but what it A232 then calls B484 and it also has a boolean?
You don't want to have to pass around every possible boolean, that would be a confusing mess, so consider making a data structure that stores all of your booleans to pass around.
And that leads to an even better idea: encapsulating the booleans and the functions in the same data structure so that you don't have to pass anything around; it's all in the same place.
Do I need to pass them [the boolean results] to the functions?
Often, but not always, it is my preference to pass them by reference, and yes, it can get to be a big chain thru many functions. sigh.
But your question is "Do you need to pass them ...".
The answer is No.
Because
a) you have tagged this post as C++, and
b) the key feature of C++ is the user-defined-class.
Consider declaring every 'adventurous function' of your story within a class scope.
Each 'adventurous function', as an attribute of the class, is implemented with one 'hidden' parameter, the 'this' pointer to the class instance.
So .. if you place all your 'result' booleans as data attributes of the class, invoking any 'adventurous function' will also 'pass' all the class instance data attributes (all your bools!) as part of the invocation. No data is actually moving, just a pointer, the 'this' pointer.
It might look something like this:
#include <iostream>
using std::cout, std::cerr, std::flush, std::endl;
// using std::cin;
#include <iomanip>
using std::setw, std::setfill;
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
namespace AS // Adventure Story
{
class CreateYourOwnAdventureStory_t
{
private:
// diagnostic purposes
stringstream ssUI;
// command line arguments concatenated into one string
// contents: strings convertable to ints to mimic cin
bool INTIM;
// other results go here
public:
int operator()(int argc, char* argv[]) {return exec(argc, argv);}
private:
int exec(int argc, char* argv[])
{
int retVal = 0;
// capture all command line arguments into a string
for (int i=1; i<argc; ++i)
ssUI << argv[i] << " ";
cout << "\n ssUI: " << ssUI.str() << "\n\n\n";
A1();
cout << "\n INTIM : " << INTIM << endl;
// ?more here?
return retVal;
}
void A1()
{
int choice = 0;
cout << "Well, Mr Artanon, ...\n "
"\n 1. ’It’s you who’ll get a rare cut across that corpulent neck of yours "
"if you don’t speed things along, you feckless blob of festering lard. "
"\n 2. ’Surely in such an industrious kitchen, there must be a starter or two "
"ready to send along and sate His Abhorentness’s appetite?’"
"\n (enter a menu option): ";
ssUI >> choice; // cin >> choice;
if (choice == 1) { A100(); }
if (choice == 2) { A167(); }
}
void A100()
{
int choice = 0;
INTIM = true;
ssUI >> choice; // cin >> choice;
cout << "\n\n A100() choice:" << choice
<< " INTIM: " << INTIM << endl;
}
void A167()
{
int choice = 0;
INTIM = false;
ssUI >> choice; // cin >> choice;
cout << "\n\n A167() choice:" << choice
<< " INTIM: " << INTIM << endl;
}
// other action-functions go here
}; // class CreateYourOwnAdventureStory_t
typedef CreateYourOwnAdventureStory_t CreateYOAS_t;
} // namespace AS
int main(int argc, char* argv[]){return AS::CreateYOAS_t()(argc,argv);}
Notes:
This example grabs the command line parameters and appends them to a string stream. The result is use-able in a fashion much like your cin statements.
Did you notice you (probably) will not need forward declarations for your functions? The compiler has to scan a lot of the class declaration to decide various issues, and thus can figure out that A100 (and A167) are actually with-in the scope of AS::CreateYOAS_t::. The functions can still be moved into a cpp file, so you can still take advantage of separate compilation. (and maybe save some effort compiling smaller files, and only the changed files.)
Did you notice that the functions accessing INTIM simply use the bool, without needing any 'this->' to de-reference?
Main invokes a simple Functor. Nothing else. Main invokes operator(). Simple, minimal. The ctor and dtor are currently default. If you need to use the ctor to initialize results or other intermediate info, I would simply add it near the operator() implementation.
PS: You mentioned using bools to return results. You might as, an alternative, consider using a stringstream ... a single stream with text ... use like a log for capturing the ongoing game, or for a single simple overall report to the user.
Good luck.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I made a program that should take input print it. Then run a simple addition thing but when i use spaces in the input it skips through the addition. I do not know what the problem is.
this is the class stuff
#include <iostream>
#include <string>
using namespace std;
class Cheese {
private:
string name;
public:
void setName(string x){
cin >> x;
x = name;
}
string getName(){
return name;
}
void print(){
cout << name << endl;
}
};
this is the main stuff
int main()
{
string h;
Cheese hole;
hole.setName(h);
hole.getName();
hole.print();
this part is getting skipped through without letting me input
int x = 5;
int y = 16;
cout << x+y;
num(x);
int a;
int b;
int c;
cout << "Type in a number and press enter.";
cin >> a;
cout << "Repeat.";
cin >> b;
c = a+b;
cout << c << endl;
if(c <= 21){
cout << "Good job!";
}
else {
cout << "You fail!";
}
return 0;
}
I suggest you divide the responsibilities a little differently. The Cheese class's setName function should simply take a string and set the instance's member variable to the given argument.
Then your program can read from standard input and populate a string within main, and pass that string to setName.
To be more concrete:
class Cheese {
private:
string name;
public:
void setName(const string& x){
// change this code to set the 'name' member variable
}
[...]
};
And the main becomes:
int main()
{
string h;
Cheese hole;
std::string input_name;
cout << "Type a name and press enter.";
cin >> input_name; // Will read up to first whitespace character.
hole.setName(input_name);
hole.getName(); // this is a no-op: compiler may warn of unused return value
hole.print();
In general, reading standard input as part of a class's interface is a bad idea, because it makes it hard to re-use that class in the future (for example, with programs that take input from a file instead of from a human at a console).
The input that you pass to cin input stream skips any white space, Tab space or newline. If you wish to input string then you can use cin.getline(string s). The input after the white space gets passed to next waiting cin, as the next cin accepts integer and it get a character string it skips that. Thus when enter a string with white spaces the program skips the remaining part.