4 Replies - 729 Views - Last Post: 21 December 2010 - 05:56 AM Rate Topic: -----

#1 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Weird for loop problem

Posted 21 December 2010 - 01:02 AM

Hi,

I just noticed this problem, and couldn't figure out why.

This loop is supposed to input 5 integers, but inputs only 4. Why is that?
And the surprising thing is that if you comment the
cin >> array [22]; 
before the for loop, it works normally.

Can someone please help me in this?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
    string input;
    int array [23];
    cin >> array [22];
    for (int i = 0; i < 5; ++i)
    {
        getline (cin, input);
        stringstream (input) >> array [i];
    }
    for (int i = 0; i < 5; ++i)
    {
        cout << array [i] << endl;
    }
    return 0;

}


This post has been edited by tejasd: 21 December 2010 - 01:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Weird for loop problem

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6552
  • View blog
  • Posts: 30,682
  • Joined: 10-May 07

Re: Weird for loop problem

Posted 21 December 2010 - 01:06 AM

Your cin is only inputting a single character into the 22nd element of the array.

Can you share with us what you are inputting into the program at run time, what it's outputting, & what you want (or expect) it to be outputting?
Was This Post Helpful? 0
  • +
  • -

#3 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Re: Weird for loop problem

Posted 21 December 2010 - 01:09 AM

View Postno2pencil, on 21 December 2010 - 12:06 AM, said:

Your cin is only inputting a single character into the 22nd element of the array.

Can you share with us what you are inputting into the program at run time, what it's outputting, & what you want (or expect) it to be outputting?



I'm sorry, I did not get you? That cin, passes the value to an integer. It shouldn't really be a problem.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6552
  • View blog
  • Posts: 30,682
  • Joined: 10-May 07

Re: Weird for loop problem

Posted 21 December 2010 - 01:15 AM

View Posttejasd, on 21 December 2010 - 02:09 AM, said:

I'm sorry, I did not get you?


This line defines an integer array :

int array[23];

Granted, it's only defined, it has no value. Because of this, there is often time "garbage" left at that memory address. When you read in your 1st integer, you specifically tell it to input only to element 22. What's in the 1st 21 elements (from 0 to 21)? You don't know. Thus you get strange results.

For example, change the definition line to the following :

int array[23]={0};

& when the program is ran, I get the following output :

Quote

/code/c >$./lp
1234
4321
1234
4321
1234
0
4321
1234
4321
1234


Lastly, when I change the 2 lines like so :

    int array[23]={0};
    //cin >> array;

I get the following out put:

Quote

/code/c >$./lp
1234
4321
1234
4321
1234
1234
4321
1234
4321
1234


I don't know for certain what you are trying to accomplish with this code. That's why I asked for an example input, & what you expect for output.
Was This Post Helpful? 0
  • +
  • -

#5 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Weird for loop problem

Posted 21 December 2010 - 05:56 AM

cin's >> operator waits for a newline before it returns, but doesn't return it. So after the first number you pressed enter. When cin's >> operator returned, the newline was the next character to be read. getline() reads characters until it encounters a newline, which it immediately did. Now there was only a newline in input. As a newline is not an int, array[0] contained rubbish. To avoid this, add cin.ignore(); right after line 10. Then causes cin to ignore the newline and move on to the next character.

This post has been edited by Hezekiah: 21 December 2010 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1