Thread: loop not terminating

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    27

    loop not terminating

    I've got a program where the user can add items to an array. All the sections of the array are error checked to make sure the user inputs numbers or letters when they are supposed to.

    The problem I am having is with one field, the others are error checked and working fine. Its the price, it is defined as a float and read into the array differently so I am unsure where the problem is. Here is basically what I'm having trouble with:

    Code:
         float price;
         char tempstr[25];
    
    int check(char tempstr[25])
    {
         int j, c=1;
         j = atoi(tempstr);
    
         if (j==0)
            {
               c = 0;
                }
    
         return c;
    }
    
    
    int main()
    {
         do{
               cout << ("Enter Price: ")
               cin >> tempstr;
     
               if (check(tempstr)==0)
                   {
                      cout << "not valid";
                        }
             }while (check(tempstr) !=1);
         
         scanf("%f",&slist[numitems].price);
    }
    With this code just now, it can detect when letters are entered and returns "Not Valid", but when numbers are entered, the line moves down and then nothing happens, it does not move onto the next section of code. Any idea?
    Last edited by akira181; 05-19-2006 at 10:28 PM. Reason: code error

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, your code contains errors.
    There's a missing semicolon on the cout >> Price line. And why on earth do you have the text "Enter Price: " in parentheses?
    There's also the issue of your check() function. Its local variable has the same name as a global one. (A global which should probably be local to main, and passed to check().)

    I'm a little confused as to what your issue is... the code you posted will remain in the while loop until the user enters a number. (Except 0... and many people recommend using strtol() over atoi())
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    27
    the mistakes in my code do not exist in my actual program. I hand typed that because it was easier that way to cut stuff down than to copy and paste it. The text "Enter Price: " is not in brackets in my code, but it doesn't appear to make a difference if it is or isn't. I don't see what you mean about my variables either.

    My problem is that the program does not continue to do what its supposed to do. When I enter a number for the price and hit enter, it moves onto the next line and does nothing, whereas it should usually display another screen. If i remove the cin, do..while and if loop, it works fine.

    I've just found out that it requires me to enter 2 sets of numbers before it continues to the next screen. Enter price comes up, say I type 57 and hit enter, it moves onto the next line and I have to hit another number, say 2, and then it goes onto the next screen like it should and saves the 57 to the array. I am stuck as to why its asking for the second number
    Last edited by akira181; 05-21-2006 at 11:17 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can circumvent this whole problem you know! :P Just use a std::string to get the input from the user and then use a std::istringstream to put it in float price. If the operation fails, try again.

    You're reinventing the wheel here, stop constructing it out of cement blocks!

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    27
    I've just realised that cin and scanf both asks for a user input, so thats why it is needing two inputs before it moves onto the next screen. My problem is now that I cannot use the same bit of code I've used for the other fields.

    Here is what I do for the other fields:

    Code:
    //Supplier
    
        do{   
    	cout <<("\n\t\t Enter supplier   ");
    	cin >> slist[numitems].supplier;
    
             if (check(slist[numitems].supplier)==1)
                {cout << "Not Valid";}
          }while (check(slist[numitems].supplier)!=0);
    
    //quantity
        do{
    	cout <<("\n\t\t Enter the quantity of this new item   ");
    	cin >> tempstr;
    	
            if (check(tempstr)==0)
                {cout << "Not Valid";}
          }while (check(tempstr)!=1);
    	slist[numitems].quantity=atoi(tempstr);
    
    //price
    	cout <<("\n\t\t Enter price of new item   ");
            scanf("%f", &slist[numitems].price);
    The supplier input gets checked to make sure its letters and then saves it to the array and the quantity input gets checked to see if its numbers and then saves it to the array.

    I cannot use the same method for price as it is defined as a float, so does anyone have any idea how I can do this?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    27
    Quote Originally Posted by citizen
    You can circumvent this whole problem you know! :P Just use a std::string to get the input from the user and then use a std::istringstream to put it in float price. If the operation fails, try again.

    You're reinventing the wheel here, stop constructing it out of cement blocks!
    lol, I know my coding is very messy and I'm just making problems for myself but I'm new to this language so I am unsure of the best way to do things, such as this std::string. I'm not too sure what it mean, I'll have a read through my C++ book to see if I can find anything

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I think I see what you're saying now.

    You can do what you've been doing, but for floats, use the function atof() instead of atoi().

    Do note the note that says that atof() (and atoi() ) do not detect errors, like the user entering in "foobar" for a price.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Terminating loop properly?
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 12-24-2004, 06:34 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM