Errors with ')' and ';'

Understanding the language, error messages, etc.

Errors with ')' and ';'

Postby Xenospark » Tue Dec 20, 2016 8:28 pm

Hiya! I'm quite new to this language, so my question may have a very simple answer, but I'm having trouble finding such an answer online. Is it possible that I could print my code here and have someone tell me what's up? I'm trying to make a very simple program (not really a game), and it really consists of about two controls and three values. However, when I try to print the integer values, it is always telling me it expects a ')' or a ';' at random spots in the line. Thanks for help ahead of time, and here is the code :P

Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int chain = 1;
int chance = 0;
int fchain = 1;
void setup() {
  // put your setup code here, to run once:
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Shiny Probability"));
  //random values
  pinMode(chain, OUTPUT);
  if (chain <= 69)
    chance = chain / 4096;

  if (chain >= 70)
    chance = chain / 1024;

  if (chain >= 256)
    chain = 1;


}

void loop() {
  // put your main code here, to run repeatedly:
  if (gb.update()) {
    //prints Chain length on the screen
    gb.display.println(F("Chain Length:")Serial.print(fchain))
    gb.display.println(F("Chance of Shiny:")Serial.print(chance))
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain + 1
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain - 1
    }
  }
}
Xenospark
 
Posts: 16
Joined: Tue Dec 20, 2016 8:11 pm

Re: Errors with ')' and ';'

Postby Sorunome » Tue Dec 20, 2016 8:48 pm

There are a few issues with your code:
First of all, this isn't really an issue but more like bad habit which can lead to seemingly random errors:
Code: Select all
  if (chain <= 69)
    chance = chain / 4096;

  if (chain >= 70)
    chance = chain / 1024;

  if (chain >= 256)
    chain = 1;

In this block, while this would work as it are all one-line if conditions, it is still recommended to add the curly brackets as else, when adding another line and you forget adding the curly brackets, the code does unexpected things.
So it would look like this:
Code: Select all
  if (chain <= 69) {
    chance = chain / 4096;
  }
 
  if (chain >= 70) {
    chance = chain / 1024;
  }
 
  if (chain >= 256){
    chain = 1;
  }


Second of all, you defined your variable 'chance' as an integer (Keyword int), however due to the context it seems like you actually want a floating point number (Keyword float), as it seems like you want to store the probability between 0 and 1 in it. An integer is only a whole number, so like 0,1,2,3 etc,
So instead of
Code: Select all
int chance = 0;

you'd do
Code: Select all
float chance = 0;

There is another thing to take into consideration here: You are deviding integers with one another, meaning that you are performing integer devision. So like 2/4 will actually return 0 instead of 0.5 as those are both integers, and thus the result is floored (everything after the comma is simply cut off).
To overgo this you'd replace lines such as
Code: Select all
    chance = chain / 4096;

with
Code: Select all
    chance = chain / 4096.0;
Here I force one of the arguments to be a floating point number, due to the value being 4096.0 rather than 4096, and thus floating point devision is performed.


Now let's get to your syntax error (at least from what I could spot, if it still doesn't compile please copypaste us the error output :3 )
Code: Select all
    gb.display.println(F("Chain Length:")Serial.print(fchain))
    gb.display.println(F("Chance of Shiny:")Serial.print(chance))

I am not sure what you were trying to do here, were you trying to gb.display.println or were you trying to do Serial.print ?
If you were trying to do gb.display.println:
Code: Select all
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance);
If you wanted to do Serial.print then substitude accordingly.

Then there is also this block:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain + 1
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain - 1
    }

It simply lacks semi-columns:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain + 1;
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain - 1;
    }

There are also shorthands for increasing and decreasing numbers:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain++;
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain--;
    }




Whew, this post got long, I hope I could help!
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Errors with ')' and ';'

Postby Xenospark » Tue Dec 20, 2016 9:10 pm

Sorunome wrote:There are a few issues with your code:
First of all, this isn't really an issue but more like bad habit which can lead to seemingly random errors:
Code: Select all
  if (chain <= 69)
    chance = chain / 4096;

  if (chain >= 70)
    chance = chain / 1024;

  if (chain >= 256)
    chain = 1;

In this block, while this would work as it are all one-line if conditions, it is still recommended to add the curly brackets as else, when adding another line and you forget adding the curly brackets, the code does unexpected things.
So it would look like this:
Code: Select all
  if (chain <= 69) {
    chance = chain / 4096;
  }
 
  if (chain >= 70) {
    chance = chain / 1024;
  }
 
  if (chain >= 256){
    chain = 1;
  }


Second of all, you defined your variable 'chance' as an integer (Keyword int), however due to the context it seems like you actually want a floating point number (Keyword float), as it seems like you want to store the probability between 0 and 1 in it. An integer is only a whole number, so like 0,1,2,3 etc,
So instead of
Code: Select all
int chance = 0;

you'd do
Code: Select all
float chance = 0;

There is another thing to take into consideration here: You are deviding integers with one another, meaning that you are performing integer devision. So like 2/4 will actually return 0 instead of 0.5 as those are both integers, and thus the result is floored (everything after the comma is simply cut off).
To overgo this you'd replace lines such as
Code: Select all
    chance = chain / 4096;

with
Code: Select all
    chance = chain / 4096.0;
Here I force one of the arguments to be a floating point number, due to the value being 4096.0 rather than 4096, and thus floating point devision is performed.


Now let's get to your syntax error (at least from what I could spot, if it still doesn't compile please copypaste us the error output :3 )
Code: Select all
    gb.display.println(F("Chain Length:")Serial.print(fchain))
    gb.display.println(F("Chance of Shiny:")Serial.print(chance))

I am not sure what you were trying to do here, were you trying to gb.display.println or were you trying to do Serial.print ?
If you were trying to do gb.display.println:
Code: Select all
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance);
If you wanted to do Serial.print then substitude accordingly.

Then there is also this block:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain + 1
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain - 1
    }

It simply lacks semi-columns:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain + 1;
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain - 1;
    }

There are also shorthands for increasing and decreasing numbers:
Code: Select all
    if (gb.buttons.pressed(BTN_A)) {
      chain++;
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain--;
    }




Whew, this post got long, I hope I could help!

WOW! Thank you that was such a fast response, and it must have taken a lot of effort. It verifies fine now, but I'm still having some issues. One thing is, when I load it up, pressing A and B does nothing to the values of "chain". Also, This is supposed to be an odds calculator, and the odds are pretty darn low. So low that the probability showed up as 0.00 and then stopped. Is there a way I can either convert it to percentage or let it go to at least the 6 digits past the decimal? Thanks again, and sorry for not including which line had the problem earlier >.<
Xenospark
 
Posts: 16
Joined: Tue Dec 20, 2016 8:11 pm

Re: Errors with ')' and ';'

Postby Sorunome » Tue Dec 20, 2016 10:29 pm

Xenospark wrote:WOW! Thank you that was such a fast response, and it must have taken a lot of effort. It verifies fine now, but I'm still having some issues. One thing is, when I load it up, pressing A and B does nothing to the values of "chain".

My bad, in your code you were trying to output the value of the variable fchain rather than of chain, I can't see fchain being used elsewhere so you might as well remove that value I guess.
Also maybe the chance-recalculation should happen after changing the chain-variable?
Also, This is supposed to be an odds calculator, and the odds are pretty darn low. So low that the probability showed up as 0.00 and then stopped. Is there a way I can either convert it to percentage or let it go to at least the 6 digits past the decimal? Thanks again, and sorry for not including which line had the problem earlier >.<
Percentages are simply floating point numbers multiplied by 100.
I'm sorry, I don't know out of the top of my head how to output more digits with gb.display.print, however Serial.print (which sends it via the usb thingy to the serial monitor in your arduino IDE) displays more, I think
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Errors with ')' and ';'

