Thread: End of user input must be 999

  1. #16
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Spidey View Post
    Well, I'm assuming you've got the first part done since your posting it.
    So, That means that you are reading in input and storing it somewhere right ?
    So, all you have to do is think of a way to not read in more data if the user entered 999.
    Try writing it in english and then work your way into code.

    heres a simple example -

    Ask the user for input.
    Check the input.
    If it is not 999, store it.
    otherwise
    stop asking for input
    .
    should i use "if... else....." to solve it?

  2. #17
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    should i use "if... else....." to solve it?
    Well, there are a number of ways you could solve this problem.
    My point was not to take it what I wrote as a direct translation, but to think about the problem and how you would solve it.

    If you believe you could do it using an if..else statements then go ahead and try it out and see what you come up with. After that think of ways to improve your program or how you could write it using a different approach.

    The best way to learn programing is by actually doing it, and I'm sure you've heard this/will hear this a lot of times.
    So write some code and see where you get with it.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The very basic fundamentals for a programmer is to be able to code logic! This usually means that the programmer can translate flow charts and pseudo code into real code.
    This is a part we cannot help you with! This is the part that YOU must fully understand in order to program any sort of program.
    But it's not difficult. All that is required is that you think about how something is done and that you know about these control statements in the language.
    Do you know about loops and if statements? They control the logic of the program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    thanks guys.. i'd try already.. but, got some errors...
    can u guys help me.. just state wut is wrong..
    i'll try to figure out myself, coz i wanna learn it.. =)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Code:
    #include <stdio.h>
    int main ()
    {
      float average, fahrenheit, Celcius, total;
      int counter;
    
      total = 0;
      counter = 0;
    
    do {
        printf("Enter temperature readings in Fahrenheit (999 to end): ");
        scanf("%f", &fahrenheit);
        counter = counter + 1;
        Celcius = (fahrenheit-32) * 5/9;
        printf("\n%d.Fahrenheit\tCelsius\n", counter);
        printf("%.2f\t\t%.2f\n\n", fahrenheit, Celcius);
      }while (fahrenheit != 999);
       
      if (counter != 0) {
       total = total + Celcius;
       average = total / counter;
       printf("Average is %.2f Celcius\n", average);
       }
      else
       printf("No reading is entered.\n");
    
      return 0;
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    i want the output to be like this:

    Enter temperature readings in Fahrenheit (999 to end): 88 77 80 999
    1. Fahrenheit-------Celcius
    88-------------------30.89
    2.Fahrenheit--------Celcius \*please ignore "-----------" i just want to make the output*\
    77--------------------24.77
    3.Fahrenheit--------Celcius
    80--------------------26.67

    Average 41.65 Celcius
    Last edited by naspek; 07-22-2009 at 06:32 PM.

  5. #20
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    got it!!!

    Code:
    #include <stdio.h>
    int main ()
    {
     float average, fahrenheit, Celcius, total;
     int counter;
    
     total = 0;
     counter = 0;
    
     printf("Enter temperature readings in Fahrenheit (999 to end): ");
     scanf("%f", &fahrenheit);
     while (fahrenheit != 999)
     {
      Celcius = (fahrenheit-32) * 5/9;
      total = total + Celcius;
    
      counter = counter + 1;
      printf("\n%d.Fahrenheit\tCelsius\n", counter);
      printf("%.2f\t\t%.2f\n\n", fahrenheit, Celcius);
    
      scanf("%f", &fahrenheit);
     }
    
     if (counter != 0) {
     average = total / counter;
     printf("Average is %.2f Celcius\n", average);
     }
     else
     printf("No reading is entered.\n");
    
     return 0;
    }
    but, my 'else" isnt workin...
    how can i fix it?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In what way does it not work?

  7. #22
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by tabstop View Post
    In what way does it not work?
    ok~
    the program should exit to else statement if i enter the input without 999..
    but it doesnt go to else statement....

    Code:
    Enter temperature readings in Fahrenheit (999 to end): 77
    
    1.Fahrenheit    Celsius
    77.00           25.00

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have no if, let alone an else, inside your while loop.

  9. #24
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by tabstop View Post
    You have no if, let alone an else, inside your while loop.
    ah ha!! got it tabstop!! gee~ thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Modify to make Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 10
    Last Post: 11-03-2008, 07:25 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM