LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2010, 04:55 AM   #1
stefanolima
Member
 
Registered: Jul 2008
Location: Dar-es-Salaam
Distribution: Fedora,CentOS,Ubuntu
Posts: 60

Rep: Reputation: 15
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?
 
Old 03-11-2010, 06:12 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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++;
	}
}
 
Old 03-12-2010, 01:24 AM   #3
stefanolima
Member
 
Registered: Jul 2008
Location: Dar-es-Salaam
Distribution: Fedora,CentOS,Ubuntu
Posts: 60

Original Poster
Rep: Reputation: 15
[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
 
Old 03-12-2010, 01:37 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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 .

Last edited by Aquarius_Girl; 03-12-2010 at 01:39 AM.
 
Old 03-12-2010, 02:41 AM   #5
stefanolima
Member
 
Registered: Jul 2008
Location: Dar-es-Salaam
Distribution: Fedora,CentOS,Ubuntu
Posts: 60

Original Poster
Rep: Reputation: 15
Ok thanks, let me try it on Linux
 
Old 03-12-2010, 08:09 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the standard channel that reads data from the keyboard? rojo2000r Linux - General 1 05-04-2007 02:04 PM
PS3: Yellow Dog USB Keyboard Error:Infinite Loop on Boot Zeychin Linux - General 2 01-17-2007 04:18 PM
cin>>int; loop crash rino.caldelli Programming 2 02-27-2006 07:03 AM
Registering Keyboard events on C++, not cin >> poeta_boy Programming 5 10-05-2004 09:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration