kind of new here and need help

This is a discussion on kind of new here and need help within the C Programming forums, part of the General Programming Boards category; i know it may is very simple but can someone tell me how to ask a question in c Ive ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    kind of new here and need help

    i know it may is very simple but can someone tell me how to ask a question in c
    Ive tried everything i could but i cant figure this out when i ask do you want to enter a new value if ans is y do this otherwise do nothing can someone please help

    Code:
    #include<stdio.h>
    #include<math.h>
    
    float R1;
    float R2;
    
    float Rf;
    float Rf2;
    
    float Vin;
    float Vin2;
    
    float Vout;
    float Vout2;
    
    int ans;
    int n;
    int y;
    
    void main(	)
    {
    		
    do
    {
    printf("Please  enter  a value  for Voltage -->");
    scanf("%f",&Vin); 
    }while(Vin<0);
    	
    	do
    	{
    	printf("Please enter a value for Resistor 1 -->");
    	scanf("%f",&R1);
    	}while(R1<0);
    
    		do
    		{
    		printf("Please enter a value for Resistor f -->");
    		scanf("%f",&Rf);
    		}while(Rf<0);
    
    			
    		Vout=-Rf*(Vin/R1);
    		printf("Voltage  out  is =%f \n\n",Vout);
    		printf("Your Voltage in was%f \n\n",Vin);
    
    		
    	printf("Would you like to enter a new value?(y/n)\n\n");
    	scanf("%d",n,y);
    				
    if(ans=y)
    	{
    	
    	do
    	{
    	printf("Please  enter  a value  for Voltage -->");
    	scanf("%f",&Vin2); 
    	}while(Vin2<0);
    			
    		{
    		printf("Please enter a value for Resistor 1 -->");
    		scanf("%f",&R2);
    		}while(R2<0);
    		
    	               {
    		printf("Please enter a value for Resistor f -->");
    		scanf("%f",&Rf2);
    		}while(Rf2<0);
    			
    		Vout2=-Rf2*(Vin2/R2);
    		printf("Voltage  out  is =%f \n\n",Vout2);
    		printf("Your Voltage in was%f \n\n",Vin2);
    		
    }
    else
    {	
    scanf("%d",n);
    printf("Thank you for using the program please press enter to terminate the program\n\n\n\n",n);
    									
    }
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    main is not a void function, ever. There's a FAQ on it. It returns an int.
    = is for assignment.
    == is for testing equality.
    scanf expects all of its arguments to be pointers.

    Thus, if you are trying to pass it a non-pointer, you'll need to pass its address. Like so:
    Code:
    scanf( "%c", &answer );
    if( answer == 'y' || answer == 'Y' )
    {
        ...do stuff...
    }
    Something like that. Your indenting could use a bit of work. Try replacing spaces with tabs, and just indenting four spaces every after { lines and unindenting after }.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Don't use globaal vars
    Don't pass to scanf wrong number of params:
    Code:
    scanf("%d",n,y);
    This is wrong.
    Always pass exactly the same number of argument as you want to read with the format.

    Instead of repeating your code twice - use function or loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    i did what you mentioned but it just go to where i said (ans=n)
    Code:
    			printf("Would you like to enter a new value?(y/n)\n\n");
    			scanf("%c",&ans);
    				
    if(ans == 'y' || ans == 'Y')
    	{
    	
    	do
    	{
    	printf("Please  enter  a value  for Voltage -->");
    	scanf("%f",&Vin2); 
    	}while(Vin2<0);
    			
    		{
    		printf("Please enter a value for Resistor 1 -->");
    		scanf("%f",&R2);
    		}while(R2<0);
    		
    			{
    			printf("Please enter a value for Resistor f -->");
    			scanf("%f",&Rf2);
    			}while(Rf2<0);
    			
    				Vout2=-Rf2*(Vin2/R2);
    				printf("Voltage  out  is =%f \n\n",Vout2);
    				printf("Your Voltage in was%f \n\n",Vin2);
    		
    }
    		else
    		{	
    		printf("Thank you for using the program please press enter to terminate the program\n\n\n\n",n);
    		scanf("%c",&ans);						
    			
    		}

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Remove ,n from the printf - your supplied arguments fro printf should be complient with the format
    Code:
    printf("Thank you for using the program please press enter to terminate the program\n\n\n\n",n);
    This code works:
    Code:
    #include <stdio.h>
    int main()
    {
    	char ans;
    	printf("Would you like to enter a new value?(y/n)\n\n");
    	scanf("%c",&ans);
    				
    	if(ans == 'y' || ans == 'Y')
    	{
    	}
    	
    	else
    	{	
    		printf("Thank you for using the program please press enter to terminate the program\n\n\n\n");
    		scanf("%c",&ans);						
    		
    	}
    }
    But note that the scanf leaves the '\n' symbol in the input buffer
    so if you try to read one more char it will read this symbol instead of the new answer.

    So you should flush the input buffer before asking a new question
    You read the FAQ to find out the way to do it : http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    its working on its own but when i put it in the program it does the same thing

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    have you implemented also the flushing like it is described in the FAQ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Thankx Vart and quzah you guys been very helpfull programs workiong thanx a lot

Popular pages Recent additions subscribe to a feed

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