473,466 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

program skipping lines

7 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. struct kart
  7. {
  8.        float x;
  9.        float y;
  10. };
  11.  
  12. struct polar
  13. {
  14.        float r;
  15.        float fi;
  16. };
  17. int main()
  18. {
  19.  
  20.     struct kart T1_kart;
  21.     struct kart T2_kart;
  22.     struct polar T1_polar;
  23.     struct polar T2_polar;
  24.     int a, b;
  25.     float kot, c, d;
  26.  
  27.  
  28. printf("Ce zelite pretvarjati iz iz kartezicnih koordinat v polarne vnesite k, sicer vnesite p.\n");
  29. scanf("%c", &a);
  30.  
  31. switch(a)
  32. {
  33.          case 'p':{
  34.               printf("Vnesite dolzino vektorja!\n");
  35.               scanf("%f", &T1_polar.r);
  36.               printf("Vnesite kot!(kasneje boste izbrali enoto - radiani/stopinje)\n");
  37.               scanf("%f", &kot);
  38.               printf("Vnesite s za stopinje ali r za radiane.\n");
  39.               scanf("%c", &b);
  40.               switch(b)
  41.               {
  42.               case 's':{
  43.                    T1_polar.fi=kot/57.2957795131;
  44.                    break;
  45.                    }
  46.               case 'r':{
  47.                    T1_polar.fi=kot;
  48.                    break;
  49.                    }
  50.               default:{
  51.               printf("Znova zazenite program in vnesite veljavno vrednost\n");
  52.              system("pause");
  53.              return 0;
  54.              }
  55.              }
  56.              T1_kart.x=T1_polar.r*cos(T1_polar.fi);
  57.              T1_kart.y=T1_polar.r*sin(T1_polar.fi);
  58.              printf("x = %f, y = %f, r = %f, fi = %f rad\n",T1_kart.x,T1_kart.y,T1_polar.r,T1_polar.fi);
  59.              break;
  60.  
  61.              }
  62.          case 'k':{
  63.               printf("Vnesite koordinati vektorja!\n\nx= ");
  64.               scanf("%f", &T2_kart.x);
  65.               printf("y= ");
  66.               scanf("%f", &T2_kart.y);
  67.               c=((T2_kart.x*T2_kart.x)+(T2_kart.y*T2_kart.y));
  68.               d=T2_kart.y/T2_kart.x;
  69.               T2_polar.r=sqrt(c);
  70.               T2_polar.fi=atan(d);
  71.               printf("r = %f, fi = %f, x = %f, y = %f\n",T2_polar.r,T2_polar.fi,T2_kart.x,T2_kart.y);
  72.               break;
  73.               }
  74.          default:{
  75.          printf("Znova zazenite program in vnesite veljavno vrednost\n");
  76.              system("pause");
  77.              return 0;
  78.              }
  79.  
  80.  
  81.  
  82.  
  83. }
  84.  
  85.     system("PAUSE");    
  86.     return 0;
  87. }
  88.  
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
Expand|Select|Wrap|Line Numbers
  1. 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').
Jun 22 '14 #1
2 1355
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. int a = 0;
  2.     int b = 0;
  3.  
  4.     switch (a) 
  5.     {
  6.     case 'p':
  7.     {
  8.                 switch (b)
  9.                 {
  10.                 case 's':
  11.                 {
  12.                             //...etc...
  13.                                 break;    // exit switch (b)
  14.                 }
  15.                 } //end of switch (b)
  16.         break; //exit switch (a)
  17.     }
  18.  
  19.     }    
  20.  
Those return 0 statements were put in because the switch statements were not coded correctly, so the program did one case and quit main().
Jun 22 '14 #2
divideby0
131 New Member
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.
Jun 23 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ColinWard | last post by:
Is there any way to skip lines programatically within a listbox as there is in a textbox? I have a listbox on a form which gets populated with the attachments I want to send with an e-mail but they...
7
by: James | last post by:
Hi, Recently my boss has given me a huge program (10000) lines of code. The program has be written in an unmaintainable fashion. It looks like someone who did not know programming has written...
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
6
by: lisa.engblom | last post by:
Hi, I've just started programming in python, and have run into an unexpected problem. I am using python to pull text data from some csv files. I have one file that has the important...
4
by: Alex | last post by:
Hi - I have some code that looks like this: widget_DragDrop(object sender, DragEventArgs e) { //1. some business logic dataobject_changedbvalues()
3
by: Anthony1312002 | last post by:
Hello. I have a scipt the reads and imports a text file into a database table. Below is an example of the text file I'm importing and the script I'm using to accomplish the import. You'll notice at...
7
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
3
by: afrobeard | last post by:
The following following code fails with the failiure:- File "test.py", line 27, in __main__.sanitize_number Failed example: sanitize_number('0321-4683113') Expected: '03214683113' Got:...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.