So Im trying to have the user be able to type the word done (and have the program stop), into a loop that finds the average of up to 15. I cant figure out how, am I even on the right track?
int total = 0;
constint sizeofarray = 15;
int numbers[sizeofarray];
string end = "";
cout << "Please enter up to 15 numbers" << endl;
cout << "You can stop at anytime by typing 'done'" << endl;
cout << "Please enter a number: ";
for (int i = 0; i < sizeofarray; i++) {
cout << "Please enter another number, or say done: ";
cin >> numbers[i];
total += numbers[i];
while (end != "done") {
cin >> end;
if (end != "done") {
numbers[i] = atoi(end.c_str());
}
}
cout << "The average is " << (float)total / sizeofarray << endl;
}
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
You want something like this:
1 2 3 4 5 6 7 8 9 10
std::vector<int> numbers;
std::string line;
while(std::getline(std::cin, line) && line != "end")
{
int v;
if(std::istringstream{line} >> v)
{
numbers.push_back(v);
}
}