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 9033673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:17:22+00:00 2026-06-16T08:17:22+00:00

Possible Duplicate: Problem of using cin twice This code works but not what I

  • 0

Possible Duplicate:
Problem of using cin twice

This code works but not what I intended. Every time I want to enter new salary by pressing 1 the the output at command prompt will become like this:

Comic books             : USD Input error! Salary must be in positive integer.

The code should stop at cout<<"\n\nComic books\t\t: USD "; at line 4 but it just executed with the inner while loop. This is the code:

    double multiplePay =0;

    cout<<"\n\nEnter employee pay for each job";
    while (1){
    cout<<"\n\nComic books\t\t: USD ";
    //cin.get(); if enable, the first user input will be 0. this is not working.

    std::string comic_string;
    double comic_double;
    while (std::getline(std::cin, comic_string))
    {
        std::stringstream ss(comic_string); // check for integer value

        if (ss >> comic_double)
        {
            if (ss.eof())
            {   // Success so get out
                break;
            }
        }

        std::cout << "Input error! Salary must be in positive integer.\n" << std::endl;
        cout<<"Employee salary\t: ";
    }

    comic = strtod(comic_string.c_str(), NULL);

        multiplePay = comic + multiplePay; // update previous salary with new user input
        cout << multiplePay;
    cout << "Add other pay?"; // add new salary again?
    int y;

    cin >> y;
    if (y == 1){


        cout << multiplePay;
    }
    else{
        break;
    }
    } // while

cout << multiplePay;  //the sum of all salary

Using cin.get() will solve the problem but the first user salary input will become 0 and only the next input will be calculated. Help me with this please. Thanks in advance.

  • 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-06-16T08:17:22+00:00Added an answer on June 16, 2026 at 8:17 am

    Your problem is that cin >> y; will read an int, but leave the end-of-line \n in the input buffer. Next time you use getline, it will find this end-of-line immediately and not wait for any more input.

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

Sidebar

Related Questions

Possible Duplicate: JQuery .each() backwards I am using this code to show each div
Possible Duplicate: jQuery .data() Not Updating DOM I am having a problem with using
Possible Duplicate: C++ cin whitespace question I'm having problem trying to understand this piece
Possible Duplicate: Program not waiting for cin I wrote the following code: #include <iostream>
Possible Duplicate: Ajax request problem: error 80020101 I am using JQuery-1.64 and this is
Possible Duplicate: Cross-thread operation not valid I have a problem with using of threading
Possible Duplicate: Send email using System.Net.Mail through gmail. (C#) I am using this code
Possible Duplicate: “slash before every quote” problem I am using FPDF to generate pdfs
Possible Duplicate: Hints for java.lang.String.replace problem? Using string.replace() in Java Why / does not
Possible Duplicate: property not working with getter AND setter this is the problem I'm

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.