while() loop isn't working right..

This is a discussion on while() loop isn't working right.. within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Sebastiani I should have been more clear earlier. I suggested using getch() for "quit". The reason is ...

  1. #16
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Sebastiani
    I should have been more clear earlier. I suggested using getch() for "quit". The reason is that cin can mess with the input stream at times, inserting line feeds and such. Also, if you don't enter anything, cin will often crash the computer! So stick to getch() for chars, and fgets() for char arrays. I inserted fgets() into your code and it worked fine, by the way...
    but.. fgets requires 3 parameters.. char array, size, and the file input.. but there are no files being dealt with here.

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A file stream. For console input, the file stream is called stdin. So:

    fgets(str, 100, stdin);

    Or wrap it:

    char *get(char str[], int max) {
    return fgets(str, max, stdin);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Sebastiani
    A file stream. For console input, the file stream is called stdin. So:

    fgets(str, 100, stdin);

    Or wrap it:

    char *get(char str[], int max) {
    return fgets(str, max, stdin);
    }
    Thanks, wasn't aware of that.

    Anyways, here it is now (still doesn't work.. works even less actually):

    Code:
    // By Will Herrick
    // takes two numbers and sees if they are factors of each other.
    // also some practice on error-handling.
    // last updated 9-29-02 (doesn't work quite yet!)
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int checkSize(int);
    int checkNumeric(char[256]);
    void factor(int,int);
    int getNumber();
    
    int main()
    {
    	
    	bool doAgain;
    	char again[100];
      int ai;
    	int bi;
    
      doAgain = true;
    	
    	do
    	{  
        cout << "Enter a:";
        ai = getNumber();
    	  cout << endl << "Enter b:";
       
      
        bi = getNumber();
    	  cout << endl;
    	  factor(ai,bi);
     
    	
    	  cout << "Again? (Y/N) ";
    	  fgets(again,100,stdin);
        if(again[0] == 'n' || again[0] == 'N')
        {
          doAgain = false;                           
        }
        else
        {
          doAgain = true;
        }
       }while(doAgain == true);
          return 0;
    }
    
    int checkSize(int a)
    {
    	if(a > 32766)
    	{
    		return 0;
    	}
    	if(a < 32766) return 1;
      return 0;
    }
    
    int checkNumeric(char a[256])
    {
    	int x;
    	int i;
    	int marker;
    	int length;
    
    	length = strlen(a);
    	
    	for(x = 0; x < length; x++)
    	{
    		i = a[x];
    		if((i < 48 || i > 57) && i != '\0')
    		{
    			marker = 1;
    		}
    	}
    	if(marker == 1) return 0;
    	if(marker != 1) return 1;
      return 0;
    }
    
    void factor(int a, int b)
    {
    		if(a % b == 0) cout << b << " is a factor of " << a << endl;
    		if(b % a == 0) cout << a << " is a factor of " << b << endl;
    		if(a % b != 0 && b % a != 0) cout << "Neither number is a factor of the other." << endl;
    }
    
    int getNumber()
    {
    	char a[256];
    	char *p;
      int r;
    	int isNum;
    	int isSize;
    	
    	cout << endl << "Enter the NUMBER: ";
    	fgets(a, 256,stdin);
    	isNum = checkNumeric(a);
    	if(isNum == 1)
    	{
    		p = &a[0];
    		r = atoi(p);
    		isSize = checkSize(r);
    		if(isSize != 1)
    		{
    			r = getNumber();
    		}
        else return r;
    	}
    	if(isNum == 0) 
    	{
    		r = getNumber();
    	}
      return r;
    }
    AHh why am I having so much trouble with something that should be so simple? (or would have been simple in C)

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No worries, mate. Such is the life of a programmer

    Anyway, I tracked it down to the checknumeric function. It was returning zero because of the newline retrieved by fgets. So I added the following, and all works fine:


    Code:
    if((i < 48 || i > 57) && i != '\0' && i != '\n')
    		{
    			marker = 1;
    		}
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    hey Thanks a lot Sebastiani!

  6. #21
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Always use the 'do while' loop if you want the code to run through atleast once. 'while' will only run if the condition is true.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  2. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  3. Replies: 6
    Last Post: 07-19-2005, 01:03 PM
  4. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM
  5. 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21