#include <stdio.h>
int main()
{
    double first_def;
    double secnd_def;
    int fibcount;
    int max;
    double curfib;
    
    int N;
    printf ("Please enter the number of loops (int value)... ");
    scanf("%d", &max);
    if(max <= 0)
    {
           printf ("Number must be greater than zero... seting to default 20\n");
           max = 20;
    }
     
    first_def = 0;
    secnd_def = 1;
    printf("Attempting to calculate fibonacci\n\n");
    printf("Loop  Fib#\n");
    for(fibcount = 1; fibcount <= max; fibcount++)
    {
                 curfib = first_def + secnd_def;
                 secnd_def = first_def;
                 first_def = curfib;  
                 printf ("%3d    %.0f \n", fibcount, curfib);
    }
}

Well, there it is! I am pretty proud of it, too! It will calculate fibonacci to n digits... :D

Since i am new to c (i am moving from QBASIC to C/C++), i would like to hear some criticism about it. Productive only, please... :)

Well, there it is! I am pretty proud of it, too! It will calculate fibonacci to n digits... :D

Since i am new to c (i am moving from QBASIC to C/C++), i would like to hear some criticism about it. Productive only, please... :)

Treat C and C++ as separate languages from early on, it'll save you confusion later.

Test for some larger values, say 1500 -- note the results. How might you handle such issues?

Ditch using scanf for user input to preserve your sanity later.

Make your program better by avoiding the use of scanf() to retrieve input. Although it's taught in many tutorials, and seems to be good, it has a problem with not removing newlines at the end of the stream, and although doesn't hurt you now, it will later.

A better solution is to grab the entire line with getline(), and then parse the data with sscanf(), or any other functions you like. So your input code could look like:

size_t len = 0;
char line[128];
getline(&line, &len, stdin);
sscanf(line, "%d", &max);

Note that len contains the number of charecters read from the input stream, so you may want to check that it's not 0 before sscanf-ing it.

But yeah, your code looks pretty good...

-edit- Looks like Dave beat me to it... ;)

1) What is the major difference between C and C++?


2) Holy crap! They are some pretty big numbers! Scientific notation would be my first guess.... what would you suggest?

3) I didn't plan on using the scanf codes very long... i just pretty much cut and pasted that part... :)

For the record i just made this progam 'cause i was board...

What is the major difference between C and C++?

C++ is a superset of C. C++ has added features, most notably the increment/deincrement operators ++ and -- and object-oriented-ness.

C++ has replacements for nearly all the old C functions, such as cout and cin instead of scanf() and printf(), to name a few. You will notice something about nearly all C++ functions: they're object oriented.

C on the other hand, is procedural-oriented. The difference is that in C++ you're working with objects: think of creating a truck in a program as an example. In C you would probably start by building the back of the truck, and working to the front of the truck. C++ on the other hand, would use objects. A bolt would be an object, the siding would be an object. The whole truck is an object.

And because of this, it's a bad, bad idea to mix object-oriented and procedure-oriented functions. Just imagine the confusion and mixup this would cause. So the bottom line is: use C functions is a C program, and use C++ functions in a C++ program (and make sure that the C++ program is object-oriented). Although it's still possible (and necessary) to use C functions in a C++ program, they should be kept at a minimum and only when C++ doesn't provide the functions you need.

I didn't plan on using the scanf codes very long... i just pretty much cut and pasted that part... :)

It's not a complete no-no, but it's not exactly something you want to get into the habit of doing, and definitely not something you want to implement in a real-world program that lots of people are going to use.

For the record i just made this progam 'cause i was board...

Not bad. :)

1) What is the major difference between C and C++?

At one time about a decade or so ago one could say that "C++ is a superset of C", but that isn't really true at the moment and the two may possibly diverge more in the future.

A "quick" summary is here:
http://david.tribble.com/text/cdiffs.htm

C++ is multi-paradigm, so calling the difference OOP vs not is an oversimplification.
http://en.wikipedia.org/wiki/C_Plus_Plus

C++ is a general-purpose, high-level programming language with low-level facilities. It is a statically typed free-form multi-paradigm language supporting procedural programming, data abstraction, object-oriented programming, generic programming and RTTI.

Keep posting questions about specific code/problems, and it will become easier to assist you in finding which direction you prefer. Early on, differentiating the two -- and various libraries -- can be overwhelming.

---

By the way, joe -- and I'm not trying to pick on you -- I'm currently writing code in C, assembly, and some in-between "structured assembly" (a C-like assembly); that said, even here you can take an OOP approach. These languages just don't hold your ear to the stove.

C++ is multi-paradigm, so calling the difference OOP vs not is an oversimplification.

Well yes, I realized that. But I also realize that the OP may not always want the "whole thing", and I was trying to simplify it. Thanks for pointing that out.

By the way, joe -- and I'm not trying to pick on you -- I'm currently writing code in C, assembly, and some in-between "structured assembly" (a C-like assembly); that said, even here you can take an OOP approach. These languages just don't hold your ear to the stove.

Definitely. OOP was supposed to be the end-all development approach, but C certainly isn't dead, and likely won't be for a long, long time. So I guess it's that "using the right tools for the job," cliche again. ;)

So I guess it's that "using the right tools for the job," cliche again. ;)

Or the available ones. ;)

So would it be ok if i just stuck with C++, but also use some C commands? I really like some of the C commands, and dis like some of the C++ commands, but it also works the other way...

So would it be ok if i just stuck with C++, but also use some C commands? I really like some of the C commands, and dis like some of the C++ commands, but it also works the other way...

You can use C specific features in C++, but getting everything to mesh nicely can be tough. As an example let's say you like scanf but not cin and like C++ strings but not C strings. C++ strings and scanf don't mix well, or at all. ;) You would find yourself doing complex and awkward hacks to glue incompatible features together at the cost of code clarity and flexibility.

One thing I see a lot of is people say they don't like a feature when they really mean that they don't understand it. When I first learned C++, I didn't like the iostream library. Why have so much more code to do the same thing when scanf is more familiar, right? Once I really learned it, I changed my mind and now I really like the iostream library. The only difference is that now I know why it's the way it is, and I agree with it. Knowledge truly is power. :)

So make sure that your dislike comes from a full understanding and rejection of something instead of just a lack of understanding. :)

I don't like to type crap like "std::cout >>" and what not...

I don't like to type crap like "std::cout >>" and what not...

You're not going to find a language that doesn't have things you don't like. ;) But if you dismiss them just because of the syntax, you might be missing out on a lot of great functionality. I felt the same way about the same thing, and now that I know better, I prefer it. :)

Besides, the worst that can happen is you know more and can read more code even if you choose not to write it.

I cant think of too much that i didn't like about basic...

I cant think of too much that i didn't like about basic...

BASIC encourages some bad coding habits, such as spaghetti coding with goto statments. Although now BASIC is taught better by proper instructors, you can tell that the goto statement was made with BASIC in mind.

Secondly, although it's relatively easy to use, it's very high-level and doesn't give you very much power such as coding hardware drivers and the like. Also, this high level-ness reduces your program's speed.

There's a lot more that I haven't mentioned, but I hope you get the idea. BASIC is good for teaching beginners; after all, that's what the whole purpose of it is and that's what the acronym stands for. But it's not too ideal in real-world situations (not too ideal, although it still does come in handy sometimes).

So you understand why i want to learn c/c++? Oh... sorry... c OR c++

Here's a simple tip for you:

I don't like to type crap like "std::cout >>" and what not...

By adding using namespace std; to your code, you wouldn't have to type std:: everytime: cout << "hello"; Looks alot cleaner.

So you understand why i want to learn c/c++? Oh... sorry... c OR c++

Yup

Regards Niek

Here's a simple tip for you:

By adding using namespace std; to your code, you wouldn't have to type std:: everytime: cout << "hello";

There is also the issue of invoking the entire std namespace and the scoping issues resulting from that, but I s'pose that's for another discussion ;)

There is also the issue of invoking the entire std namespace and the scoping issues resulting from that, but I s'pose that's for another discussion ;)

Been there done that :)

Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.