Why is your second attempt so much worse than the first one? You were a lot closer before: you only didn't understand how to write a for loop. If you're reading the thread at all, you should have a better idea how now.
Why is your second attempt so much worse than the first one? You were a lot closer before: you only didn't understand how to write a for loop. If you're reading the thread at all, you should have a better idea how now.
You NEED to use a loop to get data from the user.
You NEED to use a loop to display the data.
You do not use () around return. It is just return 0; 0 is everything went ok any non zero is there was a problem.
Actually you would NEED use parentheses any time you needed to distinguish between keywords, such as return, and a token, such as 0, without resorting to white space too, ~Kyo~. It's not strange or illegal, even if it isn't going to invoke a function.
On return values from main? I mean if there was math where it made sense to use it.
return 0; is used in every example
by default
return 0; means program executed correctly under normal circumstances;
if you use return 1; or some other value it means it terminated with issues.
This is normal practice. This is how programming is taught in college and in the AP classes way back in highschool. 0 good non zero lookout. I point out non normal parts those are non normal. Show me a programmer that ALWAYS uses parenthases around ALL returns even for int main().
ima need help. this is not coming out right.
#include <iostream>
using namespace std;
int main (int argc, char* argv[])
{
int fib1 = 0;
int fib2 = 1;
int fib3;
int numbers;
cout<<"How many numbers would you like to see";
cin >> numbers;
int count;
for (count = 1; count <= 100; count = count=1)
cout << count << " ";
fib3 = fib1 + fib2;
cout << fib3;
fib1 = fib2;
fib2 = fib3;
system("pause");
return 0;
}
Your loop executes forever...
you need braces around the code that you want to execute.
probably down to the fib2 = fib3; line...
count = count=1
count is always 1 this loop will never break. You probably ment count = count + 1.
how do i make the fibonacci sequence?
#include <iostream>
using namespace std;
int main (int argc, char* argv[])
{
int fib1 = 0;
int fib2 = 1;
int fib3;
int numbers;
cout<<"How many numbers would you like to see";
cin >> numbers;
{
int count;
for (count = 1; count <= 100; count = count+1)
cout << count << " ";
fib3 = fib1 + fib2;
cout << fib3;
fib1 = fib2;
fib2 = fib3;
}
system("pause");
return 0;
}
It doesn't matter if you think that is true, it is a fact.On return values from main? I mean if there was math where it made sense to use it.
return 0; is used in every example
"return0" would not be correct like "return(0)" or "return 0" is. Thus, parentheses for grouping is not illegal in C++. There is no harm in it, other than it being extraneous under most circumstances.
@whiteflags
It is syntax that would be graded wrong - generally unless teachers are lax now.
Also I don't think I ever said write return0; with no space. Get a life your arguement is unbased and unneeded.
@anytime
You already are move the open breace down to just after the for loop and it will work. You will need to display the first two numbers prior to the for loop though.
Last edited by ~Kyo~; 02-06-2011 at 09:07 PM.
how do i write the code for the fibonacci sequence?
This is your code it works even prints the new number out as long as this is in the loop you have it.
Code:fib3 = fib1 + fib2; cout << fib3; fib1 = fib2; fib2 = fib3;
How are you posting without code tags, anytime?
Why 100 and not number?Code:#include <iostream> using namespace std; int main (int argc, char* argv[]) { int fib1 = 0; int fib2 = 1; int fib3; int numbers; cout<<"How many numbers would you like to see"; cin >> numbers; int count; for (count = 1; count <= 100; count = count+1) { cout << count << " "; fib3 = fib1 + fib2; cout << fib3 << " "; fib1 = fib2; fib2 = fib3; } system("pause"); return 0; }
the sequence is not coming out right
Makes me wonder why he has 1 and not 2 as a start as well shrug minor things to change for him. He just needs to get everything straight and then change the little bits around.
Then the teachers are wrong, and so are you.
Does
result in any compiler messages or bugs? Have you really not seen a return value wrapped in parens before? There are lots of places where spaces are used, where equally valid though perhaps user unfriendly is parens.Code:int main() { return(0); }