I'm taking a C++ class, I'm a C# kind of person, and have some trouble with C++ due to C# methods being engraved in my mind. I think I'm unable to solve this due to stress today due to taking SAT's today and got an assignment that I messed a whole entire period trying to figure out how to fix and I am not willing to waste time on this trying to figure it out while I have work for another class that is more important. I normally would go on Stack Overflow, but they do not like me whatsoever (from previous questions) that was involved with C# when I was still learning.
The Problem:
Continue to work with this program until you get the correct result.
The correct result should be 0.292162. Do not change the data type of the two named constants. Instead, use a typecast to solve the problem.
What I've Tried: I've tried Static Casts and Type Conversion but it was saying syntax was wrong in the 1st column, which I was confused by. I would really appreciate it if you could help me with this. I would ask my teacher but he doesn't have a *cough brain *cough when it comes to programming. He's just as dumbfounded as me.
// This program will determine the batting average of a player.
// The number of hits and at bats are set internally in the program.
// Name Here
#include <iostream>
usingnamespace std;
constint AT_BAT = 421;
constint HITS = 123;
int main()
{
float batAvg = AT_BAT / HITS; //an assignment statement
cout << "The batting average is " << batAvg << endl; //output the result
return 0;
}
I don't see any casts in that code. You're also calculating the batting average incorrectly. It should be hits / at bats. Cast one of those to float (to avoid integer division) and divide. float batAvg = HITS / (float)AT_BAT;
It worked, thanks. As for not seeing the casting I didn't include my attempts with casting in the code, just because I only wanted to include what was given to me at the start. To prevent any confusion
As for what you provided, we were shown 2 method's to do this. One of them was static cast the other was in a C type format that my teacher told us not to use often. Yours wasn't brought up at all, honestly I'm not surprised.
I only asked on here because I wasn't sure whether data casting with C# was the same as C++, I usually only do C# for game programming and don't mess with datatypes like they do in this course, or like a software engineer would. I'm more used to advanced programming that have to do with networking and sockets.
I appreciate the help If I have any farther problems that I'm unable to solve, I'll definitely ask again on here.