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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:50:51+00:00 2026-05-20T20:50:51+00:00

I am very much new into C++ and I ran into the following problem.

  • 0

I am very much new into C++ and I ran into the following problem.

My goal:
I want to have a code which does the following:

  1. The user enters a line containing doubles separated somehow
  2. The doubles are being parsed into an array of doubles
  3. A computation(*) on the array takes place. For example its sum
  4. If the user doesn’t brake the loop, it reads a new line, and loop back to 1.
  5. Once the user broke the first loop (by entering empty line or something like that), a new one starts.
  6. The user enters a line containing doubles separated somehow
  7. The doubles are being parsed into an array of doubles
  8. A computation(**) on the array takes place. For example its average.
  9. User brakes the second loop.
  10. Program quits.

My code:

#include <iostream>

int main()
{
    do {
        double x1, x2, x3, y1, y2, y3;
        std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
        if (!std::cin){   
            break;
        }
        std::cout << "\n The sum is: " << (x1+y1+x2+y2+x3+y3) << "\n";
    } while (1);

    do {
        double x1, x2, x3, y1, y2, y3;
        std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
        if (!std::cin){   
            break;
        }
        std::cout << "\n The average is: " << (x1+y1+x2+y2+x3+y3)/6 << "\n";
    } while (1);
    return 0;
}

The Problem:
When I try to stop the first loop, and move to the second by either hitting CTRL-D or giving a letter as input, then the program quits and skips the second loop. I realized that it relates to the cin mechanism, but I failed to cure it.

The question: How should I program this? What is the least painful manner to overcome the problem? 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-05-20T20:50:52+00:00Added an answer on May 20, 2026 at 8:50 pm

    The Problem: When I try to stop the first loop, and move to the second by either hitting CTRL-D or giving a letter as input, then the program quits and skips the second loop. I realized that it relates to the cin mechanism, but I failed to cure it.

    This is because when you try and read a letter or EOF (Ctrl-D) this sets the state of the stream into a bad state. Once this happens all operations on the stream fail (until you reset it). This can be done by calling clear()

    std::cin.clear();
    

    The question: How should I program this? What is the least painful manner to overcome the problem?

    I would not use this technique.
    I would use something like an empty line as a separator between loop. Thus the code looks like this:

    while(std::getline(std::cin, line) && !line.empty())
    {
          // STUFF
    }
    while(std::getline(std::cin, line) && !line.empty())
    {
          // STUFF
    }
    

    Try this:
    Note: in C++ streams work best when numbers are space separated. So it is easy just use space or tab to separate the numbers.

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    struct Average
    {
        Average(): result(0.0), count(0)            {}
        void operator()(double const& val)          { result  += val;++count;}
        operator double()                           { return (count == 0) ? 0.0 : result/count;}
        double  result;
        int     count;
    };
    struct Sum
    {
        Sum(): result(0.0)                          {}
        void operator()(double const& val)          { result  += val;}
        operator double()                           { return result;}
        double result;
    };
    
    int main()
    {
        std::string line;
    
        // Read a line at a time.
        // If the read fails or the line read is empty then stop looping.
        while(std::getline(std::cin, line) && !line.empty())
        {
            // In C++ we use std::Vector to represent arrays.
            std::vector<double>     data;
            std::stringstream       lineStream(line);
    
            // Copy a set of space separated integers from the line into data.
            // I know you wanted doubles (I do this next time)
            // I just wanted to show how easy it is to change between the types being
            // read. So here I use integers and below I use doubles.
            std::copy(  std::istream_iterator<int>(lineStream),
                        std::istream_iterator<int>(),
                        std::back_inserter(data));
    
            // Sum is a functor type.
            // This means when you treat it like a function then it calls the method operator()
            // We call sum(x) for each member of the vector data
            Sum sum;
            sum = std::for_each(data.begin(), data.end(), sum);
            std::cout << "Sum: " << static_cast<double>(sum) << "\n";
        }
    
        // Read a line at a time.
        // If the read fails or the line read is empty then stop looping.
        while(std::getline(std::cin, line) && !line.empty())
        {
            // In C++ we use std::Vector to represent arrays.
            std::vector<double>     data;
            std::stringstream       lineStream(line);
    
            // Same as above but we read doubles from the input not integers.
            // Notice the sleigh difference from above.
            std::copy(  std::istream_iterator<double>(lineStream),
                        std::istream_iterator<double>(),
                        std::back_inserter(data));
    
            // Average is a functor type.
            // This means when you treat it like a function then it calls the method operator()
            // We call average(x) for each member of the vector data
            Average average;
            average = std::for_each(data.begin(), data.end(), average);
            std::cout << "Average: " << static_cast<double>(average) << "\n";
        }
    }
    

    // Or we could templatize the code slightly:

    template<typename T, typename F>
    void doAction()
    {
        std::string line;
    
        // Read a line at a time.
        // If the read fails or the line read is empty then stop looping.
        while(std::getline(std::cin, line) && !line.empty())
        {
            std::stringstream       lineStream(line);
    
            // F is a functor type.
            // This means when you treat it like a function then it calls the method operator()
            // We call action(x) for each object type 'T' that we find on the line.
            // Notice how we do not actual need to store the data in an array first
            // We can actually processes the data as we read it from the line
            F action;
            action = std::for_each(  std::istream_iterator<T>(lineStream),
                                     std::istream_iterator<T>(),
                                     action);
            std::cout << "Action Result: " << static_cast<double>(action) << "\n";
        }
    }
    
    int main()
    {
        doAction<int, Sum>();
        doAction<double, Average>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very much new to programming and have been doing fairly well so far.
I am new to javascript so sorry I don't know very much. I have
I am very new to sql.The actual problem is much bigger. I need information
Today I ran into a very difficult TDD problem. I need to interact with
I have put together a script which is very much like the flickr photostream
I'm new to actionscript and want to write the following code in for loop
I am very much new to stack overflow so I do apologize if I
I am very much new to IOS. I am using the storyboard feature to
I am very much new to BPM (Business Process Management), I was asked to
i m new to this android application development i would be very much grateful

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.