Ja-ja I'm new to this c++, BUT egger to learn. I want to write a program that will cout all the odd numbers between 6 and a positive integer.It must also be bigger than 53. This is what I came up so far but it doesn't give any output :

cout<<"Please enter a number not less than 53\n";
     int a, i;                         // Declare the number input
      cin>>i;
      while (i <= 53)
         { cout << "No, bigger than 53! Try again : ";
           cin >> i;
       for (a>6;a<=i;a++);              //Execution of the series
        if (a%2!=0);                    // calculating the odd numbers
         cout << a <<endl;
         cout <<"This is the odd numbers between 6 and "<<i<<"\n";
         cout<<endl;

Can anybody help? I'm sure there must be an easier way of doing this.
<email snipped>

Dani AI

Generated

The goal is simple: accept a positive integer greater than 53 and print every odd number strictly above 6 up to that integer. The original code from had a mix of classic beginner mistakes: stray semicolons after control statements (as pointed out) that make the loop/if bodies empty, a reversed input test in the retry loop (seen in 's attempt), use of non‑standard void main and old headers, and a scoping error when counting inside the for header. 's suggestion to step by two is efficient; reminded of correct for syntax; and is right to ask for proper code formatting so helpers can read it.

Correct, safe approach (modern C++):

#include <iostream>
#include <limits>

int main() {
    int n = 0;
    while (true) {
        std::cout << "Enter an integer greater than 53: ";
        if (std::cin >> n && n > 53) break;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Invalid input. Please enter a number > 53.\n";
    }

    std::cout << "Odd numbers between 6 and " << n << ":\n";
    for (int v = 7; v <= n; ++v) {
        if (v % 2 != 0) std::cout << v << '\n';
    }
    return 0;
}

Troubleshooting checklist:

  • Never put a semicolon immediately after for(...) or if(...) — that ends the statement and leaves the intended block disconnected.
  • Use int main() and <iostream>, not void main or iostream.h.
  • If you declare loop counters in the for(...) header and need them afterward, declare them before the loop.
  • Decide whether the upper bound is inclusive (<=) or exclusive (<) and test both edge cases (e.g., 54 and 55).
    For standard references on the main signature and C++ IO details, see the C++ reference documentation: .

Recommended Answers

All 6 Replies

for ( int c = 7; c < i; c += 2)
    cout << c << "\n";

> for (a>6;a<=i;a++); //Execution of the series
> if (a%2!=0); // calculating the odd numbers
The trailing ; on both these lines is a huge problem.

Next time, use [code]
[/code] tags around your code.

commented: There is a [noparse] code tag :) +11

Here is the improoved version

#include<iostream.h>
void main(){
int n;
do
{
cout<<"\Enter a Value bigger than 53: ";
cin>>n;
}while(n>53);

for(int i=7,j=0;i<n;i+=2,j++);
cout<<"\nThere are ";
cout<<j;
cout<<" odd number between 6 and";
cout<<n;
}

void main is not an improvement, and nor is posting code without using CODE tags.

Nice for loop

I believe the proper format is
for(int a = x ; a < y; a++)

Salem's request is to repost your code with formatting and Code Tags... When we can read the code, we can help.

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.