program skipping lines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cecnik
    New Member
    • Jun 2014
    • 7

    program skipping lines

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    struct kart
    {
           float x;
           float y;
    };
    
    struct polar
    {
           float r;
           float fi;
    };
    int main()
    {
        
        struct kart T1_kart;
        struct kart T2_kart;
        struct polar T1_polar;
        struct polar T2_polar;
        int a, b;
        float kot, c, d;
    
    
    printf("Ce zelite pretvarjati iz iz kartezicnih koordinat v polarne vnesite k, sicer vnesite p.\n");
    scanf("%c", &a);
    
    switch(a)
    {
             case 'p':{
                  printf("Vnesite dolzino vektorja!\n");
                  scanf("%f", &T1_polar.r);
                  printf("Vnesite kot!(kasneje boste izbrali enoto - radiani/stopinje)\n");
                  scanf("%f", &kot);
                  printf("Vnesite s za stopinje ali r za radiane.\n");
                  scanf("%c", &b);
                  switch(b)
                  {
                  case 's':{
                       T1_polar.fi=kot/57.2957795131;
                       break;
                       }
                  case 'r':{
                       T1_polar.fi=kot;
                       break;
                       }
                  default:{
                  printf("Znova zazenite program in vnesite veljavno vrednost\n");
                 system("pause");
                 return 0;
                 }
                 }
                 T1_kart.x=T1_polar.r*cos(T1_polar.fi);
                 T1_kart.y=T1_polar.r*sin(T1_polar.fi);
                 printf("x = %f, y = %f, r = %f, fi = %f rad\n",T1_kart.x,T1_kart.y,T1_polar.r,T1_polar.fi);
                 break;
                 
                 }
             case 'k':{
                  printf("Vnesite koordinati vektorja!\n\nx= ");
                  scanf("%f", &T2_kart.x);
                  printf("y= ");
                  scanf("%f", &T2_kart.y);
                  c=((T2_kart.x*T2_kart.x)+(T2_kart.y*T2_kart.y));
                  d=T2_kart.y/T2_kart.x;
                  T2_polar.r=sqrt(c);
                  T2_polar.fi=atan(d);
                  printf("r = %f, fi = %f, x = %f, y = %f\n",T2_polar.r,T2_polar.fi,T2_kart.x,T2_kart.y);
                  break;
                  }
             default:{
             printf("Znova zazenite program in vnesite veljavno vrednost\n");
                 system("pause");
                 return 0;
                 }
                 
                 
                 
    
    }
    
    	system("PAUSE");	
        return 0;
    }
    So here is the code I wrote. It works perfectli when your first input is "k" but if you input "p" it works till the line
    Code:
    printf("Vnesite s za stopinje ali r za radiane.\n");
    after that it skips everything too the default statement (the default statement in switch(b)). Any ideas?

    p.s.

    It's a school assignment so I didn't translate the printf stuff, basicly those are instructions to put in information necessary to convert polar coordinates (case 'p') to cartesian and the other way around (case 'k').
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Firstly, you are using scanf into an int. That means you use the %d format specifier. 'k' and 'p' are valid int values. The int is just bogger than the char but both ae integer variables.

    Second, when you enter a 'p' the case for 'p' processes and does a return. That's the end of your program since that is a return from main().

    Your switch structure should look like:

    Code:
    int a = 0;
    	int b = 0;
    
    	switch (a) 
    	{
    	case 'p':
    	{
    				switch (b)
    				{
    				case 's':
    				{
    							//...etc...
    								break;    // exit switch (b)
    				}
    				} //end of switch (b)
            break; //exit switch (a)
    	}
    		
    	}
    Those return 0 statements were put in because the switch statements were not coded correctly, so the program did one case and quit main().

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      check "b" to see if it's picking up the new line char instead of your expected input; mixing data types with scanf can be a nuisance.

      Comment

      Working...