Thread: completed a program; desire input

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Question completed a program; desire input

    /*Write a program that gives the user three choices:
    Convert from Fahrenheit to Celsius, convert from Celsius
    to Fahrenheit, or quit. If the third choice is selected,
    the program should prompt the user for either a Fahrenheit
    or Celsius temperature, as appropriate, and then calculate
    and display the corresponding temperature. Use the
    conversion equations: F = (9/5) C + 32 and C = (5/9) (F - 32) */

    /* Algorithm:
    A. Output a menu such as:
    Temperature Conversion Program
    Choose from the following options:
    1. To Convert from Fahrenheit to Celsius
    2. To convert from Celsius to Fahrenheit
    3. To Quit
    B. Store selection (store intergers 1, 2, 3)
    with a cin statement
    C. If the selection is 1 prompt the user for
    fahtemp, store fahrenheit, calculate celcius
    and output celcius
    D. if selection is 2, store celcius, calc fah,
    and output fah
    E. If selection is 3, quit the program


    #include <iostream.h>
    #include <iomanip.h>

    int main()
    {
    char selection;
    float var1, var2, var3, temp, fahren, celsius, quit;
    var1 = 1;
    var2 = 2;
    var3 = quit;

    //I am not sure how to handle process a "the menu portion."
    //I know this is not right, but here's my effort.
    //any suggestions
    cout << "Temperature Conversion Program"
    cout << "\nChoose from the following options: ";
    cout << "\nEnter a 1 if the temperature is in Fahrenheit";
    cin >> var1;
    cout << "\nEnter a 2 if the temperature is in Celsius";
    cin >> var2;
    cout << "\nEnter a 3 to quit the program";
    cin >> var3;

    cout << setiosflags (ios::fixed)
    << setiosflags (ios::showpoint)
    << setprecision (2);

    //I am confused how to set the program to quit if 'q' is selected
    if (selection== '1)
    {
    celsius = (5.0 / 9.0) * (temp - 32.0);
    cout << "\nThe equivalent Celsius temperature is "
    << celsius << endl;

    }
    else
    {
    fahren = (9.0 / 5.0) * (temp + 32.0);
    cout << "\nThe equivalent Fahrenheit temperature is "
    << fahren << endl;
    }
    //I am confused how to set the program to quit if 'q' is selected

    return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What exactly are you asking? I can tell that your program is kinda messed up. "Selection" has an undefined value when testing it and your menu is plain weird.
    Try something like this:
    Code:
    cin >> Selection;
    switch(Selection)
    {
       case 'C':
       case 'c':
          //Celcius
          break;
    
       case 'F':
       case 'f':
          //Fahrenheit
          break;
    
       case 'Q':
       case 'q':
          //Quit
          break;
    
       default:
          //Handle invalid input
          break;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >//I am confused how to set the program to quit if 'q' is selected
    Code:
    cout<<"1) Fahrenheit"
        <<"2) Celsius"
        <<"3) Quit"
        <<end;
    cin>> selection;
    
    if (selection == 1)
    {
        // Calculate Celsius
    }
    else if (selection == 2)
    {
        // Calculate Fahrenheit
    }
    else
    {
        // Assume anything else is 3
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. provide input to another program
    By oncemyway in forum C Programming
    Replies: 14
    Last Post: 07-31-2005, 04:12 PM
  5. Input via the serial port for a C program
    By Anthony in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 02:19 PM