I have 4 variables called: number, x , y, z.
x = 1,
y = 2,
z = 3
I would like the variable number to be able take the values of x, y and z and be able to display value of 123
I have tried this so far:
number = x + y + z; but the answer is 6.
or
number = x << y << z; but the output is not what I want.
Any help would be appreciated.
in C++:
cout << x << y << z
In C:
printf("%d%d%d", x, y, z);
Or to put them in a string:
ostringstream convert; // stream used for the conversion
convert << x << y << z;
std::string result = convert.str();
You can use std::to_string to convert the int to string, then the + operator acts as a concatenation operation.
#include <string>
std::string concatenated = std::to_string(x) + std::to_string(y) + std::to_string(z);
std::cout << concatenated;
Sounds like you may want to convert the characters to strings, perform the string addition, and then convert back to numbers.
This link should help:
[http://www.cplusplus.com/forum/articles/9645/]
You have plenty of ways to get your result :
numerically : number = x * 100 + y * 10 + z
string concatenation as shown by Cyber
directly using characters :
char resul[4];
resul[0] = '0' + x;
resul[1] = '0' + y;
resul[2] = '0' + z;
resul[3] = '\0';
and probably many others ...
Related
In c++ this code below shows an error:
expression must have integral or unscoped enum type
illegal left operand has type 'double'
is it possible to correct it without the need to use fmod?
# include <iostream>
using namespace std;
int main()
{
int x = 5, y = 6, z = 4;
float w = 3.5, c;
c = (y + w - 0.5) % x * y; // here is the error
cout << "c = " << c << endl;
return 0;
}
You can use type casting to fix it :
c = ((int) (y + w - 0.5)) % x * y;
To clarify your response in the comments, changing c to type int still don't work as the part (y + w - 0.5) is not evaluated as int but as double. And modulus operation doesn't take that type as an argument.
Full modified code :
#include <iostream>
using namespace std;
int main()
{
int x = 5, y = 6, z = 4;
float w = 3.5, c; //c could still stayed as float
c = ((int) (y + w - 0.5)) % x * y; //swapped out here
cout << "c = " << c << endl;
}
Output : c = 24.
To be clear here, this is only a temporary fix for this case, when you know (y + w - 0.5) is going to have a clear integer value. If the value is something like 0.5 or 1.447, std::fmod is desirable.
Here's a post on type conversion rules in an expression regarding interaction between float/double and int/long long : Implicit type conversion rules in C++ operators
void Map::LoadMap(std::string path, int sizeX, int sizeY) {
char c;
std::fstream mapFile;
mapFile.open(path);
int srcX, srcY;
for(int y = 0; y < sizeY; y++) {
for(int x = 0; x < sizeX; x++) {
mapFile.get(c);
srcY = atoi(&c) * 32;
mapFile.get(c);
srcX = atoi(&c) * 32;
ks::Game::AddTile(srcY, srcX, x * 32, y * 32);
std::cout << "X: " << srcX << " Y:" << srcY << std::endl;
mapFile.ignore();
}
}
mapFile.close();
}
I wont Post my whole map file but the layout is like so
00,01,02,03,44,00,00,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44
I am just curious my system is a Mac, I did a similar program on windows and it's read this code character by character however on XCode it is reading the file whole number by whole number and not character by character, Instead of it grabbing the first digit example 4 and multiplying it be 32 it is grabbing 44 then multiplying it by 32 instead.
I simply just want the First digit to be used as a Y coord and the second to be used as a X coord and the "," is skipped.
Apologise in advance if there is something I am overlooking however two minds are better then one any answer would be very much appreciated.
Your code invokes Undefined Behaviour as atoi expects a null-terminated string, which &c is clearly not, it's a pointer to a single character.
Why do you need atoi in the first place? It's a function from C for converting strings like atoi("532532").
If you want to convert a single char to int you can just do it like this.
const int number = c - '0';
I have a task from my teacher,like :
x^2 + y^3 = z
x filled only with odd
y filled only with even
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int x,y,z;
int main(){
for (x=1;x<=20;x++){
if ((x%2==1)&&(y%2==0)){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
}
I try to make my own code like above ,but the only one loop is Y , x stand still with 1.
I want to make x to be looping too. What should i do?
My output expectation would be like :
1^2 + 2^3 = 9
3^2 + 4^3 = 71
5^2 + 6^3 = 241
7^2 + 8^3 = 561
9^2 + 10^3 = 1081
11^2 + 12^3 = 1849
13^2 + 14^3 = 2913
15^2 + 16^3 = 4321
17^2 + 18^3 = 6121
19^2 + 20^3 = 8361
PS. Im sorry with my bad english :D
This is what you have:
int main(){
for (x=1;x<=20;x++){
if ((x%2==1)&&(y%2==0)){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
}
The problem is the first if ((x%2==1)&&(y%2==0)){ check.
After the inner for loop is completed, the value of y will be 21. Hence, the above conditional evaluates to false no matter that the value of x is. As a consequence, the inner for loop is executed only once. You need to remove that first if statement.
int main(){
for (x=1;x<=20;x++){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
Update, in response to OP's comment
Looks like you need much simpler code.
int main(){
// Start with x = 1 and increment x by 2. It will be always be odd
for ( x = 1; x <= 20; x += 2 ){
// No need to create another loop. y is simply x+1
// Since x is odd, y will be even.
y = x+1;
// Compute the result and print it.
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" << z <<"\n";
}
}
Because y = 21 after the inside y loop.So the x loop will not be executed after . Hope it helpful.
Would this kind of variable assignment work?
double a = 2.0,
x, y, z = 0.5;
Would the second line of code work properly and initialize x, y, z each to 0.5?
Your code leaves x and y uninitialized. However, a slight rearrangement can save you from repeating an initial value:
double a = 2.0, x = 0.50, y = x, z = x;
Variables that are declared earlier in a declaration are in scope of later declarations.
This is sometimes particularly useful when evaluating one initializer may have a non-trivial runtime cost. For example, in the following nested loop where m is a multimap:
for (auto it = m.begin(), kt = it, e = m.end(); it != e; it = kt)
{ // ^^^^^^^^^^^^^^^^^^^^^^^
// handle partition
for (; kt != e && kt->first == it->first; ++kt)
{
// ... handle equal-range
}
}
No, only z would be initialized .You have to write it like this:
double x = 0.50, y = x, z = x;
But you can write an assignment like this:
double x, y, z;
x = y = z = 0.50;
Simply No. Only z will be initialized.
If you try to print them afterwards
std::cout << a << " " << x << " " << y << " " << z;
you will get this kind of warning from the compiler:
warning: 'x' is used uninitialized in this function
For the sake of clarity I would use the second option that Rabbid76 suggested:
double x, y, z;
x = y = z = 0.50;
The second line of code:
double x;
x = 50.1;
actually has a return value. Usually, it is not caught, or used, but it is there. So x = 50.1 returns the value 50.1.
This implies that you could do:
double x,y,z;
x = y = z = 50.1;
And the value will make its way up the chain where x = y returns and the value isn't caught again. After that line is executed, x, y and z will all have the value 50.1.
If you want to assign to all three variables, write
x = y = z = 0.5;
which is equivalent to
x = (y = (z = 0.5));
The code that you present only assigns to z.
You can achieve what you want doing this:
x = y = z = 0.50;
You could do that in a single line like this:
double x = 0.5, y = 0.5, z = 0.5;
Whenever I try to compile I get
24 [Warning] converting to int from float
83 [Warning] converting to int from float
int a , b = 8;
float c = 5.2;
float d = 8E3;
a = static_cast<float>(b) * c; // 24
cout << a << "\n";
cout << d << "\n";
int x, y, answer;
x = 7;
y = 9;
answer = 5;
answer *= (x + y);
cout << answer << "\n";
answer *= x + y;
cout << answer << "\n";
float m = 33.97;
answer += (x + y + m); // 83
cout << answer << "\n";
Any suggestions as to what I'm doing wrong?
a = static_cast<float>(b) * c;
a is an int, and the right-hand side of the equation is the multiplication of two floats, which will result in an intermediate float value, which is then implicitly casted to an int, causing the warning you are seeing.
Also:
answer += (x + y + m);
answer is an int type, and so are x and y, but m is float, again causing the intermediate result of the right-hand side to be a float.
These conversions will cause truncation of the fractional values of the float results. You can get rid of the warnings by explicitly casting to an int:
a = static_cast<int>(static_cast<float>(b) * c);
answer += static_cast<int>(x + y + m);
Well you're just getting a warning since the compiler is changing a floating-point value to an integer, thus truncating the result.
int a;
float f = 3.2;
a = f; // a is 3, a trunctated 3.2
Your question seems to be about conversion from float to int, not from int to float.
Basically you are doing nothing wrong. It is only a warning, as the value will be truncated (and maybe you don't expect that). To tell the compiler that you really want to get an int out of a float, you can make the cast explicit, like this:
a = static_cast<int>(static_cast<float>(b) * c);
Then it will not warn you anymore.