Sinclair Sprockets
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Programming Questions

5 posters

Page 1 of 2 1, 2  Next

Go down

Programming Questions Empty Programming Questions

Post  William Surmak Sun Mar 18, 2012 1:51 am

So far I have been able to follow along with the tutorials pretty well. Although I am a little confused on how exactly a function is created. An answer is appreciated. Very Happy
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Brendan van Ryn Sun Mar 18, 2012 11:32 am

Hey Will. No problem Smile

So, we already use functions all the time. For example,

void OperatorControl(void) // The function that is run in tele-op mode on the robot
void Autonomous(void) // The function that is run in autonomous mode on the robot
int main(void) // The function that is run when we start a DOS program at home

Let's look at main and the at-home code. When you start any program you write at home, the operating system (DOS, which is a subset of Windows) loads your program into memory, and tells the processor to start executing instructions from your program. The processor starts by executing all of the code inside the main() function.

Code:

#include <stdio.h> // This includes a libarary; no instructions
#include <stdlib.h> // Same as above

// This is where the actual instructions start
// The computer will start executing your code from this point
int main(void)
{
   // Code goes in here
   
   // Wait
   system("pause");
   
   // No errors
   return 0; // The processor stops executing your code at this "return" statement
}

So, main() is a function--it is a block of code with a name attached to it. "main" is the name of the function, and the parentheses (round brackets only) following the name "main" are the biggest hint that it is a function. Essentially, a function is just a block of code--lines between braces ({})--that has a name assigned to it. So we call the block of code that the computer should start with "main". Make sense?

Now, the main() function has other things associated with it. For example, main() can be passed arguments, but we put "void" in the parentheses to indicate that this behaviour is not desired. It also has a return value--an "int" in this case. However, for the main() funcion, these features are relatively meaningless.

So, main() is a function, and the Operating System (Windows) runs this function. Executing a block of code by indicating the block's name is called "calling" the function--you refer to a block of code by its function name. Windows calls the main() function, and this function consists of other functions and statements. For example, in the above code, we call "system()", which is also a function. When we do this, the processor stops executing the code in main(), jumps to wherever system() is in memory (remember that this function was defined for us by <stdlib.h>?) and runs its code. When the code in system() is done, the processor jumps back to the main() function, but instead of starting from the beginning again, it continues from where it left off, moving on to the "return" statement.

So, when we put the code between the braces of the main() function, we're deciding what the main() function will do when the operating system "calls" it. We're already writing a function. So who says we can't write another function, with a different name? We can:

Code:

#include <stdio.h> // Input and output functions
#include <stdlib.h> // Utility functions

int main(void)
{
   // Code goes in here
   
   // Wait
   system("pause");
   
   // No errors
   return 0;
}

void MyFunction(void)
{
   // Code for this function goes here
}

We have now defined a new function called "MyFunction". It doesn't receive any arguments, nor does it return a value. If arguments/parameters and return values are confusing you as well, I would suggest re-reading the Functions tutorial about them. Here, I'm just trying to give an overview of functions--the details are in that tutorial.

So we have created a new block of code--indicated by the braces--and whatever code we put in there can be referenced by the name "MyFunction". As our program stands right now, this code does not get used, but the code is still compiled along with the rest of the program--it's just never run. Why is it never run? Because the Operating System ONLY runs the function we call "main". So, in order to run our new function, we have to call it ourselves. Since main() is guaranteed to be run, we call the function from the main() function. I will show this below, and we'll simply make the function print "Hello World" on the screen. The printf() lines before and after exemplify the change in program control between main() and MyFunction():

Code:

#include <stdio.h> // Input and output functions
#include <stdlib.h> // Utility functions

// Program starts here
int main(void)
{
   // Display message before calling function
   printf("Before function...\n");
   
   // Call function
   MyFunction();
   
   // Display message after function is completed
   printf("After function...\n");
   
   // Wait
   system("pause");
   
   // No errors (program ends here)
   return 0;
}

// Our new function
void MyFunction(void)
{
   // Display a message
   printf("Hello World\n");
}

What this program should do is print the following on the screen:

Before function...
Hello World
After function...

This shows how the processor jumps from main() to MyFunction(), executes MyFunction()'s block of code, then returns to main() and finishes the rest of it. That is the entirety of how a function works. It is simply a block of code you can "call" by name. This is useful for a lot of things. For example, if there is a certain piece of code you are using over and over again, you can make it into a function so that you can write the code once, and refer to it by a single line over and over again. You can also, as per the Functions tutorial, get the function to return a useful value to you--like input, or the result of a calculation. You can pass the function values as parameters so that it does something different depending on what it receives. Functions allow a whole new level of flexibility in your programs. They even make it easier to debug programs because, if you split your program into a set of functions, you can figure out which function is messing up, and that gives you a smaller block of code to concentrate on. You can even copy and paste, include, or "link" functions you write into other programs to use in them--again, so you don't have to ever re-write that code.

Anyway, I hope that made things a bit clearer. A function just defines a block of code to be run when it is called, and a name to refer to that block of code by. You also, of course, define a return value if you like (in which case, you need a line that says "return x", where "x" is a value or variable or expression), and you can define parameters in the parentheses. Again, see the Functions tutorial for details on how that works.

Now, this example actually won't work for a reason I try to stress over and over again. The compiler works from top to bottom, which means it sees the line that calls MyFunction() BEFORE it sees the definition of MyFunction(). Since it doesn't know what MyFunction() is, it makes a guess--one that is sometimes right, but usually wrong. If its guess is wrong, you get an error when it finds out what MyFunction() actually is. Therefore, you need to tell the compiler about MyFunction() before it is called. One way of doing it is this:

Code:

#include <stdio.h> // Input and output functions
#include <stdlib.h> // Utility functions

// Our new function
void MyFunction(void)
{
   // Display a message
   printf("Hello World\n");
}

// Program starts here
int main(void)
{
   // Display message before calling function
   printf("Before function...\n");
   
   // Call function
   MyFunction();
   
   // Display message after function is completed
   printf("After function...\n");
   
   // Wait
   system("pause");
   
   // No errors (program ends here)
   return 0;
}

In this case, we just put the declaration for MyFunction() above the main() function so that the compiler will read its definition before encoutering the line that calls the function. This is not a good way of solving the problem though. In large programs, you would have twenty, thirty, or even a hundred functions above the main function, which isn't very logical because the function that is run first should appear at the top. Additionally, once you have other functions calling each other all over the place, you'll find yourself repeatedly moving the functions around to try to get them in an order the compiler likes. A better option is the function prototype I mentioned. Instead of putting the entire function before the main() function, just put one line indicating what the function's name is, what it returns, and what arguments it receives. This allows you to arrange the functions below in any order you'd like (you could even have them in different files with a well-written header file). So, the better solution looks like this:

Code:

#include <stdio.h> // Input and output functions
#include <stdlib.h> // Utility functions

void MyFunction(void); // Prototype. Essentially, the first line of the function.

// Program starts here
int main(void)
{
   // Display message before calling function
   printf("Before function...\n");
   
   // Call function
   MyFunction();
   
   // Display message after function is completed
   printf("After function...\n");
   
   // Wait
   system("pause");
   
   // No errors (program ends here)
   return 0;
}

// Our new function
void MyFunction(void)
{
   // Display a message
   printf("Hello World\n");
}

Unless I made a mistake, you should be able to compile and test this program now. I hope that answered your question, Will Smile I know it was a long-winded response, but programmers tend to get this way. It's rare that someone actually WANTS them to explain something :p Anyway, the function prototype seems to confuse people a lot, but it's really just the first line of the function. The only difference is that you put a semicolon at the end. I hope that helps Wink

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Another Question

Post  William Surmak Sun Mar 18, 2012 11:51 am

