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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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... ;)
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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. :)
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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. ;)
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
So I guess it's that "using the right tools for the job," cliche again. ;)
Or theavailable ones. ;)
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I cant think of too much that i didn't like about basic...
BASIC encourages somebad 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).
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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 ;)
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
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
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403