Bryan Fajardo
Bryan Fajardo

Reputation: 161

Vector Loop not functioning properly

I'm trying to write a program that takes 10 people who ate pancakes, and returns the person who ate the most pancakes, I'm using vectors in order to create new elements as the user enters it and return them later, I tried using an array but I was having a harder time.

#include <iostream>
#include <vector>
using namespace std ; 

int main()
{
    int  lnum , input ;
    int temp = 0;
    vector <int> persons(10) ;

    cout << "This program takes input for 10 people who ate pancakes, \
        then checks who   ate the most pancakes " << endl;

    cout << "Enter the amount of pancakes each person ate: " << endl ;
    cout << "Person 1: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 2: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 3: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 4: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 5: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 6: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 7: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 8: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 9: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << "Person 10: " ; 
    cin >> input ;
    persons.push_back(input);
    cout << endl ;

    for(int i = 0 ; i < 11; i++){
        if(persons.at(i) > temp){
            temp == persons.at(i) ; 
        }
    }

    cout << temp << endl ;
    return 0 ;
}

Mostly everything runs fine, except when I run the program, it returns temp as 0, instead of what the actual number should be. Also when I call commands like

person.at(1) ;

it returns the incorrect value, what I am doing wrong, is this a logical error or syntax?

Upvotes: 0

Views: 98

Answers (3)

godel9
godel9

Reputation: 7390

First, you're initializing your vector incorrectly:

vector <int> persons; // Vector will resize automatically

Then, try = instead of ==, and change the limits of your loop:

for(int i = 0 ; i < persons.size(); i++){
    if(persons.at(i) > temp){
        temp = persons.at(i); // Assignment, not comparison
    }
}

See @ZacHowland's answer for a much easier way to find the maximum element of a vector.

Upvotes: 2

Zac Howland
Zac Howland

Reputation: 15872

You can replace this entire section:

for(int i = 0; i < persons.size(); i++) // note the non-hard-coded conditional
{
    if(persons.at(i) > temp)
    {
        temp = persons.at(i); // note fixed to assignment! 
    }
}

with

temp = std::max_element(persons.begin(), persons.end());

Upvotes: 4

Steve Fallows
Steve Fallows

Reputation: 6424

This line:

temp == persons.at(i) ;

does not assign the value of persons[i] to temp. It compares the two values, then the boolean result is dropped.

You probably meant a single =.

Upvotes: 1

Related Questions