0

hello i want to build a programme that finds even and odd number from given two numbers. when i build it it says succeed but still it says warning C4700: uninitialized local variable 'number' used

when i debug it debug error appeared how to solve this? and can anybody tell me the reason it happens? thank you so much

below is the code

#include<stdio.h>
#include <iostream>
using namespace std;


int main(){

    int number;
    int firstNum,secondNum;

    cout << "Enter the first number: ";
    cin >> firstNum;

   cout << "Enter the second number: ";
    cin >> secondNum;


    if(number % 2 !=0){
    for(number = firstNum;number <= secondNum; number++)
      cout << number<< " ";

    cout << "Odd numbers in given range are: ";
    cout << number<< " ";
    }
    else if(number % 2 ==0){

   for(number = firstNum;number <= secondNum; number++)
     printf("\nEven numbers in given range are: ");

             cout << number << " ";
    }
    return 0;
}

2 Answers2

0

The reason your program gives error is because you haven't initialized your variable number. I have corrected that. There were few other bugs too which would have given wrong output.I have corrected and reformatted your code a bit -

#include<stdio.h>
#include <iostream>
using namespace std;


int main(){

    int number;
    int firstNum,secondNum;

    cout << "Enter the first number: ";
    cin >> firstNum;

   cout << "Enter the second number: ";
    cin >> secondNum;

    number=firstNum;
    for(number = firstNum;number <= secondNum; number++)
      cout << number<< " ";
    //Following piece will print out all odd numbers
    cout << "\nOdd numbers in given range are: ";
    for(number = firstNum;number <= secondNum ; number++)
        if(number&1)
            cout<<number<<" ";
    cout<<"\n";    
    //Following piece will print out all even numbers        
    cout << "Even numbers in given range are: ";
    for(number = firstNum;number <= secondNum ; number++)
        if(!(number&1))
            cout<<number<<" ";
    cout<<"\n";




    return 0;
}

You are probably trying to print the odd numbers and even numbers in a given range. The above program does exactly that.

Hope this solves your question !

Abhishek Bhagate
  • 5,294
  • 3
  • 12
  • 31
  • To avoid confusion, I have used `&` operator to find even or odd. This is less expensive than the normal `%` operator and also more efficient. The `&` operator will only check the last bit of your number. It's odd if it is set, otherwise even. – Abhishek Bhagate May 27 '20 at 14:01
  • Any compiler from this century will replace `% 2` with `& 1` if it's faster on the target platform. Modern compilers are better than humans at optimising code. – molbdnilo May 27 '20 at 14:06
  • @molbdnilo Most compiler probably would, but not in every case. Anyways, it's good to know this way of checking for parity. On your last point, I do agree that I could have just incremented `number` by two instead of checking everytime. – Abhishek Bhagate May 27 '20 at 14:11
-1

You actually never used number in the entire program except its declaration and neither initialized.

This code will help you understand the usage of your variable number:

#include <iostream>

int main(void) {
    int value1, value2, number; // declaration

    std::cout << "Enter the first number: ";
    std::cin >> value1;

    std::cout << "Enter the second value: ";
    std::cin >> value2;

    number = value1; // assigning number as temporary

    std::cout << "Even numbers in the range: ";

    for (; number <= value2; number++)
        if (number % 2 == 0) 
            std::cout << number << ' ';

    std::cout << std::endl << "Odd numbers in the range: ";

    for (; value1 <= value2; value1++)
        if (value1 % 2 == 1)
            std::cout << value1 << ' ';
    std::cout << std::endl;

    return 0;
}

Side Tip: Avoid using using namespace std.

Rohan Bari
  • 6,770
  • 3
  • 12
  • 31
  • Your code is a bit confusing. You use number in one loop, then suddenly switch to using value1 as a loop iterator. keep it uniform! – Abhishek Bhagate May 27 '20 at 13:57
  • The value of 'number' is changed and no longer usable for expected results. That's why I had commented an important message in the initialization section of 'number'. – Rohan Bari May 27 '20 at 13:58
  • There's nothing wrong in using - `using namespace std`, it will just save you from unnecessary redundancy. Of course, you shouldn't use it when you are working on big projects. But for such small programs, it's probably OK to use. – Abhishek Bhagate May 27 '20 at 13:59
  • Seems like you've never gone through [this important article](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Rohan Bari May 27 '20 at 14:00
  • Seems you didn't read my comment clearly. I said there's no problem in using it for **small programs**. It will just save you from **unnecessary redundancy**. It isn't applicable to not use `using namespace std` everywhere. If it was useless in each case, they probably wouldn't have provided the functionality :) – Abhishek Bhagate May 27 '20 at 14:03
  • We can't take any responsibilities of faultiness in OP's code due to our code. For compatibility purposes, I must not use 'using namespace std' from my side nevertheless I make small programs. – Rohan Bari May 27 '20 at 14:05
  • I agree on your point. But I just prefer to use that one line to avoid using **typing `std::` repeatedly over a gazillion times** when I know that my program is just used by me and is just a small program/assignment/code which will never face issues when I use `using namespace std;`. – Abhishek Bhagate May 27 '20 at 14:09