I'm a beginner to C++ and I'm having a problem with this code, which is supposed to display the scores during the Superbowl final:
#include <iostream>
enum POINTS { EXTRA_POINT = 1, SAFETY = 2, FIELD_GOAL = 3, TOUCHDOWN =6 };
unsigned short giantsScore = 0, patriotsScore = 0;
int main()
{
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + SAFETY << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + TOUCHDOWN + EXTRA_POINT << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + TOUCHDOWN + EXTRA_POINT << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + TOUCHDOWN + EXTRA_POINT << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + FIELD_GOAL << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + FIELD_GOAL << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT << "\n\n";
return 0;
}
Ignoring that this is quite inelegant, when I run this through the compiler, G++, I get the error message
error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'
If I remove the constants and add them in before each std::cout, then it runs fine. I just wanted to know why I can't add the constants during each output line?
Your error message states: int << char, which of course is an odd operation.
It is because of operator priorities.
Each operator has a priority meaning it will evaluate before or after the other operators are evaluated.
+ evaluates before =
and << should be evaluated after = had cout<<"stuff" been its original purpose.
<< is originally the bit-shift operator (still is), so that is why you are experiencing this odd behaviour. Add parentheses and you'll be good.
Check http://cs.smu.ca/~porter/csc/ref/cpp_operators.html for an overview of the priority rules of operators. When you write this:
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT << "\n\n";
Then according to the priority rules, the + operator will be executed first, giving you this:
std::cout << " Patriots: " << patriotsScore = result << "\n\n";
Then the << operator is executed, which means also `result << "\n\n". But this operator is not defined between int and char[2].
To solve your problem, put parenthesis around the assignment operation, like this:
std::cout << " Patriots: " << (patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT) << "\n\n";
Related
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
I try to make uncrustify to go from:
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
to :
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
But so far I failed miserably! Is this even possible, and if so, can I get any hints?
No, there is no such angle align option in Uncrustify as of now.
Only
# Whether to align lines that start with '<<' with previous '<<'.
#
# Default: true
align_left_shift = false # true/false
which for some reason is defaulted be always used
I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);
Well basically the condition for healthtwo causes the program to stop but not healthone for some reason
complete code: http://en.textsave.org/CmN
if (chance<=rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo=healthtwo-attackp;
}
else {
cout << " " << endl;
cout << "Miss!" << endl;
}
chance=1+rand()%23;
if (chance<=rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone=healthone-attackcp;
}
else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
} while (healthone>=0 || healthtwo>=0);
That should be a logical and (&&). After all, you want to check whether the health of both contestants is greater or equal to zero.
The user inputs a double and string which gets stored into two arrays. Like so :
{
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
Then the double and string are passed to this :
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
last10charges[i] = last10charges[i-1];
}
last10withdraws[0] = amount;
last10charges[0] = name;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :
{
cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
cout << " " << last10withdraws[0] << " " << last10deposits[0] << " " << last10charges[0] << endl;
cout << " " << last10withdraws[1] << " " << last10deposits[1] << " " << last10charges[1] << endl;
cout << " " << last10withdraws[2] << " " << last10deposits[2] << " " << last10charges[2] << endl;
cout << " " << last10withdraws[3] << " " << last10deposits[3] << " " << last10charges[3] << endl;
cout << " " << last10withdraws[4] << " " << last10deposits[4] << " " << last10charges[4] << endl;
cout << " " << last10withdraws[5] << " " << last10deposits[5] << " " << last10charges[5] << endl;
cout << " " << last10withdraws[6] << " " << last10deposits[6] << " " << last10charges[6] << endl;
cout << " " << last10withdraws[7] << " " << last10deposits[7] << " " << last10charges[7] << endl;
cout << " " << last10withdraws[8] << " " << last10deposits[8] << " " << last10charges[8] << endl;
cout << " " << last10withdraws[9] << " " << last10deposits[9] << " " << last10charges[9] << endl;
cout << endl;
}
Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :
60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?
Initialize the array as:
int last10withdraws[10] = {0};
Now all elements are zero.
If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.
If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
Hope that helps.
You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.