0

The program asks user to enter numbers and stores the numbers in the vector. The program stops asking numbers when user enter a negative number. The negative number is not stored the vector.

When program has finished asking numbers it sorts the numbers. After sorting the program prints the numbers on separate lines with 3 decimal precision.

SPECIFIC QUESTION:

Most of my code is ready I just need to fix the following:

How can I make the program below not to store the negative number in the vector?

Finally, my last line of code is not printing the numbers on separate lines with 3 decimal precision

MY CODE:

#include <iostream>
#include <vector>
#include <iomanip>      // std::setprecision

using namespace std;

int main()
{
    double values;
    int fox;
    vector<double> numbers;
    cout << "Enter any positive numbers (negative to stop): ";
    while (cin >> values) {
        if (values < 0) {
            break;
        }
        numbers.push_back(values);  
    }

    int y = numbers.size();
    for (int i = 0; i < y; i++) {
        cout << numbers[i] << " ";
    }
    
    cout << fixed << setprecision(3) << values << " " << endl;

    return 0;
} 

4 Answers4

2

The negative number is not being stored in the vector. It is explicitly being printed out by

cout << fixed << setprecision(3) << values << " " << endl;

because if a negative number, rather than in input error, exited while (cin >> values), values will be that negative number.

Instead use

#include <iostream>
#include <vector>
#include <iomanip>      // std::setprecision


int main()
{
    double values;
    std::vector<double> numbers;
    std::cout << "Enter any positive numbers (negative to stop): ";
    while (std::cin >> values) {
        if (values < 0) {
            break;
        }
        numbers.push_back(values);
    }

    std::cout << std::fixed << std::setprecision(3);
    for (std::vector<double>::size_type i = 0; i < numbers.size(); i++) {
        std::cout << numbers[i] << " ";
    }

    return 0;
}

The request for a precision of 3 is moved to before the the values are printed, a newline is printed to separate the numbers rather than a space, and values is no longer printed.

In addition using namespace std; has been removed as it is a common source of errors and a the loop indexes have been changed from int to the correct type.

Note: Sorting is not covered by this answer as it is not attempted in the question.

user4581301
  • 33,082
  • 7
  • 33
  • 54
1

As already stated the negative number is not printed due to the iteration of the vector but the last line where you also change the precision using std::setprecision(3) in addition to std::fixed.

The above constructs are what you need to achieve the printing with precision.

You can consult the documentation: http://www.cplusplus.com/reference/iomanip/ If you are in doubt of what the different constructs of iomanip do.

The ostream(cout) which you are using supports the << operators to push data into it.

When pushing numbers into the stream the stream has <<(const double&)overloads to represent the double precision numbers as characters. (not 100% sure thats the signature).

The line feed is based on escape sequences.

A line feed is the std::endl construct which is used in one of the streams above, it has a character representation which dependent on OS can be different but by using the std::endl it should work platform agnostic if i recall correctly.

For more information about newlines consult https://en.wikipedia.org/wiki/Newline Scroll down to the Representation section to see a table with escape sequences.

In short adding std::endl to your stream will do a linebreak.

About the sorting of the vector. Once you are convinced the data in the vector is the numbers you want to sort it is possible to use standard libraries found in the <algorithm> headers of standard cpp

https://en.cppreference.com/w/cpp/algorithm/sort

    #include <algorithm>
    // sort using the default operator<
    std::sort(numbers.begin(), numbers.end());

You can overwrite the function if you want to sort the numbers using other strateges than less(<)

Hope my input is of some use and points you in the right direction of how to solve your issues.

0

To set the presision after the decimal point you can use this:

// Replace n with your number and i with your decimal precision

floor(pow(10,i)*n)/pow(10,i);

Check this out for more information geeksforgeeks

Skywolfmo
  • 24
  • 6
0

You could also use printf.

#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>


int main()
{
    double values;
    std::vector<double> numbers;
    std::cout << "Enter any positive numbers (negative to stop): ";
    while (std::cin >> values) {
        if (values < 0) {
            break;
        }
        numbers.push_back(values);
    }

    for (std::vector<double>::size_type i = 0; i < numbers.size(); i++) {
        printf("%.3f ", numbers[i]);
    }

    return 0;
}
NoahCharef
  • 23
  • 5