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'.
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 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
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 5 years ago.
Improve this question
Fill up the blanks with appropriate keyword to get the desired output according to the test cases. (Language use c++)
Sample Test Cases
input output
Test Case 1 4 square = 16, ++ square = 25
test case 2 -8 square =64, ++ square = 49
#include <iostream>
using namespace std;
______ int SQUARE(int x) { ______ x * x; }
int main() {
int a , b, c;
cin >> a ;
b = SQUARE(a);
cout << "Square = " << b << ", ";
c = SQUARE(++a);
cout << "++ Square = " << c ;
return 0;
}
Second blank place is "return"
First may be "inline" or nothing
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 8 years ago.
Improve this question
I want to store this string into an an array.
space= 0
A,a =1
B, b =2
C , c = 3
.
.
Z, z= 26
string myArray[26] =
{ "A", "B", "C", "D",”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,
”Q”,”R”,”S”,T”,”U”,”V”,W”,X”,”Y”, ”z” };
for (int i = 0; i < myArray; i++)
{
myArray[] = myArray[i]
cerr << myArray[i] << endl << endl;
}
Is that how to get each character with number?
What you've got is an array of strings, not an array of characters. A string is a container of characters, in the sense that it is capable of holding multiple characters. Your task can be solved with one or two strings, depending on your design preferences (see below).
A, a =1
B, b =2
You are placing two characters per position. However, strings cannot hold more than one character at a single index. If you need both the upper and lower case character to occupy the same spot, you need to make either two strings, or two spots.
Here is the first approach (two strings):
string upper = " ABCDEF...";
string lower = " abcdef...";
int pos = ...; // The desired position
cout << upper[pos] << endl;
cout << lower[pos] << endl;
Here is the second approach (two positions):
string pairs = " AaBbCcDdEeFf...";
int pos = ...; // The desired position
cout << pairs[2*pos] << endl; // Upper
cout << pairs[2*pos+1] << endl; // Lower
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 have defined a struct data and assigning it to a char*. However the size of strlen function always gives result as 1 and cout doesn't show anything. Here is my code:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
char *ch = (char*)&d;
cout <<"Length:" strlen(ch) << endl;
cout << ch << endl;
}
The output is:
Length: 1
Please help me in understanding what's going on ?
The strlen function counts all bytes in the passed string until it finds the character '\0'. This character is the same as the integer 0.
So what the call to strlen is count the number of bytes until it finds a zero, and in your case the zero happens to be in the second byte of the structures binary representation, meaning that the "string length" is one.
The only thing that you can deduce from this is that you are on a little endian system. Other than that, the call is undefined behavior as the "string" isn't actually a string.
The reason it returns 1 is because you are treating the struct as a char array. Doing this you are re-interpreting the contents of the struct - the integer 10, which is probably stored in memory as 0x0A000000 - as a string. And that yields a length of 1 (only 1 non-zero value before a null-character in the array)
Even though you're trying to treat something as a string which isn't a string, if all you want to do is print out the values in the struct, you can do something like:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
cout << "x is " << d.x << ", y is " << d.y << endl;
}
Why are you trying to treat that struct as a string?