while loop - C++ - inputting and outputting multiple integers with io redirection -
i'm new c++ , writing simple program should take integers file ints , output them formatted appropriately. issue program skips 1 of values when outputting. eg (\n representing new line) "51 123\n -10\n 153 111" come out "123\n -10\n 153\n 111\n 0". tips or pointers bettering code great. here code:
using namespace std; int main(int argc, char *argv[]) { size_t i; int n; int a, b, c, d, e; if (argc == 1) { while (cin >> n) { cin >> a; cin >> b; cin >> c; cin >> d; cin >> e; cout << setw(10); cout << << "\r\n"; cout << setw(10); cout << b << "\r\n"; cout << setw(10); cout << c << "\r\n"; cout << setw(10); cout << d << "\r\n"; cout << setw(10); cout << e; } } else if (strcmp(argv[1],"-x")==0) { /* not used yet */ } else if (strcmp(argv[1],"-o")==0) { /* not used yet */ } }
problem 1:
you reading number n
using while ( cin >> n )
. number not being written out cout
. means, first number being read , discarded.
problem 2:
the line cin >> e;
not read e
. that's why have 0
in output.
suggested fix:
read numbers in conditional of while
.
while (cin >> >> b >> c >> d >> e) { cout << setw(10); cout << << "\r\n"; cout << setw(10); cout << b << "\r\n"; cout << setw(10); cout << c << "\r\n"; cout << setw(10); cout << d << "\r\n"; cout << setw(10); cout << e; }