Shouldn't this program stop, and ask me to "Enter number of values you want to sum: " again, after the first run? Without the while loop, the code does what it is supposed to. Code starts infinite loop after values.push_back(val); part at line 27.
What is the correct way to get this code to run again without having to restart the program?
#include <iostream>
#include <cmath>
#include <vector>
usingnamespace std;
vector<int> values;
int sum = 0;
int summer(int num) { // Sum number of values.
for (int i = 0; i < num; ++i)
sum += values[i];
return sum;
};
int main()
{
while (1) {
cout << "Enter number of values you want to sum: ";
int numofValues = 0;
cin >> numofValues;
cout << "Enter some integers. Enter any letter when done: ";
for (int val; cin >> val;)
values.push_back(val);
cout << "\nSum of the first " << numofValues << " numbers is " << summer(numofValues);
values.clear(); // Clear vector.
};
}
if you put a while(true){} or a for(;;) at least you need a break;
also I dont recommend the condition cin >> val; assign it to a temp variable then do the checking to prevent any runtime failures.
@LB
-2 loops I was referencing to no break
-no check-value : never trust the user to enter the correct type of value , never think a file is not corrupted (mostly for serialization).
From now on , this is ok but you might want to add extra checking to avoid further additionnal - I dont know what is going on.
Can I enter all input with functions like this or is there going to be problems?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int integer () {
std::string line;
int num;
while (std::getline(std::cin, line)
&& !(std::istringstream{line} >> num))
{std::cout << "invalid input\n";};
std::istringstream {line} >> num;
return num;
};
int main ()
{
int a = integer ();
int b = integer ();
std::cout << a+b;
}
You seem to have accidentally changed the order of conditions/statements and your code no longer does the right thing. Re-read my article's code more closely. You can definitely use functions, just make sure you understand what the code actually does.
int numint () { //Read in integer
std::string line;
int num;
while (std::getline(std::cin, line)) {
std::istringstream is {line} ;
if ((is >> num) && !(is >> line)) {
return num;
break; };
std::cout << "invalid input, try again.\n"; };
};
int main ()
{
int a = numint ();
int b = numint ();
std::cout << a+b << std::endl;
}
That's better. The break on line 9 is superfluous - you're returning from the function before that so no code after that will run. Also, your indentation style makes it really hard to read your code, try it this way instead: