Reputation: 9
I want to take three user input values A, B, C and then create a new integer that stores these three values called capacity. I want the integers to be seperated by a comma but when I output the integer capacity it returns only as the value of C. I don't know how to fix this issue.
#include <iostream>
using namespace std;
int main()
{
int A;
cout <<"Enter Capacity of Jug A"<< endl;
cin >>A;
int B;
cout <<"Enter Capacity of Jug B"<< endl;
cin >>B;
int C;
cout <<"Enter Capacity of Jug C"<< endl;
cin >>C;
int capacity;
capacity = (A, B, C);
cout <<"capacity is "<< capacity;
return 0;
}
Upvotes: 0
Views: 92
Reputation: 11
You can simply write this code, as:
#include <iostream>
using namespace std;
int main()
{
int A;
cout << "Enter Capacity of Jug A" << endl;
cin >> A;
int B;
cout << "Enter Capacity of Jug B" << endl;
cin >> B;
int C;
cout << "Enter Capacity of Jug C" << endl;
cin >> C;
cout << "capacity is " << A << "," << B << "," << C << endl;
return 0;
}
Upvotes: 0
Reputation: 213
You can use array like this:
#include <iostream>
using namespace std;
int main()
{
int A;
cout << "Enter Capacity of Jug A" << endl;
cin >> A;
int B;
cout << "Enter Capacity of Jug B" << endl;
cin >> B;
int C;
cout << "Enter Capacity of Jug C" << endl;
cin >> C;
int capacity[3];
capacity[0] = A;
capacity[1] = B;
capacity[2] = C;
cout << "capacity is " << capacity[0] << "," << capacity[1] << "," << capacity[2];
return 0;
}
Upvotes: 3
Reputation: 604
What's wrong?
You simply cannot store three int
values in one int
variable. This doesn't make sense. You need other data types to store those values which I explain later.
What's happening?
When you separate values by ,
in a right-hand side expression, the compiler sees them as multiple expressions (expr1, expr2, etc.) and evaluates them in order, but returns the last result (that is the value of C
in your code). So, there's no surprise that capacity
always equals C
.
What is the right solution?
There are various ways to solve this problem.
Use struct
You can define an struct and store the values as its fields:
struct caps {
int A, B, C;
};
caps c = { A, B, C };
Use std::array
(since C++11)
std::array<int, 3> c = { A, B, C };
Use std::tuple
(since C++11)
std::tuple<int, int, int> c = std::make_tuple(A, B, C);
Upvotes: 0
Reputation: 8987
I want the integers to be seperated by a comma but when I output the integer capacity it returns only as the value of C.
It is unrealistic to have that with int capacity;
. int
can only hold numeric, integral values (within a certain range).
You've titled your question with Concatenation issue which makes me believe you're looking for an std::string
to hold a string value. Then you can separate your integers by commas with string literals.
#include <iostream>
#include <string> // std::string
int main()
{
// ... omitted for brevity
// use std::string, and CONCATENATE using the + operator
std::string capacity = "(" + std::to_string(A) + ", " + std::to_string(B) + ", " + std::to_string(C) + ")";
// ... do something with `capacity` ?
std::cout << "capacity is " << capacity << '\n';
return 0;
}
Man, that's a lot. Unless you're planning on using the string for other purposes, you should output your values directly using std::cout
(as in Bolov's answer) or use an std::stringstream
if you like.
#include <iostream>
int main()
{
// ... omitted for brevity
std::cout << "capacity is (" << A << ", " << B << ", " << C << ")\n";
return 0;
}
Upvotes: 0
Reputation: 96
one solution to your problem would be creating an array of int
values. On each position of the array an int
value is stored.
int capacity[3];
cin >> capacity[0];
cin >> capacity[1];
cin >> capacity[2];
cout << capacity[0] << ", " << capacity[1] << ", " << capacity[2] << endl;
However, a better way to do this is using a loop. for
, for example:
for (int i = 0; i < 3; ++ i)
{
cout <<"Enter Capacity of Jug "<< i << endl;
cin >> capacity[i];
}
And the same way for printing.
More info on arrays: https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
More info on the for
loop: https://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm
Upvotes: 0