Question?

This is a discussion on Question? within the C++ Programming forums, part of the General Programming Boards category; This program I have written has the user enter a number and it will display the fibonacci number that is ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Question?

    This program I have written has the user enter a number and it will display the fibonacci number that is associated with that number. This part works what i need to know what do I need in my code so that a user can enter 0 or a negative number so they can quit. I want it so that it will ask the user to enter a number and give the fibonacci number until a user enters a 0 or a negative number then it will end the program.

    Thanks,
    Nate

    #include <iostream.h>
    #include <math.h>
    #include <iomanip.h>

    int main()
    {
    int count = 0;
    int count2 = 1;
    int count3 = 1;
    int count4 = 0;
    int num;



    cout << "Enter a number (0 or a negative to quit): ";
    cin >> num;




    do
    {

    count3 = count + count2;
    count2 = count;
    count = count3;

    count4 += 1;

    }while ( count4 != num );

    cout << "Fibonacci ( " << num << " ) = " << count3 << endl;




    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > cin >> num;
    Mmm how about
    cin >> num;
    if ( num <= 0 ) {
    &nbsp; // bye
    }

    Wrap your while loop inside another while loop (or better still, wrap your existing while loop inside a function, which you pass a single int (num) to).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21