#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    [C]Problem with while


    Hi all guys i have a problem with while...

    the probram must to stop to ask e number and operations when i write =
    but don't work...

    do you know why?

    Code:
    #include <stdio.h>
    #define PER_ARRAY 50
    
    int main () {
    	
    	int partenza = 0;	
    	int numeri[PER_ARRAY];
    	char operatore[PER_ARRAY];
    	char *puntatore_operatore;
    	puntatore_operatore = operatore;
    	
    	
    	printf("Calcolatrice V0.3 \n");
    
    
    	
    	
    		while(operatore[partenza] != '=') 
    	{
    		
    	    printf("Numero %d: \n", partenza);
    		scanf( "%d", &numeri[partenza]);
    		
    	
    		while ((operatore[partenza] != '-') &&  (operatore[partenza] != '/') && (operatore[partenza] != '+') &&   (operatore[partenza] != '*') &&    (operatore[partenza] != '=')) {
    		
    		printf("Inserisci il tipo di operazione: + - * / \n Invece per il risultato digita = \n");
    		scanf(" %c",&operatore[partenza]);	
    		}
    			partenza++;
    		
    		}
    		
    	
    	int i, risposta= numeri[0];
    	for(i = 1; i < partenza; i++)
    	{
    		if(operatore[(i - 1)]  == '*') {
    			risposta *= numeri[i];
    		}
    		else if(operatore[(i - 1)]  == '/'){
    			risposta /= numeri[i];
    		}
    		else if(operatore[(i - 1)]  == '-'){
    			risposta -= numeri[i];
    		}
    		else if(operatore[(i - 1)]  == '+'){
    			risposta += numeri[i];
    		}
    	}
    	printf("Il risultato : %i", risposta);
    	
    	
    	
    	return 0;
    }
  2. #2
  3. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1110
    The issues with your code are:
    1. You still didn't check your input functions for success.
    2. The line
    Code:
    int i, risposta= numeri[0];
    is in between the program and is not supported by C89. Just in case you are using a C++ compiler, be aware.
    As far as your problem is concerned, you need a do while loop, this type of situation is where the real use of the do-while loops are understood.
    Try this:
    Code:
    do
    {
        printf("Numero %d: \n", partenza);
        scanf( "%d", &numeri[partenza]);
    
    	while ((operatore[partenza] != '-') &&  ... (operatore[partenza] != '=')) 
           {
             printf("Inserisci il tipo di operazione: + - * / \n Invece per il risultato digita = \n");
    	scanf(" %c",&operatore[partenza]);	
           }			
    }while(operatore[partenza++] != '=') ;
    But don't just copy paste it, try to understand what is done, and ask questions if you don't understand. Oh, and do remember to check scanf for failure. Best of luck
    Last edited by RAJ_55555; September 27th, 2010 at 03:25 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0
    Originally Posted by RAJ_55555
    The issues with your code are:
    1. You still didn't check your input functions for success.
    2. The line
    Code:
    int i, risposta= numeri[0];
    is in between the program and is not supported by C89. Just in case you are using a C++ compiler, be aware.
    As far as your problem is concerned, you need a do while loop, this type of situation is where the real use of the do-while loops are understood.
    Try this:
    Code:
    do
    {
        printf("Numero %d: \n", partenza);
        scanf( "%d", &numeri[partenza]);
    
    	while ((operatore[partenza] != '-') &&  ... (operatore[partenza] != '=')) 
           {
             printf("Inserisci il tipo di operazione: + - * / \n Invece per il risultato digita = \n");
    	scanf(" %c",&operatore[partenza]);	
           }			
    }while(operatore[partenza++] != '=') ;
    But don't just copy paste it, try to understand what is done, and ask questions if you don't understand. Oh, and do remember to check scanf for failure. Best of luck
    thanks for all
  6. #4
  7. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1110
    stop quoting entire posts. uncheck the "Quote message in reply?" checkboxm if you don't need to quote someone.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0
    ok sorry

IMN logo majestic logo threadwatch logo seochat tools logo