Thank you for explaining functions a little better for me, it makes a lot more sense now. Another question though, when attempting to make the guessing game assignment you suggested. rand()%100 does not work for me. I tried printing out the value of rand()%100 to see what was wrong and it was giving me a 7 digit number Neutral . Any suggestions?


Last edited by William Surmak on Sun Mar 18, 2012 12:42 pm; edited 1 time in total (Reason for editing : Spelling/grammar mistakes)
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Random Numbers

Post  Brendan van Ryn Sun Mar 18, 2012 3:22 pm

Well, rand() should give a random double value. You could try doing #include <time.h>. This includes the time() function. Then, only ONCE, at the beginning of the program type "srand(time(NULL));" Finally, you should be able to get a random number:

randomNumber = (int)rand() % 100;

That should work now. If not, try this:

randomNumber = (int)(rand() * 100.0f);

Please post back if it doesn't work Smile

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Dev c++ not working

Post  William Surmak Sun Mar 18, 2012 6:42 pm

Found answer Very Happy .


Last edited by William Surmak on Sun Mar 18, 2012 9:46 pm; edited 1 time in total (Reason for editing : Found answer)
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty working now

Post  William Surmak Sun Mar 18, 2012 9:51 pm

Ok, good news. Rand()%100 actually works. Turns out I had to re install dev c++ because it was not working properly. My code is working now, sorry for the trouble.
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Brendan van Ryn Sun Mar 18, 2012 11:20 pm

No problem Will Smile Actually, Stuart had the same problem where he had to re-install DevC++ as well. Strangely, it's never given me any problems, but maybe you guys have a different version :p

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Programming Request

Post  David Smith Mon Mar 19, 2012 10:01 pm

Riley and I might have found an angle for the launcher that enables us to make the 10 foot shoot and also make the 41" shoot to the top net. This is very good.
This means that we might not need one of the code requests. To have the robot move backwards a preset amount.
I did think of a new request.

Push a button and the wheels on the back of the robot turn (Rotate) a preset amount based on the encoder.
Each time the button is pressed the wheels turn, for example a 1/8 of a turn. Push it again and they turn 1/8 of a turn and they hold there position.

Let me explain why I think this would be helpful.
We line up to shoot. The first ball misses by a little to the left. Instead of ken using the joy stick he pushes the button. This rotates the wheels a little. If we missed by a lot he pushes the button 2 or 3 times.
This will help us align for shooting. The hold feature is needed because the front bumper will be up against the bump in front of the hoops. We will be pivoting on one corner of the robot. Because the bumpers have spring to them and the wheels are on carpet I think a hold feature would be helpful on keeping our position.
I think to make more shoots we need a way of pivoting one or two degrees easily..

David Smith

Posts : 145
Join date : 2010-10-13

Back to top Go down

Programming Questions Empty Request so far

Post  David Smith Mon Mar 19, 2012 10:14 pm

1. steering when the Bridge manipulator is down
2. Drive code so we can straff sideways
3. Change speeds of the launcher by the joy stick knob and have it displayed.
4. Pid lop for launcher speed.
5. Less down and more up for launcher conveyor
6. Will not fire if at the incorrect speed.
7. Push button and the back wheels rotate a small amount and hold position
This will help because the wheels will be 90 degrees turned as compared to the front so
used to steer the robot a few degrees right or left.


David Smith

Posts : 145
Join date : 2010-10-13

Back to top Go down

Programming Questions Empty Follow Up

Post  David Smith Tue Mar 20, 2012 9:56 pm

Brendan, it would be much appreciated if you could update me on what has been accomplished with the programming requests before you leave the shop.
I would also like to be informed of a schedule of when and who will do more work so I can plan accordingly.

We were able to complete one of the requests tonight. The roll speed did work being controlled by the z axis
and the speed was displayed. Jacob and Caleb were not aware of what we were trying to do and also why.
When a task is left for some one to test or complete it is very important that they know exactly what has to be done.

