loop

This is a discussion on loop within the C Programming forums, part of the General Programming Boards category; I am trying to make this program run until 5 valid numbers are entered, but it stips after only 5 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    loop

    I am trying to make this program run until 5 valid numbers are entered, but it stips after only 5 numbers are entered (whether they are valid or not)
    Please help
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(int);
    int isUnique(int[], int, int);
    
    main(){
    
    	int values [SIZE]={0};
    	int count;
    	int i;
    	int valid;
    	int result;
    	int validSoFar;
    	int unique;
    	int uniqueSoFar;
    	int number;
    	count=0;
    	result=0;
    	i=0;
    	validSoFar=0;
    	uniqueSoFar=0;
    
    	for (i=0; i<SIZE; i++){
    		printf("Enter an integer (50-100):\n");
    		scanf("%i",&number);
    		valid =isValid(number);
    
    		if(valid){
    	
    		unique=isUnique(values,SIZE, number);
    
    		if (unique){
    			values[uniqueSoFar]=number;
    			uniqueSoFar++;
    		}
    		validSoFar++;
    		}
    		
    		//validSoFar+=valid;
    		//uniqueSoFar+=unique;
    
    	
    		printf("so far valid %i\n",validSoFar);
    		printf("Unique so far %i\n",uniqueSoFar);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	int i;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }
    
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    		
    		return result;
    	}
    
    
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    First, I may suggest that main should be int main.
    And as for your problem, you tell the compiler you want to loop 5 times only.
    A better idea might be to loop until you have 5 valid numbers. Use a variable to hold the amount of valid answers you've gathered so far.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, because your loop is for(i=0; i < SIZE; i++), and that will finish when i is >= SIZE.

    There are two approaches you can take:
    1. Use a function that "ask user for a number, and don't return until you got a valid one".
    2. Loop around as many times as necessary, and count the number of valid entries, until you get the required ones.

    The first option is fine to do with a for-loop, the second one is more of a do-while (or regular while) loop, since the number of iterations is unknown [the user may hit 5 good numbers in sequence, or may need 100 attempts to get 5 numbers right].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    here is my new code. Now i have another problem. If the last number enetered is not unique it still says that it is unique. why does it do that.
    example: 67 is the second valid number and I enter 67 as the last valid number. for the first 67 it will say valid and for the last 67 it will also say valid.
    The program checks all other numbers but the last valid???????
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(int);
    int isUnique(int[], int, int);
    
    main(){
    
    	int values [SIZE]={0};
    	int count;
    	int i;
    	int valid;
    	int result;
    	int validSoFar;
    	int unique;
    	int uniqueSoFar;
    	int number;
    	count=0;
    	result=0;
    	i=0;
    	validSoFar=0;
    	uniqueSoFar=0;
    
    	for (i=0; i<SIZE; i++){
    		printf("Enter an integer (50-100):\n");
    		scanf("&#37;i",&number);
    		valid =isValid(number);
    
    		if(valid){
    	
    		unique=isUnique(values,SIZE, number);
    
    		if (unique){
    			values[uniqueSoFar]=number;
    			uniqueSoFar++;
    		}
    		validSoFar++;
    		}
    		
    		//validSoFar+=valid;
    		//uniqueSoFar+=unique;
    
    	
    		printf("so far valid %i\n",validSoFar);
    		printf("Unique so far %i\n",uniqueSoFar);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	int i;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }
    
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    		
    		return result;
    	}
    
    
    }

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Another *bad* approach would be to decrement i when a number is invalid. That would also cause you to stop after 5 valid numbers have been input. I stress however that that's a hack and you should use a while loop instead.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    		
    		return result;
    	}
    
    
    }
    Can you step (if you don't have a debugger, check it by hand - it should be trivial) through this code and see what happens?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    this is what i have now
    now what happens is that certain times it goes until 5 valid #s have been entered and other times it goes to 7 valid or even 9.
    ????
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 4
    
    //prototype
    int isValid(int);
    int isUnique(int[], int, int);
    
    main(){
    
    	int values [SIZE]={0};
    	int count;
    	int i;
    	int valid;
    	int result;
    	int validSoFar;
    	int unique;
    	int uniqueSoFar;
    	int number;
    	count=0;
    	result=0;
    	i=0;
    	validSoFar=0;
    	uniqueSoFar=0;
    
    	while (i<=SIZE){
    		printf("Enter an integer (50-100):\n");
    		scanf("&#37;i",&number);
    		valid =isValid(number);
    
    		if(valid){
    	i++;
    		unique=isUnique(values,SIZE, number);
    
    		if (unique){
    			values[uniqueSoFar]=number;
    			uniqueSoFar++;
    		}
    		validSoFar++;}
    		else {
    			i--;
    		}
    		
    	
    		printf("so far valid %i\n",validSoFar);
    		printf("Unique so far %i\n",uniqueSoFar);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	int i;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }
    
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    		
    		return result;
    	}
    
    
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    1: Match your function prototypes to your actual function definitions. They should look the same. Omitting names in prototypes is bad practice.
    2: Again, main should be int main!
    3: Your loop is still incorrect. You want to loop while you still haven't received 5 valid inputs, but you're checking i (wrong variable) against SIZE, which is defined as 4 and not the 5 inputs you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks mats. I did that by hand and I changed it. But i still have the problem of the program running until valid=5 or 7 or 12. It is different every time.
    any suggestions?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    as far as putting the name in the prototype i did that at first and my professor said not to do it.
    I changed SIZE to 5, what should I check against it. i tried values[i], but that just takes two numbers from the user.
    btw what is the difference between main and int main

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Code:
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
                                                    /*Here causes the problem*/
    			return ( result = FALSE );
    		
    		return result;
    	}
    
    
    }

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    this is that i did and it seems to work. but i am not sure because i am having the problem with valid part of it
    Code:
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    	}	
    		return result;
    	
    
    
    }

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks Elysia
    i think i have the part of the validity down, the only problem is that when i enter a negative number the code goes crazy and just keeps printing without stopping.
    How would I fix that
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(int);
    int isUnique(int[], int, int);
    
    main(){
    
    	int values [SIZE]={0};
    	int count;
    	int i;
    	int valid;
    	int result;
    	int validSoFar;
    	int unique;
    	int uniqueSoFar;
    	int number;
    	count=0;
    	result=0;
    	i=0;
    	validSoFar=0;
    	uniqueSoFar=0;
    
    	while (validSoFar<SIZE){
    		printf("Enter an integer (50-100):\n");
    		scanf("&#37;i",&number);
    		valid =isValid(number);
    
    		if(valid){
    		i++;
    		unique=isUnique(values,SIZE, number);
    
    		if (unique){
    			values[uniqueSoFar]=number;
    			uniqueSoFar++;
    		}
    		validSoFar++;}
    		else {
    			i--;
    		}
    		
    	
    		printf("so far valid %i\n",validSoFar);
    		printf("Unique so far %i\n",uniqueSoFar);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	int i;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }
    
    int isUnique(int values[],int size,int number)
    {
    	int result=TRUE;
    	int i;
    	for(i=0; i<size;i++){	
    		if (number==values[i])
    			result=FALSE;
    	}	
    		return result;
    	
    
    
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For your count numbers valid, you don't really need to use i, and mixing i++ and i-- in a loop is definitely not a good thing - only increment it when you KNOW the number is OK.

    But your latest code doesn't actually use i at all, so why not just remove the variable alltogether.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by goran00 View Post
    as far as putting the name in the prototype i did that at first and my professor said not to do it.
    Then don't listen to your professor. I can tell you from experience that it hurts your code far more than it helps.
    Someone who takes a look at those functions later they'll be wonder what kind of arguments the function takes. Just types isn't very descriptive.
    Plus some IntelliSense parsers take the information from the prototypes and just showing a function signature such as "my_func(int, int, int)" is not going to help you at all.
    It's just pure bad practice.

    btw what is the difference between main and int main
    The difference is that the first has an "implicit" return type, which is considered a bad practice. All functions should have an explicit return type, including main. And main should return int, so therefore, the correct(-er) version is int main.
    And to be 100% correct, it should be int main(void).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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