-2

I have this assignment due today but i don't quite understand this question. Write a program that outputs Fibonacci numbers. This part I understand I have this it lets you input a number and it'll create a fubonacci sequence of that length.

typedef unsigned long long ull;
int main() {
int N;
cout << "Enter the N : ";
cin >> N;
ull f0 = 0, f1 = 1;
ull f = f1;
cout << "The Sequence of Fibonacci Numbers : " << endl;
cout << f0 << " ";
cout << f1 << " ";

for (int i = 1; i < N; i++) {
cout << f << " ";
f0 = f1;
f1 = f;
f = f0 + f1;
}
cout << endl;
return 0;
}

WHAT I DON'T UNDERSTAND is this part of the assignment.. any of you guys able to make sense of this?

"Using a while loop and two or three integer variables, have your program output a new Fibonacci number to the screen each time the user enters a key (use getchar()!)."

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Not surprised you're confused. That crap, sorry *code*, isn't written to be read. – user4581301 Jul 06 '16 at 21:26
  • 3
    Sounds like you need a strategically placed `cin.get()` within that `for` loop. And you can ask your prof why in the world you would want to use `getchar()` instead. – WhozCraig Jul 06 '16 at 21:29
  • Are you printing `0 1 1 ...`? Shouldn't it be `1 1 2 ...`? – James Adkison Jul 06 '16 at 21:32
  • Instead of the user entering `N`, you're supposed to just start printing Fibonacci numbers, 1 at a time. Then you wait for the user to type something and you print the next number, and so on. – Barmar Jul 06 '16 at 21:42

3 Answers3

0

A fibonacci number will be the sum of the 2 elements before it.

Thus:

 int main(){
      char c = 'a';
      int curr=0, next=1;
      while (c != 'q'){
           c = getchar(); //This just pauses and waits for user input.  Exits if user hits q.
           cout << curr << endl;
           next = curr + next;
           curr = next - curr;
      }
 }

What you should be thinking about is in situations like this how you can avoid using temporary variables (that's why he said 2 or 3 variables). Note that I set next to curr + next. Then I need to set curr to be equal to the old value of next, which is gone -- but it's not really gone, it's just had curr added to it.

Shawn
  • 409
  • 3
  • 12
  • Answers a question, but not the one OP asked. What about `getchar` and why does OP need it along with a while loop and a couple integers? – user4581301 Jul 06 '16 at 21:38
  • Sorry, I don't understand what you mean. His question was "any of you guys able to make sense of this?" concerning "Using a while loop and two or three integer variables, have your program output a new Fibonacci number to the screen each time the user enters a key (use getchar()!).". What do you think he meant for reference? Edit: The reason why specifically he needs to use getchar() is just because that's what the professor has decided he wants to use to go through the loop right? – Shawn Jul 06 '16 at 21:42
  • 1
    The problem: How should OP go about writing this program? You wrote the program. It's a good program. Nice and clear, doesn't require much in the way of explanation. I have no problems with it, other than the fact that OP will now copy and paste it and learn absolutely nothing. – user4581301 Jul 06 '16 at 21:52
-1

try using

c=getchar();
if(c!='/n')
  getchar();

in the beginning of the while loop which will do the fibonacci calculation. The second getchar() is to take care of the newline character, which would cause two character input otherwise,thus printing two fibonacci numbers even when the user entered just one key/character.

-2

Whoever gave you this assignment either doesn't know how C++ works or how English works.

If they meant that literally the moment you press a key (you can't enter a key by the way), the fibonacci number should appear then this question is for you.

If they meant that after pressing Enter (which is when the input is flushed too) the numbers should appear as many times as there are characters (including whitespace), then:

while(getchar() != EOF) //print next fibonacci number

== CORRECTION ==

Actually you'd only need the piece of code I typed if the program was reading a file instead of from the standard input. So, the assignment sounds even more confusing now.

Community
  • 1
  • 1
Andros Rex
  • 372
  • 1
  • 7