Infinite Loop

Not asking for cin just infinitley looping

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2471 Views - Last Post: 01 February 2010 - 10:07 AM Rate Topic: -----

#1 jai6y6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-January 10

Infinite Loop

Posted 31 January 2010 - 08:45 PM

ill just show the code that contains the used variable and the loop
int main()
{
	 float hours_worked;

	 do 
	 {
		   cout << "How many hours did you work this week?";
		   cin >> hours_worked>> endl;
		   if(hours_worked < 1 || hours_worked >85)
		   {
				cout<< "Invalid input. Try again."<<endl;
			}
	  }while(hours_worked <1 || hours_worked > 85);



Now its not waiting for the input its just stuck in an infinite loop.

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite Loop

#2 xTorvos   User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Infinite Loop

Posted 31 January 2010 - 08:50 PM

What are you inputting and are you receiving any output?

EDIT: Also, I'm not a C++ expert, but I don't think you should be using 'endl' with a 'cin' statement.

This post has been edited by xTorvos: 31 January 2010 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,537
  • Joined: 10-May 07

Re: Infinite Loop

Posted 31 January 2010 - 08:51 PM

Your variable "hoursworked" is of type float, but you are comparing it to whole number integers?

The reason why you can't see why your loop isn't existing is because you never show what its' value is. You declare the variable hoursworked, but you don't give it a value. You then have a loop based it's value, but you have not established the value yet. Declare it to zero & see what that does for your program.
Was This Post Helpful? 0
  • +
  • -

#4 jai6y6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-January 10

Re: Infinite Loop

Posted 31 January 2010 - 08:54 PM

View PostxTorvos, on 31 Jan, 2010 - 07:50 PM, said:

What are you inputting and are you receiving any output?

Nothing I run program and get spammed with
How many hours have your worked this week. Invalid Input.Try Again.

Infinitley till i stop program


View PostxTorvos, on 31 Jan, 2010 - 07:50 PM, said:

What are you inputting and are you receiving any output?

EDIT: Also, I'm not a C++ expert, but I don't think you should be using 'endl' with a 'cin' statement.

Set to 0 still not working. I just used a float in case the input was like 4.5 or something. I can make it a int if you think that will make a difference
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,537
  • Joined: 10-May 07

Re: Infinite Loop

Posted 31 January 2010 - 08:57 PM

What happens with the following code?
int main(void) {
	 int hours_worked = 0;
	 cout << "How many hours did you work this week?" << endl;
	 cin >> hours_worked;
	 while (hours_worked < 85) {
		   cout << "How many hours did you work this week?" << endl;
		   cin >> hours_worked;
	   if(hours_worked < 85) cout<< "Invalid input. Try again."<<endl;
	 }
	 return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 GenHornet18   User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Infinite Loop

Posted 31 January 2010 - 09:02 PM

No2 are you going for the 'learning experience' there?

(You realize you have an infinite loop?)
Was This Post Helpful? 0
  • +
  • -

#7 jai6y6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-January 10

Re: Infinite Loop

Posted 31 January 2010 - 09:05 PM

View PostGenHornet18, on 31 Jan, 2010 - 08:02 PM, said:

No2 are you going for the 'learning experience' there?

(You realize you have an infinite loop?)

Yeah this is for an assignment for my Intro to Programming Class. I have the whole program compiling. I just can't figure out why this is an infinite loop. We are supposed to error check which is why I have the loop at all.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,537
  • Joined: 10-May 07

Re: Infinite Loop

Posted 31 January 2010 - 09:05 PM

View PostGenHornet18, on 31 Jan, 2010 - 10:02 PM, said:

(You realize you have an infinite loop?)


How do you figure?

while (hours_worked < 85) {


While the value is less than 85.... so when the value is greater than 85 it will exit.

Oh, well yeah, it needs to be re queried :P
Was This Post Helpful? 0
  • +
  • -

#9 GenHornet18   User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Infinite Loop

Posted 31 January 2010 - 09:06 PM

EDIT: Good

This post has been edited by GenHornet18: 31 January 2010 - 09:07 PM

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,537
  • Joined: 10-May 07

Re: Infinite Loop

Posted 31 January 2010 - 09:08 PM

It's not my program, it's the original posters, so I don't know what the requirements are for the loop. But you say it's infinite, when it clearly is not.
Was This Post Helpful? 0
  • +
  • -

#11 jai6y6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-January 10

Re: Infinite Loop

Posted 31 January 2010 - 09:11 PM

View PostxTorvos, on 31 Jan, 2010 - 07:50 PM, said:

What are you inputting and are you receiving any output?

EDIT: Also, I'm not a C++ expert, but I don't think you should be using 'endl' with a 'cin' statement.

Yeah the "endl" on the cin statement was me being dumb copying from my program. Its not actually in program. but the rest is right
Was This Post Helpful? 0
  • +
  • -

#12 jai6y6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-January 10

Re: Infinite Loop

Posted 31 January 2010 - 09:21 PM

EDIT: Ok nevermind I figured it out. The one thing before the loop that asked for name messed it up. For some reason I had the name variable declared as float instead of string. Fail. Not sure why that would cause the problems it did though

This post has been edited by jai6y6: 31 January 2010 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Infinite Loop

Posted 31 January 2010 - 10:57 PM

Don't worry, C++ has lots of fun ways to shoot yourself in the foot.
Was This Post Helpful? 0
  • +
  • -

#14 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Infinite Loop

Posted 31 January 2010 - 11:07 PM

i usually just make a while(true) and then do an if statment to break out

	 float hours_worked;

	 while(true)
	 {
		   cout << "How many hours did you work this week?";
		   cin >> hours_worked;
		   if(hours_worked < 1 || hours_worked >85)
				cout<< "Invalid input. Try again."<<endl;
		   else
			   break;
	  }


Was This Post Helpful? 0
  • +
  • -

#15 ymihere   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 10

Re: Infinite Loop

Posted 01 February 2010 - 07:10 AM

#include<iostream.h>
#include<conio.h>
int main()
{
	 float hours_worked;

	 do
	 {
	   cout << "How many hours did you work this week?";
	   cin >> hours_worked;
	   if(hours_worked < 1 || hours_worked >85)
	   {
		cout<< "Invalid input. Try again."<<endl;
		}
	  }while(hours_worked <1 || hours_worked > 85);
getch();
return 0;
}


This Gives Correct Output....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2