#include <iostream>
#include "medel.h"
#include <vector>
usingnamespace std;
int main()
{
float speedsum;
int antal = 0;
vector<float> myVector;
cout << "Skriv in hur många tal du vill ha in : " << endl;
cin >> antal;
myVector.resize(antal);
for (unsignedint i = 0; i < myVector.size(); i++) {
cout << "skriv in tal : ";
int lol = 0;
cin >> lol;
myVector.push_back(lol);
speedsum =+ myVector.at(i);
cout <<"speedsum : " << speedsum << endl;
}
float medelspeed = medel123(speedsum, antal);
cout << "du har genomsnitshastigheten : " << medelspeed;
cin.get();
}
I am trying to let the user decide how big the vector should be, but i cant figure out a way to do that. some wierd shit is going on my error is "syntex error ; before namespace missing, when i click it i get into some new file called xmemory? wtf :/
Okej now it works, apperently i need ; on the header, althou the for loop wont stop and the values wont add up on speedsum? any suggestons to fix that?
Okej now it works, apperently i need ; on the header, althou the for loop wont stop and the values wont add up on speedsum? any suggestons to fix that?
You are looping on the size of the vector, and you are adding a new element to the vector every iteration, thus the loop will never finish (assuming the vector isn't empty.)
1 2 3 4 5 6 7
for (unsignedint i = 0; i < myVector.size(); i++) {
cout << "skriv in tal : ";
cin >> myVector[i];
speedsum += myVector.at(i); // =+ and += are not the same.
speedsum =+ myVector.at(i);
cout <<"speedsum : " << speedsum << endl;
}
for (unsignedint i = 0; i <antal; i++) {
cout << "skriv in tal : ";
int lol = 0;
cin >> lol;
myVector.push_back(lol);
}
The loop will stop after the amount i choiced for "antal" and i made a new for loop that will
add the amounts to speedsum. but noting gets added. i get the amount of 0 on my screen :/
1 2 3 4
for (unsignedint i = 0; i < antal; i++) {
speedsum += myVector.at(i);
cout << "speedsum : " << speedsum << endl;
}
Why does speedsum not get the amount that myVector has at (i)