Can we make the display speed go to 3 decimal?
The z axis is far too sensitive also. It only controls the speed above the half way point. Can this be changed?

Testing the shooting tonight there were 5 of us there and all five said that request number 5 would be more helpful then having the joy sticks ramped more. Is it possible to try and make a push button to rotate the wheels.

David Smith

Posts : 145
Join date : 2010-10-13

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Brendan van Ryn Tue Mar 20, 2012 10:17 pm

I'm sorry that I left so quickly, but I was rushed out by my ride. Additionally, I did inform those still present many times of what was being done, as Will must've known for sure what was happening to have handled things. As for the display, I was fairly certain that it DID go to three decimal places already, but the new programmers should've known to change "%0.2f" to "%0.3f". Regardless, the sensitivity of the z-axis is confusing because I have never used it, but it should definitely still work for all values as it has been calibrated recently.

As for the buttons to rotate the robot, I'm sure we'll manage it. I'll be in Thursday this week, and maybe Saturday. I think it would be best if we prioritized things in order of importance so we can get the most valuable changes made first and leave the others unless we have time, as two weeks is not a lot of time for the number of changes we're hoping to make.

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  William Surmak Tue Mar 20, 2012 10:31 pm

Sorry, I left shortly after you so wasn't there for very long after. I have a quick question though. While attempting your calculator assignment I was wondering if there would be anyway to say

if (operation == /)
{
//do this
}
(I know this is wrong but is there anyway to do something like this that would yield no errors)
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Characters

Post  Brendan van Ryn Tue Mar 20, 2012 11:23 pm

Without getting into strings, yes, there is a way of doing this. I think I mentioned in a few of the tutorials that other variable types exist besides the float and int types we mostly use. One of these is the char type. You declare a char the same way as an int, and it acts the same as an int for the most part. The difference is the range. While int can hold between (-2^31) and (+2^31 - 1), char can only hold from -128 to +127. This isn't very useful for numbers (though it is commonly used for flags), but it's perfect for characters.

See, characters on a computer screen are stored as numbers, just like everything else. The way these characters are encoded differ, but one common way is ASCII encoding. If you look up "ASCII chart" on google, a table will show up with 128 characters (including zero) and their ASCII values. For example, '0' is 48, and 'A' is 65. Therefore, to store a character 'B' in a char, you can set that char to 66 (the ASCII for B). Then, if you call printf() with the "%c" specifier, it will print the corresponding character.

Code:

char x = 66; // Character variable set to the ASCII value for 'B'

printf("%c -- %d", x, x);

This code prints the value of x twice--the first time, it prints the character using "%c", which causes the letter 'B' to be displayed. The second time, it displays the number 66, because the "%d" tell it to display a number, not a character. Of course, if printf() can do the "%c" specifier, so can scanf(). So you could take in a character like this:

Code:

char input; // Character input by user

printf("Enter an operation (+, -, x, /): "); // Prompt user to enter addition, subtraction, multiplication, or division
scanf("%c", &input); // Take in a character and store it's ASCII value in "input"

You might need to be careful with scanf() and characters. I think I've had problems with it before, but it should all be fine in theory. Now, all that needs to be done is to check the ASCII value to determine which operation the user wants:

Code:

if(input == 43)
{
    // Perform addition
}

This checks if the character entered was ASCII value 43, which corresponds to the addition sign. This could be repeated for the other three operations (or others if you want to support exponents or something). Now, memorizing a series of ASCII values is a bit tedious, and it makes the code harder to understand, so C has a feature that makes things easier. If you put a single character between SINGLE quotes (apostrophes), it will replace that "character constant" with the correct ASCII value. Therefore, the if-statement could be re-written as:

if(input == '+') // ...

Instead of putting 43, the ASCII value of the plus sign, we put the plus sign itself between single quotes (double-quotes are different and will turn red if you use them).

So that's that Will. Definitely a good question Wink So yes, you can take in characters as wells as numbers, and check them quite easily. Also, as an added note, the single quotes aren't magical at all. They act like a number, or a macro. For example, this code would work fine:

int x;
x = 'A' * 'B' + 'C';

This sets x to (65) x (66) + (67). Again, the single-quoted characters just get replaced by their ASCII values, so they're the same as any other number. Finally, like your strings in printf() and scanf(), special escape-sequences still exist:

"\n" -- ASCII value of a newline (the character at an end of a line when you press "enter")
"\t" -- ASCII value of a tab
"\\" -- ASCII value of a backslash (putting "\" by itself confuses the compiler because the first backslash tells it to look at the next character)

I hope that helps and makes sense Will Wink

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Question

Post  William Surmak Wed Mar 21, 2012 6:33 pm

I tried the other version of the guessing game assignment today and I ran into some problems. After using rand()100% for a starting point and the user entering 1 or -1 for too high or low. I couldn't figure out a expression to say generate a random number that is less then the first number. Something like rand()100%<Value; (except this doesn't work). Any suggestions? Question
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Brendan van Ryn Wed Mar 21, 2012 8:35 pm

I hope that's a typo. The random number is generated by "rand() % 100" not "rand()100%". Also, I should have mentioned that the number is, in both cases, between zero and ninety-nine (inclusively), which the user should be informed of in these two programs. So, let me explain the "rand() % 100" function first.

double rand(void);

rand() returns a random double that can be pretty much any value. How we use this to generate a value in a particular range is the "modulus" operator. First, the modulus operator uses ints only, so we start by casting rand() to an int.

x = (int)rand();

This gives us a random integer of any possible value. To make it in a particular range, we use the percent sign, which, when used outside of scanf() and printf(), calculates the remainder of division. Therefore, in "r = x % y", the computer divides x by y and stores the remainder in r.

5 % 1 = 0 // Five divided by one is five with a remainder of zero
5 % 2 = 1 // Five divided by two is two with a remainder of one
5 % 3 = 2 // Five divided by three is one with a remainder of two
5 % 4 = 1 // Five divided by four is one with a remainder of one
5 % 5 = 0 // Five divided by five is one with a remainder of zero

This shows why we can only use ints for modulus: with integer math, there is a remainder; but with floating-point math, there is no remainder--just decimal places. Without getting into a proof, I hope you'll ieve me when I say that "Y % X" will always give a number that is between zero and "X". Therefore, "rand() % 100" gives a random number between zero and ninety-nine (because you can't have a remainder of 100 if you're dividing by 100). Therefore, to get a number between zero and fifty, you would write "rand() % 51", using 51 instead of 50 because we want to include 50 in the possible numbers.

This give us a "range" for our number, but it always starts at zero. If we wanted a number between 50 and 100, we would write:

x = (int)rand() % 51 + 50

The modulus gives us a number between zero and fifty, and adding fifty gives us a range between fifty and one hundred. Therefore, we can change the possible numbers by changing the two values. Consider something like this:

Code:

int range = 100, minimum = 0, number;

do
{
      number = (int)rand() % range + minimum;
      // Change range and minimum based on the user's response
} // Repeat until the user says it's correct...

So, the first number gives the range, and the second number gives the starting value for this range. I hope that helps, and I'm glad to hear you're plugging away at these assignments. Continue to ask questions as needed Wink

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Programming Requests

Post  David Smith Wed Mar 21, 2012 9:22 pm

After talking with Ken

In Order

1. No parts of code commented out
2. PID loop for roll speed with gating so the launcher does not shoot unless the rolls are at speed.
3. Steering when the Bridge manipulator is down
4. 2011 robot back working so Ken can practise driving
5. Steering when arm down
6. 3 decimal launcher speed control and less sensitivity.
7. Less down and more up for launcher conveyor
8. Push button and the back wheels rotate a small amount and hold position
This will help because the wheels will be 90 degrees turned as compared to the front so
used to steer the robot a few degrees right or left.
8. Drive code so we can strafe sideways
9. Hybrid switch tested

