【发布时间】:2013-12-20 03:40:33
【问题描述】:
我正在尝试编写一个程序,该程序需要 10 个吃煎饼的人,并返回吃煎饼最多的人,我正在使用向量以便在用户输入时创建新元素并稍后返回,我尝试使用数组,但我遇到了困难。
#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 ;
}
几乎一切都运行良好,除了当我运行程序时,它返回 temp 为 0,而不是实际数字应该是什么。另外,当我调用像
这样的命令时person.at(1) ;
它返回不正确的值,我做错了什么,这是逻辑错误还是语法?
【问题讨论】:
标签: c++ arrays loops for-loop vector