LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help: cin>> in while loop , it reads keyboard only once (https://www.linuxquestions.org/questions/programming-9/help-cin-in-while-loop-it-reads-keyboard-only-once-794662/)

stefanolima 03-11-2010 04:55 AM

help: cin>> in while loop , it reads keyboard only once
 
Hi!

I'm entering data in array using while loop, the problem I'm getting is that it only asks me to enter data only once, it never repeat asking me to keep on entering data.

I'm just using simple while loop code with cin command

while (y < 5)
{cout<<"Enter the "<<z<<"th word:\n";
z++;
cin>>ziword;
array[y] = ziword;
y++;
}

How to make sure that it asks me to enter data in all five steps?Is it the problem with cin command?

Aquarius_Girl 03-11-2010 06:12 AM

Quote:

Originally Posted by stefanolima
How to make sure that it asks me to enter data in all five steps?Is it the problem with cin command?

I tried the below program, its working absolutely fine, what's the problem ?.

It is asking me to enter data in all five steps !!!!

Code:

#include <iostream>
using namespace std;

int main ()
{
        int y = 0;
        int z = 0;
        int ziword;
       
        while (y < 5)
        {
                cout << "Enter the "<<z<<"th word:\n";
                z++;
                cin >> ziword;
                cout << ziword;
                y++;
        }
}


stefanolima 03-12-2010 01:24 AM

[QUOTE=anishakaul;3894087]I tried the below program, its working absolutely fine, what's the problem ?.

Maybe it's a compile problem, or the functioning of my windows xp.

Because I was using codeblock and it asks me to enter data only once, and then it prints
Enter the 1th word:
kachiri
4246726Enter the 2th word:
4246726Enter the 3th word:
4246726Enter the 4th word:
4246726Enter the 5th word:
4246726
Process returned 0 (0x0) execution time : 3.843 s
Press any key to continue.

When I use Dev-C++ it asks me to enter data only once and then the screen disappears, which compiler are you using?

Cheers,
Stefano

Aquarius_Girl 03-12-2010 01:37 AM

Quote:

Originally Posted by stefanolima
When I use Dev-C++ it asks me to enter data only once and then the screen disappears, which compiler are you using?

I am using gcc compiler and I am on Linux,

I can't guide anything regarding the compilers used on windows,
Its been 6 years since I last worked on windows !!!

May be you should ask this on some windows forum .

stefanolima 03-12-2010 02:41 AM

Ok thanks, let me try it on Linux

ntubski 03-12-2010 08:09 AM

Did you want to enter numbers or words? I ran this on windows and got the same behaviour you described, but after changing the declaration of zi word to
Code:

string ziword;
it works fine.


All times are GMT -5. The time now is 11:32 PM.