Issue with Float command not working (1st Week of Coding) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 months ago.
Improve this question
So I am trying to learn C++ and apart of taking basic courses online, I also try to create programs and put my spin on them to learn. I have a very old tutorial that was teaching how to create a battle simulator - I keep getting an error that I can't figure out. The code I have is here:
default_random_engine RandomEngine(time(nullptr));
uniform_real_distribution<float> attack(0.0f, 1.0f);
// Human Properties
float HumanAttack = 0.6f;
float HumanHealth = 250.0f;
float HumanDamage = 200.0f;
float CurrentHumanHealth = HumanHealth
// Skeleton properties
float SkeletonAttack = 0.4f;
float SkeletonHealth = 150.0f;
float SkeletonDamage = 55.0f;
float CurrentSkeletonHealth = SkeletonHealth;
float AttackResult;
int numskeletons;
int numhumans;
I keep getting an error ' Expected ',' or ';' before 'float'
I have been looking things up but as I said - I'm in the first week. Maybe this is to advanced, but I am trying to play with code to parse it out. I appreciate any help.

You have to add semicolon at the end of float CurrentHumanHealth = HumanHealth;
// Human Properties
float HumanAttack = 0.6f;
float HumanHealth = 250.0f;
float HumanDamage = 200.0f;
float CurrentHumanHealth = HumanHealth;
// Skeleton properties
float SkeletonAttack = 0.4f;
float SkeletonHealth = 150.0f;
float SkeletonDamage = 55.0f;
float CurrentSkeletonHealth = SkeletonHealth;
float AttackResult;
int numskeletons;
int numhumans;

Related

for loop counter doesn't increment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
i'm having some issues with this bit of code. basically "k" doesn't increment more than 1. I've already tried to declare it outside the loop but doesn't fix it. basically what the code does is generating a grid of crystals.
this is a uni assessment and at the moment I'am a newbie with console functions, especially managing the cursor. as you can see, at every iteration i add +2 to pos.x. it seems to work, but when it starts again, pos.x returns to the start value and instead is pos.y to increment(?).
void gridGeneration(Crystal simbols[][Columns])
{
COORD pos = {10, 55};
for (int i = 0; i < Rows; i++)
for (int k = 0; k < Columns; k++)
{
WriteCrystalAt(simbols[i][k].crystal, pos.X, pos.Y, simbols[i][k].color= rand() % light_yellow + light_blue);
pos.X += 2;
if (k = 1)
{
pos.X = 10;
pos.Y += 2;
}
}
}
It would increment further than 1, but you keep setting it to 1 again:
if (k = 1)
You should use == for comparisons.
Your compiler should have issued a warning about this. If it did not, review your warning settings. If it did, stop ignoring warnings.

Error when using constant in defined function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I'm not a novice, but I may be making a novice mistake here. I'm writing code and I have declared a constant at the top of my program. Whenever I try to use that constant in one of my defined functions, I get an error.
#include <iostream>;
#include <fstream>;
#include <string>;
#include <cmath>;
#define PI 3.14159265358979323846;
#define RADI 300.0;
void CreatePieChart(unsigned char pixels[][WID][DEP], const int dims[3],
double percentages[7], double radius)
{
double radians, distance, deg;
for (int i = 0; i < HITE; i++) {
for (int j = 0; j < WID; j++) {
radians = get_theta(j, i, center);
distance = get_distance_from_center(j, i, center, radians);
deg = quadrant_converter(j, i, center, radians);
if ( RADI < distance ) {
pixels[i][j][0] = 0;
}
}
}
}
When I try to access RADI I get an error.
Syntax Error: ')'
syntax error: missing ';' before '{'
'<' result of expression not used
language feature 'init-statements in if/switch' requires compiler flag '/std:c++17'
All on the same line
Please help.
A #define literally replaces the thing on the left, with the thing on the right.
So when you write
#define RADI 300.0;
if ( RADI < distance )
that is the same as writing
if ( 300.0; < distance )
which has an extra ; in the middle of it. Delete the ;.
I needed to delete the semi-colons after the headers and define declarations

' variable' was not declared in this scope [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I'm new to Arduino coding and I'm not sure what issue I'm having with displaying the variable as serialPrint. Here is part of my code:
void loop()
{
int force1raw = analogRead(FSR_PIN1);
int force2raw = analogRead(FSR_PIN2);
int force3raw = analogRead(FSR_PIN3);
float force1 = force1raw;
float force2 = force2raw;
float force3 = force3raw;
for (int i = 0; i < 1000; i++)
{
// Use ADC reading to calculate voltage:
float f1v = force1 * VCC / 1023.0;
// Use voltage and static resistor value to
// calculate FSR resistance:
float f1r = R_DIV * (VCC / f1v - 1.0);
}
Serial.println("Resistance 1: " + String(f1r) + " ohms");
// Estimate force based on slopes in figure 3 of
// FSR datasheet:
float force;
float f1g = 1.0 / f1r; // Calculate conductance
// Break parabolic curve down into two linear slopes:
if (f1g <= 600)
force1f = (f1g - 0.00075) / 0.00000032639;
else
force1f = f1g / 0.000000642857;
Serial.println("Force 1: " + String(force2f) + " g");
Serial.println();
delay(100);
Well,
Serial.println("Resistance 1: " + String(f1r) + " ohms");
it uses the variable f1r whose scope just ended.
In C++, when a variable is defined inside a scope (between some { and }), it does not exist outside of it.

Loop to save vertices in openGL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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 have a grid with tetragons and i want to save all the vertices in an array. I wrote ths code:
int counter=0;
int i = 0;
for(i=0; i<=600; i+=40){
verticePosition[counter] = i;
verticePosition[counter+1] = i;
verticePosition[counter+2] = i+40;
verticePosition[counter+3] = i;
verticePosition[counter+4] = i;
verticePosition[counter+5] = i+40;
verticePosition[counter+6] = i+40;
verticePosition[counter+7] = i+40;
counter += 8;
}
I want to save four-four vertices in the table and then i call a function to fill every tetragon with a different color but im getting an error in this for loop:
prog.c:13:1: error: expected identifier or ‘(’ before ‘for’
for(xpos=0; xpox<=600; xpos+=40){
^
and also another error:
prog.c:13:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<=’ token
for(xpos=0; xpox<=600; xpos+=40){
^
I cant find what is wrong with my loop.
The variable xpos is used but not declared, you must declare and initialize it:
for (int xpos = 0; xpos <= 600; xpos += 40) {
Or declare it before the loop:
int xpos;
for (xpos = 0; xpos <= 600; xpos += 40) {

Generating random reals uniformly using Boost [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I am trying to generate some uniform real numbers for a Monte Carlo integration but the routine I build was returning some really strange values. Upon closer inspection I notices that Boost was returning some crazy looking random numbers e.g.:
temp = -0.185276
temp = -0.864523
temp = -0.0942081
temp = -0.164991
temp = -0.873013
temp = -0.0311322
temp = -0.0866241
temp = -0.778966
temp = -0.367641
temp = -0.691833
temp = 5.66499e-310
temp = 9.42007e-311
temp = 6.29821e-310
temp = 5.80603e-310
temp = 8.82973e-311
temp = 6.73679e-310
temp = 6.35094e-310
temp = 1.53691e-310
temp = 4.39696e-310
temp = 2.14277e-310
Whilst these numbers are technically still reals generated between the bounds -1 and 1 I would prefer it if they weren't quite so small!
My implementation of the call to boost is in a function which is called multiple times (for different bounding values) as follows:
// Define Boost typedefs
typedef boost::mt19937 Engine;
typedef boost::uniform_real<double> Distribution;
typedef boost::variate_generator <Engine, Distribution> Generator;
int main (void) {
...
Integral = MCRecursion(...);
...
return 0;
}
double MCRecursion (int Count, double Lower, double Upper, double (*Integrand)(double)) {
// Define Boost objects
Engine Eng;
Distribution Dist (Lower, Upper);
Generator RandomGen (Eng, Dist);
Eng.seed(time(0));
// Variables for Monte Carlo sample sums
double Sum = 0.0;
double temp;
for (int i = 0; i < Count; i++) {
temp = RandomGen();
std::cout << " temp = " << temp << std::endl;
Sum += Integrand(temp);
}
return (Upper - Lower) * Sum / Count;
}
I assume the problem is something with my implementation but I can't find any errors. Any and all help appreciated!
Cheers,
Jack
EDIT
Code for calling MCRecursion:
The Code I am writting runs a Monte Carlo on the entire domain I am interested in [Lower, Upper] and then looks again at the left half of the whole domain and the right half of the domain.
e.g. if we were integrating f(x) between -a and a I calculate the full integral using:
double FullDomain = MCRecursion (1e5, LowerBound, UpperBound, f);
double Centre = (Upper + Lower) / 2.0;
double LeftHalf = MCRecursion (1e5, LowerBound, Centre, f);
double RightHalf = MCRecursion (1e5, Centre, UpperBound, f);
and I then look at the uncertainty by calculating:
double difference = fabs(FullDomain - LeftHalf - Righthalf);
to see if more samples is 'worth it' in some sense
Jack
Based on the pastebin the questioner posted in the comments:
This is not a problem with the random library but rather a simple programming error. Compiling the code throws the warning:
../src/Test.cpp: In function ‘double AdaptiveMCRecursion(double, double, double, double, int, double, double (*)(double))’:
../src/Test.cpp:100:72: warning: ‘Right’ is used uninitialized in this function [-Wuninitialized]
double Right = MCSample (Count, Central, Right, Integrand);
So all the behaviour from that line on is basically undefined. Especially it results in calling the function MCSample with an undetermined Upper parameter. So your result is not unexpected. You are actually lucky the program runs at all.