Thread: Program using while loop, swtich, and for loop

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    Program using while loop, swtich, and for loop

    I need help changing this program

    Program using while loop, swtich, and for loop-capture2-png

    Program using while loop, swtich, and for loop-capture3-png

    To this program

    Program using while loop, swtich, and for loop-capture-png

    Program using while loop, swtich, and for loop-capture1-png

    The first 2 images are what I've already did in a previous program, but I have no idea how to get the loops and switch to work properly. Many attempts and no success. Somebody please help me

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Giving you code would violate our homework policy and would be academically unethical. Also, you wouldn't learn anything except how to copy somebody else's work.

    So please post your actual attempt in [code][/code] tags. Make sure it's properly formatted/indented so we can read it. And read this link too, which you seem to have missed: Announcements - General Programming Boards

    If you are truly lost on loops and switch statements, such that you can't even start this assignment (very, very, very, very, very unlikely) consult your textbook and class notes, and read some online tutorials. Start with a simple program that just has one basic loop (e.g. to print a message 5 times), then work up to more complex loops. You can check here C Tutorial - Learn C - Cprogramming.com for tutorials. Once you get the idea, try working the loop it into your assignment. If you're still stuck, post back here and ask for specific help.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    Code:
    /* Converts a Celsius temperature to Fahrenheit and Kelvin */ 
    #include <stdio.h>
    
    
    #define FREEZING_PT 32.0f
    
    
    #define SCALE_FACTOR (9.0f / 5.0f)
    
    
    int main(void)
    {
    float fahrenheit, celsius, kelvin;
    int m, d, y;
    
    
    printf ("Enter today's date: (yyyy-mm-dd): ");
    scanf ("%d-%d-%d", &y, &m, &d);
    
    
    printf ("\v"); 
    printf ("TEMPERATURE CHART \n");
    printf ("- \t - \t -\t - \t - \t - \t - \t - \t - \t  \n"); //explanation border
    printf ("This chart will display celsius values ranging from 0 to 100 \nand the corresponding fahrenheit and kelvin conversions, with a \nstep of 10 between each celsius temperature. You may use the \nMenu Options below to change the default settings. \n");
    printf ("- \t - \t - \t -\t - \t - \t - \t - \t - \t \n"); //explanation border
    printf ("Menu Options: 0=contine, 1=lower bound, 2=upper bound, 3=step \n");
    
    
    int option;
    
    
    do {
    printf ("Choose Option (0 to continue with set values): \n"); 
    scanf ("%d", &option);
    
    
    switch (option)
    {
    	case 1: printf ("Enter lower bound: ");
    		break;
    	case 2: printf ("Enter upper bound: ");
    		break;
    	case 3: printf ("Enter step: ");
    		break;
    	case 0: printf ("\n");
    		break;
    } while (option != 0); {
    
    
    int c;
    
    
    for (c = 10; c > 0; c++) {
    printf ("%f", c);
    
    
    fahrenheit = (celsius * SCALE_FACTOR) + FREEZING_PT;
    kelvin = celsius + 273.15;
    
    
    printf ("\n");
    printf ("Today's date is: %d/%d/%d\n", m, d, y);
    printf ("Celsius temperature: %.2f\n", celsius);
    printf ("Fahrenheit temperature: %.2f\n", fahrenheit);
    printf ("Kelvin temperature: %.2f\n", kelvin);
    
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    This is what I have so far. I'm more so stuck on the for loop. I've been over the textbook and lecture powerpoint note slides numerous times. It just isn't making sense.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    2 problems, your loop would run forever but you have a return 0; statement at thee nd of it.

    Let's see what you have for each:
    Code:
    for (c = 10; c > 0; c++) { /* c starts at 10, you increment it every loop, so it will always be greater than 0, and loop forever (until it overflows possibly) */
      printf ("%f", c);
      fahrenheit = (celsius * SCALE_FACTOR) + FREEZING_PT;
      kelvin = celsius + 273.15;
    
      printf ("\n");
      printf ("Today's date is: %d/%d/%d\n", m, d, y);
      printf ("Celsius temperature: %.2f\n", celsius);
      printf ("Fahrenheit temperature: %.2f\n", fahrenheit);
      printf ("Kelvin temperature: %.2f\n", kelvin);
      return 0; /* Returning exiting the loop on the first iteration (and the program itself) */
    }
    Due to poor indentation and style practices you have confused your Braces and added the return 0 at the end of your main inside of your for loop (assuming unintentionally..)

    You also need to correct the condition or the increment statement of your for loop. If you want it to exit at some point, perhaps you meant c-- instead of c++ for your increment statement?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please make sure to post properly formatted and indented code next time. It makes it easier for everybody helping you. Most decent code editors offer an auto-indent feature that not only makes your code readable, it helps you find errors like missing curly brackets.

    Always compile at the maximum warning level (-Wall option for gcc, else check your compiler documentation):
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:54:17: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
                     printf ("%f", c);
                     ^
    foo.c:69:13: error: expected declaration or statement at end of input
                 }
                 ^
    foo.c:69:13: error: expected declaration or statement at end of input
    foo.c:69:13: error: expected declaration or statement at end of input
    make: *** [foo] Error 1
    The first one is easy. %f is for printing floats, c is an integer. printf doesn't convert for you. You must pass the right type for the format specifier, otherwise you risk undefined behavior (Question 11.33). Try %d.

    The rest are because your curly braces are all out of whack. You're missing a closing curly bracket on your switch statement, and one at the end of your for loop as well; you have an extra one after the "while" line. Again, auto-indent would make those errors much more obvious.

    Now, a for loop has the following form
    Code:
    for (initial statements; keep looping while this part is true; do this at the end of every loop) {
        All the things you want to do each time through the loop
    }
    Now, let's break down your attempt
    Code:
    for (c = 10; c > 0; c++) {
    }
    That is the magic word. Good, you need that.
    You start with c equal to 10.
    You continue the loop while c is greater than zero.
    Each time you finish the loop body, you increment c, i.e. make it bigger by 1.

    Do you see the problem? How will your loop stop if you start with c greater than zero, and keep making it bigger?

    Before we get back to your actual program, start by working on a program that prints all the numbers from 1 to 10 in order, using a for loop. Once you have that working, see if you can make it print the sequence 1, 4, 7, 10, 13, ... up to 30 (it might not end exactly at 30). This will give you the necessary foundation for your actual program.

    If you have trouble with those, post your attempt back here. Please be more specific in your questions. Tell us what isn't working? Bad output? Crashes? Tell us what you don't understand about for loops. It's much easier for us to give good, useful help that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM

Tags for this Thread