Eclipse c++ not letting me use global variable? - c++

I'm trying to make this recursive program count how many times it calls itself, and I was going to use a global variable to keep count but eclipse isn't recognizing it for some reason. Here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
int count = 0;
int fib(long int);
int main()
{
long int number;
cout << "Enter a number => ";
cin >> number;
cout << "\nAnswer is: " << fib(number) << endl;
return 0;
}
int fib (long int n)
{
//cout << "Fibonacci called with: " << num << endl;
if ( n <0 )
{
cout <<" error Invalid number\n";
exit(1);
}
else if (n == 0 || n == 1)
return 1;
else{
count++;
return fib(n-1) + fib(n-2);}
cout << count;
}
Whenever I initially declare count, it doesn't even recognize it as a variable, does anybody know the reason for this?

Your problem is here:
using namespace std;
It brings in std::count from the algorithm header, so now count is ambiguous. This is why people are told not to do using namespace std;. Instead, remove that line and put std::cout instead of cout (and the same for cin and endl).

Related

How to loop the program's asking for new set of inputs in c++?

Consider the following code:
#include <iostream>
using namespace std;
int main(){
int a,b;
cout << "Enter two positive numbers:" <<endl;
cin >> a >> b;
if (a<b) cout <<a<<" is less than "<< b<<endl;
else if (a>b) cout <<a<<" is greater than " <<b<<endl;
}
How can I make the program endlessly repeat asking for a new set of numbers as input?
Here's the simplest way of doing what you want (there are other ways). Basically, you just need to 'wrap' the code that you want to repeat in a loop, where the 'test' condition for the loop will always evaluate to true.
Note the comments with "///" I've given:
#include <iostream>
//using namespace std; /// Search this site for "Why using namespace std is bad"
using std::cout;/// Just declare usage of those feature you ACTUALLY use...
using std::cin;
using std::endl;
int main() {
int a, b;
while (true) { /// The test condition will always be "TRUE" so the loop will never end!
cout << "Enter two positive numbers:" << endl;
cin >> a >> b;
if (a < b) cout << a << " is less than " << b << endl;
else if (a > b) cout << a << " is greater than " << b << endl;
// cout /// This line is wrong!
}
}
Feel free to ask for further clarification and/or explanation.
Depends on what exactly do you want your program to do. If you want it to "deny access". For example lets say you have want a number K > 3 always for the program to continue. The all you have to do is use a do- while loop:
do
{
cout << "Enter the value for the sequence: ";
cin >> K;
if ( K <= 3)
{
cout << "Write a bigger number!" << endl;
}
} while(K <= 3);
Otherwise just use a normal loop with the condition suitable for the task.
Suppose your program is to find the Factorial of number and you want it to loop such that it ask for new value from the user
int main()
{
int n;
while (true) {
int factorial = 1;
cin >> n;
if (n==0) {
cout << 0;
}
else {
for (int i=n;i>0;i--) {
factorial = factorial*i;
}
cout << factorial;
}
}
return 0;
}

How can I debug my while loop C++ program?

Please disregard some of the undeclared variables. I do not really know what is wrong.
#include <iostream>
using namespace std;
int main()
{
int number{}, tries{}, ans{}, count{};
cout << "Enter an integer greater than 0: ";
cin >> number;
while (number > 0){
ans = number / 10;
++count;
if (ans == 0){
cout << "The number has " << count << "digits";
break;
}
}
return 0;
}
You're never actually changing number, so every iteration, you set ans to the same thing and run the same test.
As indicated by others, you are not updating the loop variable (number) anywhere inside the loop. Hence it is very likely to get in an infinite loop. Here is a sample updated code you can try out.
#include <iostream>
using namespace std;
int main()
{
int number{}, tries{}, ans{}, count{};
cout << "Enter an integer greater than 0: ";
cin >> number;
if (number<=0){
cout << "Incorrect input.";
}
else{
while (number>0){
number = number / 10;
count ++;
}
cout << "The number has " << count << " digits";
}
return 0;
}