Matt Acceto is available on Monday at 5:00 to help with the strafing sideways problem

Thanks

David Smith

Posts : 145
Join date : 2010-10-13

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  William Surmak Wed Mar 21, 2012 10:39 pm

Explained it to me at robotics today. Smile
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Range Question

Post  Brendan van Ryn Thu Mar 22, 2012 8:43 pm

Will asked me today about the second version of the number-guessing game, and he asked me to post the answer up here. So, here it is.
Essentially, the question concerns how to change the range of the random number generated by the computer as per the user's response (too high, too low, correct). With the exception of correct, obviously, the possible values for the computer to guess is decreased with each response.

We already established the following method for finding a random number based on a range value and a minimum value:
Code:

int guess, range = 100, minimum = 0;

do
{
   guess = (int)rand() % range + minimum;
   
   // Change "range" and "minimum" according to the user's response
} // Repeat until the guess matches the user's number

This formula always gives a value between "minimum" and "minimum + range - 1". This is because "(int)rand() % range" gives a value that is always between zero and "range - 1" (if the remainder were equal to "range", then the quotient would be one larger, reseting the remainder to zero). Adding the "minimum" value allows us to shift that range up above zero. However, you have to change "range" appropriately--a number between zero and one hundred with fifty added becomes a number between fifty and one hundred and fifty. Therefore, the way we would change these values is incredibly important and must be carefully thought-out. Let's look at each case individually: the number is too low, or the number is too high:

If the number is too low, then we know that no value equal to or less than that number can be correct. For example, if the computer guesses "24" and the user says that the number is too low, then the smallest value that the answer could still be is 25--anything equal to or less than 24 will also be too low. Therefore, our "minimum" value must be set to the previous guess (24) plus one (25). This is true for every example. However, as we saw above, if the "minimum" value becomes 25, but the "range" value isn't changed, we will run into a problem. If "(int)rand() % 100" gives a value of, say, 81, then adding 25 to this value will yield 106, which is too large. Therefore, we must also change the value of "range" to keep our value between minimum and one hundred. A good way to do this (which is different than how I showed you Will; sorry) would be to also keep track of a maximum value. This maximum value starts at one hundred. From then on, the "range" is just the maximum value minus the minimum value. At the start of the program, the maximum value is 100 and the minimum value is 0, so our range is 100. After the user says too low, however, our minimum value will change, so our range will become (according to the above example) maximum minus twenty-five, which is 75. Note that this still works. Of course, the "maximum" is a bit misleading because it is exclusive--the value can be greater than or EQUAL to minimum, but only LESS THAN maximum--it cannot be equal to maximum. Therefore, our program always operates in the range of 0 - 99. To change this, you could start with a maximum (and range) of 101, to get 0 - 100. Anyway, our code can be updated like this:

Code:

int guess, maximum = 100, minimum = 0, range = 100;

do
{
   guess = (int)rand() % range + minimum;
   
   // Show guess to user and get input
   
   // If the user says the value is too low...
   minimum = guess + 1;
   range = maximum - minimum;
} // Repeat until the guess is correct

It's up to you guys to fill in the commented code. I hope that much makes sense: we're just changing our minimum value whenever the user says our guess is too low, then we re-calculate the maximum value. Next, the case where our value is too large, is almost exactly the same. All we do is change the maximum instead of the minimum. Also, since the range needs to be re-calculated in BOTH cases, we can keep them ouside of the if-statements:

Code:

int guess, maximum = 100, minimum = 0, range = 100;

do
{
   guess = (int)rand() % range + minimum;
   
   // Show guess to user and get input
   
   // If the user says the value is too low...
   {
      minimum = guess + 1;
   }
   
   // Else, if the user says the value is too high...
   {
      maximum = guess - 1;
   }
   
   // Re-calculate the range
   range = maximum - minimum;
}// While the guess is wrong

This can also be used to detect if the user is lying about his or her number. Eventually, the range will become one--there will only be one possible value left. If the user still says this is wrong, continuing the program will cause "range" to become negative, which could cause weird things to happen. Instead, if the range becomes equal to one, and the user still says the guess is wrong, you can accuse them of cheating, like this:

Code:

// If range == 1 and the user does not say we are correct...
{
   printf("You cheated!\n");
   break;
}

The "break" exits the loop, and is a fairly good example of when it's okay to use it--depending on how your code works, not exiting there could cause messages to be displayed or calculations performed or some other thing inside the loop that you don't want to happen. You could also just set whatever variable you're checking in your "while()" condition to the value necessary for it to quit, but this may or may not be a better solution, depending on your code.

I hope that helps you all, and I'm very proud of you for taking on these challenges and keeping up. I'm especially impressed with Will, who has completed most of the assignments, read all of the tutorials, always shows up to programming meetings, stays late, asks lots of questions, and even looks things up on the internet himself. You've really been keeping on top of things, and great job to anyone else who's been silently working through things as well without my knowledge. Soon enough, the season will be over, and we'll be able to focus entirely on lessons Wink

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Impressed

Post  David Smith Fri Mar 23, 2012 7:45 am

I am also impressed with Will's programming effort.

David Smith

Posts : 145
Join date : 2010-10-13

Back to top Go down

Programming Questions Empty printf not working?

Post  Jacob Fri Mar 23, 2012 7:31 pm

So, I was using printf with the number-ordering code, I'm not entirely sure what happened, but it stopped working. When I tried to use it my compiler (I'm using Code::Blocks) said there was an error. Unfortunately code::Blocks does not show what causes the error. I included both header files, <stdlib.h> and <stdio.h>. I checked to see if the error was my mistake, but I looked online for other uses of printf and inserted them, every one was said to have an error. Does anyone know why there is, or what causes, the error? scratch

Jacob

Posts : 16
Join date : 2011-10-04

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  William Surmak Fri Mar 23, 2012 8:41 pm

Well if nobody can post a better solution i would suggest getting a new compiler such as Dev c++. Its free and only takes a second to download. Also, thanks for the complements it means a lot.


Last edited by William Surmak on Fri Mar 23, 2012 8:59 pm; edited 1 time in total
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Thanks :D

Post  Jacob Fri Mar 23, 2012 8:51 pm

Ok, thanks Will, I'll do that (I got it out of the back of a library book anyway Razz ) The weird thing is that, it used to work, and then just kinda stopped.

Jacob

Posts : 16
Join date : 2011-10-04

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Brendan van Ryn Fri Mar 23, 2012 10:04 pm

Certain compilers, if they aren't installed properly, can do strange things. I would suggest what Will said: try using DevC++. It's pretty compact, and if it stops working, you can just re-install it. You could try re-installing Code::Blocks, but I'm not sure that will work. If it doesn't explain what the error is, it doesn't sound like a reliable IDE anyway. Other than that, you'd have to post your actual code for me to try and help (I suspect a linking error).

Anyway, keep the questions coming if you have any problems Wink Also, when you install Dev-C++, if you're having issues, I would suggest re-installing it and selecting "no" for the code completion option Wink

Brendan van Ryn

Posts : 95
Join date : 2010-09-30
Age : 30

Back to top Go down

Programming Questions Empty Dev C++

Post  Jacob Fri Mar 23, 2012 11:14 pm

Thanks guys, I installed Dev C++, so far, it's been working fine Razz

Jacob

Posts : 16
Join date : 2011-10-04

Back to top Go down

Programming Questions Empty To Brendan:

Post  William Surmak Tue Jun 19, 2012 5:56 pm

When will the programming laptop be returned, it is much needed. Thanks Smile
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Programming Questions Empty Re: Programming Questions

Post  Sponsored content


Sponsored content


Back to top Go down

Page 1 of 2 1, 2  Next

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum