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 12 months ago.
Improve this question
i wrote a function that check if the variable is integer or not but if I put double or float it give me that it`s integer can anyone help me
bool check(int a) {
if (a == (int)a) {
cout << "integer" << endl;
}
else {
cout << "not integer" << endl;
}
return 1 ;
}
void main() {
int a;
cout << "enter a number" << endl;
cin >> a;
check(a);
}
You are taking in an integer value as an argument:
bool check(int a) // int a
So for example, if you pass in 3.12 into check(), then it will get converted to 3. That's why:
a == (int)a
.. will mean:
3 == (int)3 // true
..while to get the correct result, it should be:
3.12 == (int)3 // false
So to fix your issue, just change the parameter type:
bool check(double a)
..and also the input variable type:
double a;
Samples:
enter a number
3
integer
enter a number
3.12
not integer
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
In the code below I wanted the answer to be up to 2 decimal places but the result only has 1 decimal place. I already used the setprecision.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cin >> a >> b;
if (b > a && a % 5 == 0)
{
cout << ((b - 0.50) - a);
}
else
{
cout << b;
}
}
How can I get 2 decimal places in the answer?
I already used the setprecision.
Certainly you are not using it right, for two decimal places, regarldess of the integral part, you need to use std::fixed.
Example:
//...
if (b > a && a % 5 == 0)
{
std::cout << std::fixed << std::setprecision(2) << ((b - 0.50) - a);
// ^^^^^^^^^^
}
//...
Sample exec:
Input:
5
10
Output:
4.50
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
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
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
int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
How to make result = x (+ or - or / or *) y (+ or - or / or *) z !?
Reading the operators in between the numbers is simple:
long long x,y,z;
char f,g;
cin >>x>>f>>y>>g>>z;
// See what you've got
cout << x << " " << f << endl;
cout << y << " " << g << endl;
cout << z endl;
However, figuring out the result of the operation is trickier: you need to check the values you've got in f and g, and perform the operations as needed. Note that there must be no space between your numbers and the operators, otherwise the input would be processed incorrectly.
Demo.
This is probably at the core of the exercise that you are solving, so I will suggest that you write a function like this:
long long compute(long long a, long long b, char op) {
... // Check the operator, and return the result
}
With this function in hand, you can produce the result in one simple call:
long long result = compute(compute(x, y, f), z, g);
Once you write the compute function, this should give the result that you expect.
You can do cin>>astring. And separate the string by delimiter and convert them to integer.
For example:
1,2,3
will become '1','2','3'.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I need to write my own sqrt function: double my_sqrt_1(double n)
How would I go about doing this? At first I tried putting this outside of "int main()":
double my_sqrt_1(double n)
{
int x = 1;
x = (x + n / x) / 2;
}
I then put this:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << x;
}
I also tried:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << my_sqrt_1;
}
None of this worked though. I'm probably doing this completely wrong, but it made sense in my head.
"I'm probably doing this completely wrong ..."
Sorry to say that, but yes.
You need a variable to receive input, and call your function passing that variable
int main() {
cout << "Please enter a value ";
double myNumber;
cin >> myNumber;
cout << '\n' << my_sqrt1(myNumber) << endl;
}
Also your function is supposed to return the result of the calculation
double my_sqrt_1(double n) {
double x = 1.0;
// ^^^^^^ ^^
x = (x + n / x) / 2.0;
// ^^
return x; // <<<<<<<<<<<<<<
}
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 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}