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;.
Related
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 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 5 years ago.
Improve this question
When I tried to give the string input like "Hello" it is producing an error. How can I check that when I give string input string it should ask me to give correct input?
int y,m,d,h,min,s;
do
{
cout<<" Please enter the year: ";
cin>>y;
}while(y < 1970 || y > 2020);
#include "iostream"
#include<limits>
using namespace std;
int input()
{
int y;
do
{
std::cin.clear();
std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
std::cout << "Give the year" << std::endl;
std::cin >> y;
}
while (std::cin.fail() || y < 1970 || y > 2020);
return y;
}
main()
{
int x = input();
}
If the input cannot be converted to a int (in your case), then the failbit will be set for std::cin. This can be retrieved by calling cin.fail().
std::cin >> y;
if (std::cin.fail()) {
std::cout << "data entered is not of int type";
}
You can also use !std::cin instead of std::cin.fail().
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 6 years ago.
Improve this question
i cant figure out where to put all the loop parts for a simple y/n (repeat/exit) loop.i tried to find answers, but none are clear enough for my particular case.
P.S. iam a beginner at coding, so please dont make it too complicated unless necessary
this is my code so far
#include <stdio.h>
#include <iostream>
using namespace std;
// input function
void Input (float &x, float &y);
float a=1.0, b=1.0, result;
char operation;
char yesNO;
int main ()
{
do {
cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";
cout << "Geef een opdracht (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);
cout << "Het antwoord is: " << result << endl;
system ("pause");
return 0;
}
while (yesNO == 'y');
void Input (float &x, float &y)
{
a = x;
b = y;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "foutieve invoer: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}
}
I'll ignore some of the things wrong with your program and answer the question directly.
Two things:
You are never asking the user if they want to continue
You are aborting your loop by returing out of main()
So replace these 2 lines:
system ("pause");
return 0;
with a query that asks the user if they want to continue, and populate the variable yesNO with their answer.
It stops because of the return statement in "int main". I would suggest using "void main ()" instead of "int main ()". But if you want to use "int main ()", shift the "return 0" below the while statement. You also need to ask the user if he or she wants to continue. Try this: (Ignore the bad spacing)
int main () {
do {
cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";
cout << "Geef een opdracht (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);
cout << "Het antwoord is: " << result << endl;
cout << "Press y to continue: ";
cin >> yesNo;
} while (yesNO == 'y');
return 0;
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
float distance(tocki *A, tocki *B);
int main()
{
struct tocki{
int x, y;
};
tocki A, B, C;
cout << "x = ";
cin >> A.x;
cout << "y = ";
cin >> A.y;
cout << "x = ";
cin >> B.x;
cout << "y = ";
cin >> B.y;
cout << "x = ";
cin >> C.x;
cout << "y = ";
cin >> C.y;
cout << distance(&A, &B);
return 0;
}
//distance between (x1,y1) i (x2,y2) e d = sqrt((x2-x1)^2 - (y2-y1)^2);
float distance(tocki *A, tocki *B){
return sqrt(pow(A.y - A.x, 2) - pow(B.y - B.x, 2));
}
The errors I'm getting are:
'tocki' was not declared in this scope
'A' was not declared in this scope
'tocki' was not declared in this scope
'B' was not declared in this scope
On this line:
float distance(tocki *A, tocki *B);
So, what exactly am I doing wrong here? I want to pass a struct to function and get the result of the function in my main() program.
place the toki struct outside the main function
The struct tocki should be declared before the declaration of the function distance, so the compiler knows that the struct exists when check the type of the parameters. Moreover, you should use A->y ecc. , because you have passing a pointer to the struct.