Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:09:08+00:00 2026-05-27T16:09:08+00:00

I am trying to solve a 3n+1 problem in C++. I want to take

  • 0

I am trying to solve a 3n+1 problem in C++. I want to take the input in pairs and calculate its maximum cycle length then output it.

I/P: 1 10
     100 200
     201 210
     900 1000
O/P: 1 10 25
     100 200 125
     201 210 89
     900 1000 174

Code:

#include<iostream>
using namespace std;

int clen(int i)
{
    int len=1;
    while(i!=1)
    {
        len++;
        if(i%2==0)
            i/=2;
        else
            i=3*i+1;
    }
    return len;
}

int maxclen(int a, int b)
{
    int i,len,maxlen=0;
    for(i=a;i<=b;i++)
    {
        len=clen(i);
        if(len>maxlen)
            maxlen=len;
    }
    return maxlen;
}

main()
{
    int b[10][2],l,i,len[10];
    for(i=0;;i++)
    {
        cin>>b[i][0];
        if(cin.fail())
        {
            cin.clear();
            goto a;
        }
        cin>>b[i][1];
        if(cin.fail())
            goto a;

    }
    a:
    l=i;
    for(i=0;i<=l;i++)
    {
        if(b[i][1]>b[i][0])
            len[i]=maxclen(b[i][0],b[i][1]);
        else
            len[i]=0;
    }
    for(i=0;i<l;i++)
        cout<<b[i][0]<<" "<<b[i][1]<<" "<<len[i]<<endl;
}

I want to stop entering the input whenever cin.fail() returns true but its working for few first execution of the program but after that it is entering in an infinite loop to enter the numbers and it just cant get out. Can anyone help me on this cin issue, How can I avoid this infinite loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:09:09+00:00Added an answer on May 27, 2026 at 4:09 pm

    You have an off-by-one error over here:

    for(i=0;i<=l;i++)
    {
      // ...
    }
    

    I modified your main like this to correct a couple of problems:

    int main()
    {
        const size_t length = 10;
        int b[length][2], len[length];
    
        size_t i = 0;
        while(i < length)
        {
            cin >> b[i][0];
            if(!cin) break;
            cin >> b[i][1];
            if(!cin) break;
            ++i;
        }
    
        for(int j = 0; j < i; j++)
        {
            if(b[j][1] > b[j][0])
                len[j] = maxclen(b[j][0], b[j][1]);
            else
                len[j] = 0;
        }
        for(int j = 0; j < i; j++)
            cout << b[j][0] << " " << b[j][1] << " " << len[j] << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying for hours now to solve this problem.. So.. I want to use
The actual problem I'm trying to solve is, I want to automatically find out
The problem I am trying to solve is that I want to check all
I'm trying to solve pretty easy problem. I want to establish connection to 2
I'm trying to solve Problem 13 of project euler, which involves the sum of
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I'm trying to solve this problem in a pure-functional way, without using set! .
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.