help: cin>> in while loop , it reads keyboard only once
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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++;
}
}
[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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.