I am currently working on a small project with Fibonacci numbers and it requires me to report an error when given a negative numbers. This is the assignment my professor gave me,
"The program is not assured of receiving good data, thus if -2 is received as the number of numbers, an error should be reported."

I am having a bit of trouble setting up the error report.

# include <iostream.h>
void main ()
{
int x=0, y=1, b, n=0,ter;

cout<<"Enter The number of terms";
cin>>ter;
cout<<x<<" "<<y<<" ";

while (n<ter-1)
{
b=x+y;
cout<<b<<" ";
x=y;
y=b;

n++;
}

}

Recommended Answers

All 5 Replies

No, you're not having trouble. You're not even trying to check if a negative number has been entered. You need to make an attempt. Where is the most logical place to test for this negative number?

# include <iostream>
using namespace std;
void main ()
{
int x=0, y=1, b, n=0,ter;

cout<<"Enter The number of terms: ";
cin>>ter;
cout<<x<<" "<<y<<" ";

while (n<ter-1)
{
b=x+y;
cout<<b<<" ";
x=y;
y=b;

n++;
}
cout<<endl;
system("pause");
}

commented: Bad code, no CODE Tags, doesn't do what the poster wants, and an attepmt to do his work for him. Baaaad post!! -4

No, don't try that. It's worthless.

#include <iostream>
using namespace std;
void main ()
{
int x=0, y=1, b, n=0,ter;

do
{


cout<<"Enter The number of terms: ";
cin>>ter;
} while (ter<=0);
cout<<x<<" "<<y<<" ";

while (n<ter-1)
{
b=x+y;
cout<<b<<" ";
x=y;
y=b;

n++;
}
cout<<endl;
system("pause");
}

And again. No Code Tags. Non standard code. Attempting to do other's homework for them. Yet another bad post.

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.