Thread: while loop weirdness (does not accept compound statements?)

  1. #1
    Unregistered
    Guest

    while loop weirdness (does not accept compound statements?)

    well i have finally solved my problem! woooohoooo!

    now here is my question try to uncomment that while loop with the two statments and try using it... why does it not work when it is compund statement? any way to fix this?
    Code:
    #include <iostream.h>
    
    int main()
    
    {
    	int number1;
    	int number2;
    	int stonum;
    	int count=0;
    	int num1;
    	int num2;
    	
    	cout<<"Enter one single-digit number: ";
    	cin>>number1;
    	cout<<"Enter one single-digit number: ";
    	cin>>number2;
    	
    	cout<<endl;
    	cout<<number1<<" "<<number2;
    		
    	//while ((number1!=num1)&&(number2!=num2))
    	while(number2!=num2)
    	{
    		if (count<1)
    		{
    			num1=number1;
    			num2=number2;
    		}
    		
    		count++;
    		stonum=((num1+num2)%10);
    		cout<<" "<<stonum;
    		
    		num1=num2;
    		num2=stonum;
    	
    		
    	}
    	cout<<endl;
    	
    	return (0);
    }

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You have to place while loops within brackets { }

  3. #3
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Do you get a syntax error or is something not working right?

  4. #4
    Unregistered
    Guest
    Originally posted by rmullen3
    You have to place while loops within brackets { }
    could you plese provide an example i did put my while loop inside the main {}

    sorry i am a bit confused

  5. #5
    Unregistered
    Guest
    Originally posted by tim545666
    Do you get a syntax error or is something not working right?
    no try it for yourself... it compiles fine but the while loop with the two statements will not analyse the second part of the statement for truth why does it do this?

  6. #6
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    You're telling me this doesn't work?
    Code:
    #include <iostream.h>
    
    int main()
    
    {
    	int number1;
    	int number2;
    	int stonum;
    	int count=0;
    	int num1;
    	int num2;
    	
    	cout<<"Enter one single-digit number: ";
    	cin>>number1;
    	cout<<"Enter one single-digit number: ";
    	cin>>number2;
    	
    	cout<<endl;
    	cout<<number1<<" "<<number2;
    		
    	while ((number1!=num1)&&(number2!=num2))
    	{
    		if (count<1)
    		{
    			num1=number1;
    			num2=number2;
    		}
    		
    		count++;
    		stonum=((num1+num2)%10);
    		cout<<" "<<stonum;
    		
    		num1=num2;
    		num2=stonum;
    	
    		
    	}
    	cout<<endl;
    	
    	return (0);
    }
    I don't feel like opening a compiler right now but it looks fine to me.

  7. #7
    Unregistered
    Guest
    yes the while loop will only check for this (number1!=num1) and when it is true it will end... the second part of the statement seems to have no effect on it... im not sure why...

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    int main()

    {
    int number1;
    int number2;
    int stonum;
    int count=0;
    int num1;
    int num2;

    cout<<"Enter one single-digit number: ";
    cin>>number1;
    cout<<"Enter one single-digit number: ";
    cin>>number2;

    cout<<endl;
    cout<<number1<<" "<<number2;

    while ((number1!=num1)&&(number2!=num2))
    {
    What I see here is that neither num1 nor num2 have been initialized. If this is intentional mystery code, I don't like it . If it's an error, why not document the program enough so I can understand what you are trying to do.
    Ciao, Al

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Yeah if people would use descriptive variable names and/or commenting it would be a lot easier and faster to debug programs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  2. Loop, Loop, Loop?!
    By aprilbiz in forum C Programming
    Replies: 10
    Last Post: 07-24-2002, 04:45 AM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM