Reputation: 239
When I try to build a forloop for vector like this:
vector<int> v;
int a;
while (cin>>a){
if(i<=0 || i>=10)
cout << "Please enter int between 0 and 10:" << endl;
else
v.push_back(a);
}
int min;
int sum;
for (int &i: v){
if(v.empty())
min = i;
else(i<min){
min = i;
sum += i;
}
}
It shows :expected initializer before â token
Where did i get wrong? Thanks so much!
.bashrc:
Here is my .bashrc:
# .bashrc
# User specific aliases and functions
export PATH=/usr/remote/gcc-4.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/remote/gcc-4.8/lib:$LD_LIBRARY_PATH
alias g++='g++ -std=c++11'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fio9
Upvotes: 0
Views: 1951
Reputation: 53145
You do not seem to initialize min before using it for the else if condition.
However, you simply have an else with a condition which is a syntax error.
This works for me:
#include <vector>
#include <iostream>
#include <limits.h>
using namespace std;
int main()
{
vector<int> v;
int i;
while (cin >> i) {
if(i < 0 || i > 10)
cout << "Please enter int between 0 and 10:" << endl;
else
v.push_back(i);
}
int sum = 0;
int min = INT_MAX;
for (int &i: v) {
if (i < min)
min = i;
sum += i;
}
cout << "min: " << min << endl;
cout << "sum: " << sum << endl;
}
I build this code with the following command: g++ -std=c++11 main.cpp
Output is:
1
2
3
min: 1
sum: 6
Upvotes: 1