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

  • Home
  • SEARCH
  • 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 650545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:03:53+00:00 2026-05-13T22:03:53+00:00

The bug starts at cin.getline ( string, 25, ‘\n’ ); or the line below

  • 0

The bug starts at cin.getline ( string, 25, ‘\n’ ); or the line below it (strtod). If I use cin, it works, except I cannot quit out. If I type anything that’s not a double, an infinite loop runs. Need help. Basically, the first iteration runs, does not ask for input, so the user gets the math questions wrong. The second iteration works fine. And the next is fine, too. If I back out, using q, I get dumped back to the mode-chooser. After choosing a mode, the bug reappears for the first iteration. Next iterations it’s gone.

int main()
{
    char choice, name[25], string[25], op;
    int operator_number, average, difference, first_operand, second_operand, input, answer, total_questions = 0, total_correct = 0;
    double dfirst_operand, dsecond_operand, dinput, danswer, percentage;
    bool rounding = false;

    srand ( time(NULL) );

    cout << "What's your name?\n";
    cin.getline ( name, 25, '\n' );
    cout << '\n' << "Hi, " << name << ".";

    do {
    do {
    cout << "\nWhich math operations do you want to practice?\n  1. Addition\n  2. Subtraction\n  3. Multiplication\n  4. Division\n  5. Mixed\n  6. Difference of squares multiplication.\nChoose a number (q to quit).\n";
    cin >> choice;
    } while( choice < '1' || choice > '6' && choice!= 'q');

    cout << "\n";

    switch(choice) {
    case '1':
            while( string[0]!= 'q') {
                dfirst_operand = rand() % 15 + 1;
                dsecond_operand = rand() % 15 + 1;
                danswer = dfirst_operand + dsecond_operand;
                cout << dfirst_operand << " + " << dsecond_operand << " equals?\nEnter q to quit.\n";
                cin.getline ( string, 25, '\n' );
                dinput = strtod( string,NULL);
                //cin >> dinput;
                if(string[0]!='q') {
                ++total_questions;
                if(dinput==danswer) {
                    ++total_correct;
                    cout << "Correct. " << total_correct << " correct out of " << total_questions << ".";
                } else {
                    cout << "Wrong. " << dfirst_operand << " + " << dsecond_operand << " equals " << danswer << ".\n" << total_correct << " correct out of " << total_questions << ".";
                };
                percentage = floor(10000 * (float) total_correct / total_questions)/100;
                cout << ' ' << percentage << "%.\n\n";
                }
            }
            break;
            }
    } while(choice!='q');
    return 0;
}
  • 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-13T22:03:54+00:00Added an answer on May 13, 2026 at 10:03 pm

    The problem is this line:

     cin >> choice;
    

    This line parses the input buffer for character input that can be converted to an integer. So if you enter:

    2<newline>
    

    The string “2” is converted, and <newline> remains in the input buffer; so the subsequent cin.getline() is satisfied immediately.

    This is also why JonH’s suggestion does not work, you need to purge the input buffer after the cin << choice input. An alternative is to use cin.getline() for all input (or better; use ::getline() which operates on std::string rather than C-strings), and then parse that input using a std::istringstream object when you need formatted input scanning.

    However if you must use cin.ignore() to solve this problem, you should do it thus:

    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
    

    where std::numeric_limits is defined in the header. Your solution trusts the user not to enter more than 25 characters. That is not a very safe assumption.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to Wikipedia: On March 28, 2010, the following changes… May 14, 2026 at 5:33 am
  • Editorial Team
    Editorial Team added an answer After some surfing days in the web I found a… May 14, 2026 at 5:33 am
  • Editorial Team
    Editorial Team added an answer look into creating an "HTML Application" (.HTA) May 14, 2026 at 5:33 am

Related Questions

This is a registered bug ( Bug ID: 6515708 ) but does anyone have
I am writing an iPhone app – a client for some social network. The
I'm having a problem where a collection of objects isn't being accessed correctly when
We're developing a Python web service and a client web site in parallel. When
Update : The problem appears to have been corrupt DLLs somewhere in my PHP

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.