LinuxQuestions.org
Visit Jeremy's Blog.
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 11-06-2001, 03:51 AM   #1
iam3
LQ Newbie
 
Registered: Oct 2001
Posts: 5

Rep: Reputation: 0
Same problem with a new aspect


Note: I have asked a question about the same code before. But now it is a different question. I have just started to learn C++ ;-)


#include <iostream>
main( ) {
char command[40];
int time_of_day;
const bool ever = 1;
for ( ;ever; ) {
cin >> time_of_day >> command;
cout << "Command was " << command << " at time " << time_of_day << endl;
}
}

*^*****************************************
Input = 13 shoot if they shoot first
Output
******
Command was shoot at time 13
Command was at time 13
Command was at time 13
Command was at time 13........

Input = 12:00 fire
Output
******
Command was :00 at time 12
Command was at time 12
Command was at time 12....
*******************************************
Question
********
1: Why do I get infinite loops in both cases?
2: It prints "Command was at time 12 " after first time. Why it does not print " Command was :00 at time 12 "
as before.
 
Old 11-06-2001, 02:51 PM   #2
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
well, the infinite loop part is easy... it's just because you have the



for( ;ever; ){



//stuff...



}



loop in there. unless there's a break in the loop, it's never going to stop. if you just want it to print once, then just comment out the loop.
 
Old 11-07-2001, 12:23 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Your infinite loop is caused by your for condition. I'm sure you are expecting cin to prompt you for more input but your program shoots off into never never land. Your problem is bad data you are entering in. cin is interpreting the data you enter in the following way:

Data: 13 shoot if they shoot first

Loop1:
time_of_day = 13
command = shoot
Loop2:
time_of_day = if (error; all bets are off from here)
command = shoot
Loop3:
...


Your program works if you enter data like this:
data: 13 shoot 14 dude 15 kill 16 hello

If you want to enter a string after the number it is a bit tricker. You will have to use get or getline. I think they are cin members.

Here is a way to check for bad data:
Code:
#include <iostream>

main( )
{
   char command[40];
   int time_of_day;
   const bool ever = 1;
   do
   {
      cin >> time_of_day >> command;

      if ( cin.fail() )
         cout << "Get lost you idiot!" << endl;
      else
         cout << "Command was " << command 
                 << " at time " 
                 << time_of_day << endl
;

   } while ( !cin.fail() && !cin.eof() );
}

Last edited by crabboy; 11-07-2001 at 12:25 AM.
 
  


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
Xine (and Kaffeine) Aspect Ratio Problem g8keepR Linux - Software 1 01-11-2006 05:37 PM
Display/Aspect ratio problem surfdog88 Mandriva 2 03-12-2005 11:17 AM
Mplayer Aspect Ratio Glock Shooter Linux - Software 6 11-22-2003 07:19 PM
16/9 aspect ratio abby_normal Linux - Software 4 11-03-2003 04:40 PM
most important aspect of a computer frieza General 16 08-05-2002 04:18 AM

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

All times are GMT -5. The time now is 12:35 PM.

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