Postby Xenospark » Tue Dec 20, 2016 10:53 pm

Sorunome wrote:
Xenospark wrote:WOW! Thank you that was such a fast response, and it must have taken a lot of effort. It verifies fine now, but I'm still having some issues. One thing is, when I load it up, pressing A and B does nothing to the values of "chain".

My bad, in your code you were trying to output the value of the variable fchain rather than of chain, I can't see fchain being used elsewhere so you might as well remove that value I guess.
Also maybe the chance-recalculation should happen after changing the chain-variable?
Also, This is supposed to be an odds calculator, and the odds are pretty darn low. So low that the probability showed up as 0.00 and then stopped. Is there a way I can either convert it to percentage or let it go to at least the 6 digits past the decimal? Thanks again, and sorry for not including which line had the problem earlier >.<
Percentages are simply floating point numbers multiplied by 100.
I'm sorry, I don't know out of the top of my head how to output more digits with gb.display.print, however Serial.print (which sends it via the usb thingy to the serial monitor in your arduino IDE) displays more, I think

So do you think changing it all out for Serial.print would let me multiply it by 100 and then display a bit farther? And I messed up earlier, it wasn't your bad. I used the wrong variable. F-chain is supposed to be the entire number without reverting back to 255, whereas chain reverts back after it exceeds 255 and is used to calculate odds. Thanks so much!
Xenospark
 
Posts: 16
Joined: Tue Dec 20, 2016 8:11 pm

Re: Errors with ')' and ';'

Postby Sorunome » Tue Dec 20, 2016 10:57 pm

Xenospark wrote:So do you think changing it all out for Serial.print would let me multiply it by 100 and then display a bit farther?[...]

You could just do like gb.display.println(chance*100);
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Errors with ')' and ';'

Postby Xenospark » Wed Dec 21, 2016 12:06 am

Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
  // put your setup code here, to run once:
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Shiny Probability"));
  //random values
  pinMode(chain, OUTPUT);
  if (chain <= 69) {
    chance = chain / 4096.0;
  }
  if (chain >= 70) {
    chance = chain / 1024.0;
  }
  if (chain >= 256) {
    chain = 1;
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  if (gb.update()) {
    //prints Chain length on the screen
   
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain ++;
      fchain = fchain ++;
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain --;
      fchain = fchain --;
    }
    gb.display.print(F("Chain Length:"));
    gb.display.println(fchain);
    gb.display.print(F("Chance of Shiny:"));
    gb.display.println(chance*100);
  }
}

Here is my code now. even at 100 times it just shows .02 no matter how many times I press A. Also, the chain display does not change from 1. I'm determined to get this to work but it's a little frustrating haha (such is coding I guess :P). If I do Serial.print (chain)(DEC6) would that let it go past two decimals?
Xenospark
 
Posts: 16
Joined: Tue Dec 20, 2016 8:11 pm

Re: Errors with ')' and ';'

Postby Sorunome » Wed Dec 21, 2016 6:25 am

Xenospark wrote:Here is my code now. even at 100 times it just shows .02 no matter how many times I press A. Also, the chain display does not change from 1.

That is because you have to re-calculate chance every time you change chain. Try this:
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
  // put your setup code here, to run once:
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Shiny Probability"));
  //random values
  pinMode(chain, OUTPUT);
}

void reCalcChance() {
  if (chain <= 69) {
    chance = chain / 4096.0;
  }
  if (chain >= 70) {
    chance = chain / 1024.0;
  }
  if (chain >= 256) {
    chain = 1;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (gb.update()) {
    //prints Chain length on the screen
   
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain ++;
      fchain = fchain ++;
      reCalcChance();
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain --;
      fchain = fchain --;
      reCalcChance();
    }

    gb.display.print(F("Chain Length:"));
    gb.display.println(fchain);
    gb.display.print(F("Chance of Shiny:"));
    gb.display.println(chance*100);
  }
}

Also what's the point of the fchain variable? Why have two variables for the same thing?
I'm determined to get this to work but it's a little frustrating haha (such is coding I guess :P).
Hang in there!
If I do Serial.print (chain)(DEC6) would that let it go past two decimals?
Sorry, I don't know what you mean with this question :/
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Errors with ')' and ';'

Postby Xenospark » Wed Dec 21, 2016 3:12 pm

Sorunome wrote:
Xenospark wrote:Here is my code now. even at 100 times it just shows .02 no matter how many times I press A. Also, the chain display does not change from 1.

That is because you have to re-calculate chance every time you change chain. Try this:
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
  // put your setup code here, to run once:
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Shiny Probability"));
  //random values
  pinMode(chain, OUTPUT);
}

void reCalcChance() {
  if (chain <= 69) {
    chance = chain / 4096.0;
  }
  if (chain >= 70) {
    chance = chain / 1024.0;
  }
  if (chain >= 256) {
    chain = 1;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (gb.update()) {
    //prints Chain length on the screen
   
    if (gb.buttons.pressed(BTN_A)) {
      chain = chain ++;
      fchain = fchain ++;
      reCalcChance();
    }
    if (gb.buttons.pressed(BTN_B)) {
      chain = chain --;
      fchain = fchain --;
      reCalcChance();
    }

    gb.display.print(F("Chain Length:"));
    gb.display.println(fchain);
    gb.display.print(F("Chance of Shiny:"));
    gb.display.println(chance*100);
  }
}

Also what's the point of the fchain variable? Why have two variables for the same thing?
I'm determined to get this to work but it's a little frustrating haha (such is coding I guess :P).
Hang in there!
If I do Serial.print (chain)(DEC6) would that let it go past two decimals?
Sorry, I don't know what you mean with this question :/

Alright, I have yet to test it, but I'm pretty sure it will work. I'm understanding a lot of this language now thanks to you! For the fchain question, I'll try and answer by explaining what I'm trying to make. What I am trying to make is a program that will give me the probability of getting a certain, um, "Drop item" I guess. The way the game works is that the rate of the drop event quadruples after you reach a chain of seventy. However, the game uses an 8-bit counter to count the chain length, so after 255, the rate goes back to 4096. Fchain is to display the full chain that I have reached, and chain is only used to calculate the odds (Example: I have an fchain of 278, but a chain of 28 for calculating, simply because it resets after 255. However, my Full Chain is still past 255, and is really purely for cosmetics). I hope that explains it pretty well. I also realized that I may need to adjust it to get the correct odds, as once it goes past 70 and the odds quadruple, I haven't rolled those odds 70 times, and it might be calculated like that. For the decimal thing, I was wondering if using Serial.print would let me get past 0.02 and make it look more like 0.02234 or whatever the odds may be. I used the wrong code above however, in fact I'm not even sure that was a command.
Code: Select all
Serial.print(F("Chain Length:"));
    Serial.print(fchain);
    Serial.print(F("Chance of Shiny:"));
    Serial.print(chance*100, 5);

The code above is an example of trying to get the chance*100 to display 5 digits after the decimal. Thank you for all of your help so far, you are making my programming experience mush more fun ^_^

*EDIT* So I did end up testing it. Even with reCalcChance() it still only changes to 0.02 and the chain does not go up. Do I need to make a reCheckFchain() or something to fix the chain? And for the percentage not going up I'm just lost >.< sorry if my questions are stupid.
Xenospark
 
Posts: 16
Joined: Tue Dec 20, 2016 8:11 pm

Re: Errors with ')' and ';'

Postby Sorunome » Wed Dec 28, 2016 6:16 pm

I'm sorry it took so long to reply. I'm a bit confused right now as to what you are mean, would you mind posting your entire code that you are running currently?
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 20 guests

cron