C++ Issue with my code [duplicate]

This question already has answers here:
Why is cout printing twice when I use getline?
(2 answers)
Closed 5 years ago.
When I run this and after I select my number as a player, Computer returns me two outputs (instead of one...). I have no idea why, could you please help me explain why that happens?
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int random(int a, int b)
{
int num = a + rand() % (b + 1 - a);
return num;
}
int main()
{
srand(time(NULL));
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
int min = 1;
int max = 100;
int comp;
string player;
while(1) {
comp = random(min, max);
cout << "Computer: " << comp << endl; // why does this get called twice??
getline(cin, player);
if (player == "too high") {
max = comp - 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "too low") {
min = comp + 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "correct") {
cout << "Computer found the number..." << endl;
break;
}
}
}
It's because you are mixing input using >> and getline. getline reads to the next newline, >> does not. After you have entered your number, there is still a newline left behind, you have typed it, but hasn't yet been read. The first time you call getline that left behind newline gets read, and the program doesn't pause. Only on the second time that you call getline does your program pause and wait for you to type something.
Simple way to fix the problem is
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
// flush pending newline
string dummy;
getline(cin, dummy);

Sudden Break in the while loop in Program to find Factors

I was writing a code that calculates the two factors of any given number using two nested 'While' loops but after just one iteration the loop just stops
Program
#include <iostream>
#include <conio.h>
using namespace std;
long int Password;
void main()
{
long int n=2,n1=2;
cout<<"Type the number whose factor you need"<<endl;
cin>>Password;
while( n < 3600 )
{
while( n1 < 3600 )
{
if( n*n1 == Password )
{
cout<<"your Factors are "<<n<<" and "<<n1<<endl;
getch();
}
else
{
n1++;
break;
}
}
n++;
}
}
Output is only working for small numbers but when a little big numbers are inserted the program terminates. I am not understanding the problem as the code is perfectly fine. Am I having a less powerful processor?
Your program is not calculating the factors of any given number. Also, naming the number "Password" is confusing.
Maybe you want something like this in C++:
#include <iostream>
using namespace std;
int main() {
unsigned int number;
cout << "Enter a positive integer whose factors you need: " << endl;
cin >> number;
cout << "Factors of " << number << " are ";
for (int i = 1; i <= number; ++i) {
if (number % i == 0)
cout << i << " ";
}
cout << endl;
return 0;
}
As was noted in the comments, in C++ you have int main() not void main(), even though some compilers do support void main().

Using function integer arguments with my C++ program

I have a little project to do, I've created a program that when the user types a number, it will say if it's even or odd. I even used a function. Here's my question:
How do I use integer arguments in a function for the program? (My program does work, it just doesn't use integer arguments.)
Instructions
Write a C++ function that accepts an integer argument, determines whether the passed integer is even or odd, and displays the result of this determination. (Hint: Use the % operator.)
Make sure your function is called from main(). Test the function by passing various data to it.
My code
#include <iostream>
using namespace std;
void oddEven() //My Function
{
int num;
cout << "Please enter a number " << endl;
cin >> num;
if (num % 2)
{
cout << "It's odd" << endl;
}
else
{
cout << "It's even" << endl;
}
}
int main() //Main program
{
oddEven(); //Calling my Function
return 0;
}
Sample program:
#include <iostream>
using namespace std;
void oddEven(int num/*num is integer argument*/){
if (num % 2)
{
cout<<"It's odd"<<endl;
}
else
{
cout<<"It's even"<<endl;
}
}
int main() //Main program
{ int x;
cout<<"Please enter a number "<<endl;
cin>>x;
oddEven(x); //Calling my Function
return 0;
}
Here's the code
#include <iostream>
using namespace std;
void oddEven( int num ) //My Function
{
if (num % 2)
{
cout << "It's odd" << endl;
}
else
{
cout << "It's even" << endl;
}
}
int main() //Main program
{
int num;
cout << "Please enter a number " << endl;
cin >> num;
oddEven(num); //Calling my Function
return 0;
}