Thread: Vector problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Vector problem

    So here is the assignment speculation:
    Write a program that reads in a list of integers. This list may contain positive or negative integers and be any size, determined by the user of the program. The user should signal they are done entering values by entering the character q instead of an integer. When the user is done entering integer values, the program should output a two-column list. The first column is a list of the distinct vector elements; the second column is the count of the number of occurrences of each element. The list should be sorted on enteries in the first column, largest to smallest.

    If the user enters the following values:

    -12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
    the output should be:

    N Count
    4 2
    3 3
    2 2
    1 4
    -1 1
    -12 4

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    /**
    
    */
    
    int main()
    {
        vector<double> integers;
        cout << "Please enter integers. Enter q to see result.\n";
        bool more = true;
        while (more)
        {
            int value;
            cin >> value;
            if (value == )
                more = false;
            else
                integers.push_back(value);
        }
    
        int i;
        for (i = 1; i < integers.push_back(value); i++)
        {
            
        }
    
    return 0;
    }
    And this is what I got so far but I'm stuck on if (value == ) because I have no idea how to make the user to input the character instead of an integer in order to quit by entering "q" as stated above in cout. Anyone??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The short answer is to test the stream status after the cin
    cin.good()

    The long answer is to always read user input into a std::string (using getline), then parse the line to see whether it contains numbers or a 'q'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    boost::lexical_cast (see boost.org) is an excellent tool to convert a string to a number. That way, you can read a string, check if it's equal to q, and if not, convert it to a number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed