1

I'm new to C++ and writing a simple program that should take integers from a file as ints and output them formatted appropriately. The issue is that the program skips one of the values when outputting. eg (\n representing a new line) "51 123\n -10\n 153 111" would come out as "123\n -10\n 153\n 111\n 0". Also any tips or pointers for bettering my code would be great. Here is my 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 << a << "\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 */
    }
}
3
  • You don't have code to write n to cout. Commented Jun 2, 2016 at 4:53
  • @RSahu What do you mean by that? Commented Jun 2, 2016 at 4:56
  • You are reading a number into n using while ( cin >> n ). That number is not being written out to cout. Commented Jun 2, 2016 at 4:58

2 Answers 2

1

Problem 1:

You are reading a number into n using while ( cin >> n ). That number is not being written out to cout. That means, the first number is being read and discarded.

Problem 2:

The line cin >> e; does not really read anything into e. That's why you have 0 in the output.

Suggested Fix:

Read all the numbers in the conditional of the while.

while (cin >> a >> b >> c >> d >> e)
{
   cout << setw(10);
   cout << a << "\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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch, works and your answer led me to this: while (cin >> n ) { if (n < 0) {cout << "-";} else {cout << "+";} cout << setw(10); cout << abs(n) << "\r\n"; }
@Bobbis, glad I was able to help.
0

You while loop condition : while(cin >> n) will try to store an integer in your variable n and return true if the cin >> n succeeded thus allowing you to go inside the loop. I don' really see the point of using a loop if all your data is in a file and your are not using an array/vector to iterate through the file to get the file. Using an array will be way effective e.g:

int values[NUMBER_OF_ELEMENTS]; // if number of elements is known
std::vector<int> values;// Use vector otherwise

In any case remove your while loop condition as a well as the braces associated to it and your code should run as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.