Page 12 of 20

Re: C++/C programming discussion

Posted: Mon Sep 27, 2010 5:51 pm
by Scott
Matthew wrote:I think you just need to know a library. I'm not sure.

You'll need a PIC programmer. They can cost various amounts. I've seem some for hundreds of pounds and others for about £30.
So the programmer connects to your PC and transfers the code onto the chip? Aahh, just saying that sounds confusing but it'll be a neat project over the Christmas holidays or sometime where I have the time. I have lots of ideas for little extras that other calculators can't do.
Matthew wrote:I haven't done proper C console input.... ever. I've done C++ but I forgot. I'll see for you. I believe C uses the function scanf()...

http://www.cplusplus.com/reference/clib ... dio/scanf/
I was referring to C++ with never ending loop. I know scanf for C and it does a good job ignoring improper inputs.
Matthew wrote:Unlike the C++ rubbish, this nice C function should return 1 if the input is correct and there is one format specifier. It should return 0 if everything failed.
C++ rubbish? :O

Re: C++/C programming discussion

Posted: Mon Sep 27, 2010 6:40 pm
by Matthew
The solution is to just use scanf all the time. C++ can still use it.

Re: C++/C programming discussion

Posted: Mon Sep 27, 2010 9:05 pm
by Scott
Well I'm going to start my adventure of looking for tutorials, and parts for my calculator. I'd like to do a colour screen with more than enough room for numbers so I can later add more functionality if I so please (such as graphing, ect).

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 4:35 pm
by Matthew
:O

They wont be easy. Where would you get the colour (LCD?) screen from?

No idea how to interface with those.

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 5:13 pm
by Scott
Ya, I'm not sure why I jumped the gun and said that. I should have said:
Scott should have said wrote:Well I'm thinking about starting my adventure of looking for tutorials, and parts for my calculator. I'd like to do a colour screen with more than enough room for numbers so I can later add more functionality if I so please (such as graphing, ect).
I have registered on an electronics forum with a query of beginner projects with hardware and software. So far, as unlikely as it seems, a first project was suggested of a robot: http://www.mstracey.btinternet.co.uk/index.htm

Anyway, I'm unsure about a calculator as taking apart/reading up on them I found that they are mostly electronics with logic gates and little software required.

On another note, I have found a neat programming language: http://processing.org/

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 6:22 pm
by Matthew
Real calculators would have purpose designed hardware and no software at all appart for some software on more advanced calculators.

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 6:34 pm
by Scott
Which is why I'm trying to find a different project.

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 6:57 pm
by Matthew
You can still make a calculator with a large part software. Why don't you try getting a PIC development kit (programmer/tester) and making simple programs with it. Then you might want to try interfacing with basic components on a breadboard and maybe you will someday want to make a home-made printed circuit board (Something experts do and requires the use of dangerous chemicals and extreme safety caution must be used).

Have you used electrical components before and do you know much about them?

Re: C++/C programming discussion

Posted: Tue Sep 28, 2010 7:10 pm
by Scott
Matthew wrote:You can still make a calculator with a large part software. Why don't you try getting a PIC development kit (programmer/tester) and making simple programs with it. Then you might want to try interfacing with basic components on a breadboard and maybe you will someday want to make a home-made printed circuit board (Something experts do and requires the use of dangerous chemicals and extreme safety caution must be used).

Have you used electrical components before and do you know much about them?
I have used them before but too long to remember any information.

Oh, and I have this snippet of C...

Code: Select all

void PlayerTurn(char board[3][3])
{
    int placement;
    int loop = 0;

    do
    {
        printf("\nWhere would you like to place an 'X'?\n> ");
        scanf("%d", &placement);

        switch (placement)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                loop = 0;
                break;

            default:
                loop = 1;
        }
    } while (loop);
}
And although it loops, it does not do a great job of what I am trying to get it to do. I want it to check to see if it's 1-9, and if not, loop again until it is. Help is appreciated.

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 12:29 pm
by Matthew
I hate the use of "do". Using while at the top is my preferred method. I always use the "%i" format specifier also.

You need top change the while condition to "while(!loop)" so it ends when the loop variable becomes true (Use stdbool.h for boolean readability). Else change what the boolean becomes.

For nested breakages you can use gotos. This is another option to exit the loop. Simply use "while(1)" or "while(true)" when you exit loops in that fashion.

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 12:43 pm
by Scott
Matthew wrote:I hate the use of "do". Using while at the top is my preferred method. I always use the "%i" format specifier also.

You need top change the while condition to "while(!loop)" so it ends when the loop variable becomes true (Use stdbool.h for boolean readability). Else change what the boolean becomes.

For nested breakages you can use gotos. This is another option to exit the loop. Simply use "while(1)" or "while(true)" when you exit loops in that fashion.
Do...while loops are fine. You don't like do...while but you are suggesting goto? :-?:

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 12:55 pm
by Matthew
Goto is the only way to break out of nested loops and switch statements. It also allows you to do some other things like basic exception handling.

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 3:03 pm
by Scott
Matthew wrote:Goto is the only way to break out of nested loops and switch statements. It also allows you to do some other things like basic exception handling.
What happened to breaks?

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 6:42 pm
by Matthew
The break statement only breaks out of one loop or switch statement. You can't do several unlike PHP wonderfully does.

Re: C++/C programming discussion

Posted: Wed Sep 29, 2010 7:04 pm
by Scott
Matthew wrote:The break statement only breaks out of one loop or switch statement. You can't do several unlike PHP wonderfully does.
Maybe not on consecutive lines but there are ways around it...

Code: Select all

#include <iostream>

int main()
{
    int i = 3;

    for (;;)
    {
        while (true)
        {
            switch (i)
            {
                case 1:
                    std::cout << i << "\n";
                    break;
                case 2:
                    std::cout << i << "\n";
                    break;
                case 3:
                    std::cout << i << "\n";
                    break;
            }
            if (i == 3)
            break; //break out of while
        }
        if (i == 3)
        break; //break out of for
    }

    std::cout << "\nI got out of all the loops!\n";
    return 0;
}