GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-May-2005, 13:25
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road

Creating options and menus


I'm trying to create a program that will, when you launch it, ask you if you either want to convert the temperature from Fahrenheit to Celsius or Celsius to Fahrenheit. I think im on the right track but the program closes when I put in the temperatrue. For some reason it doesnt get the the part of the code that shows the result.

CPP / C++ / C Code:
#include <iostream>


float Convert(float);
float Change(float);

  using namespace std;

  float TempFer;
  float TempCel;
  int choice;

  int main()
{
  cout << "Please select: \n";
          cout << "(1) Fahrenheit to Celsius.\n";
          cout << "(2) Celsius to Fahrenheit.\n";
          cin >> choice;

          if (choice == 1)
                        {cout << "Please enter the temperature in Fahrenheit: ";
                        cin >> TempFer;
						{TempCel = Convert(TempFer);}
                        cout << "\nHere's the temperature in Celsius: ";
                        cout << TempCel << endl;
						return 0;}

          if (choice == 2)
                        {cout << "Please enter the temperature in Celsius: ";
                        cin >> TempCel;
						{TempFer = Convert(TempCel);}
                        cout << "\nHere's the temperature in Fahrenheit: ";
                        cout << TempFer << endl;
						return 0;}

                else
				{cout << "Sorry, you entered an invalid number.";
				return 0;}
}


float Change(float TempFer)
{
  float TempCel;
  TempCel = ((TempFer - 32) * 5) / 9;
  return TempCel;
}


float Convert(float TempCel)
{
  float TempFer;
  TempFer = (TempCel * 9 / 5) + 32;
  return TempFer;
}
__________________
*****Disclaimer*****

I am in no way a professional programmer nor do I play one on T.V.
  #2  
Old 03-May-2005, 13:56
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
why these lines are in braces? Did you try removing the braces arround them and check?

CPP / C++ / C Code:
{TempCel = Convert(TempFer);}
{TempFer = Convert(TempCel);}

Also, use following structure for if loop

CPP / C++ / C Code:
if (choice == 1)
{
   /* Do something */
} else if (choice == 2)
{
    /* Do something */
} else {
    printf("Invalid choice..Bye\n");
}

return 0;

Or better yet, use "switch"

CPP / C++ / C Code:
switch (choice)
{
   1: /* Do something */ break;
   2:  /* Do something */ break;
   default:  printf("Invalid choice..Bye\n"); break;
}

return 0;
  #3  
Old 03-May-2005, 19:27
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
Quote:
or better yet, use "switch:"
...
yikes! can't be giving incorrect code as examples!
correct switch structure is:
CPP / C++ / C Code:
switch (choice)
{
case 1: //note the "case"
//choice == 1
break;
case 2:
//choice == 2
break;
default:
//choice != 1 && choice != 2
break;
}
also, a common headache with switches is that you declare a variable inside one of the cases, and then you get an error like "jump to case label bypasses declaration of ..." or something similar. The reason for this has something to do with scope that is not important. What is important is that when you declare a variable inside a switch, put brackets around that case. Code says thousand words:
CPP / C++ / C Code:
switch (choice)
{
case 1:
    { //need these
        int Fahren;
    } //without the braces, you will get an error
    break; //you still need a break statement. It can go inside or outside of the braces.
case 2:
//choice is 2
break;
default:
    {
        //choice is neither 1 nor 2
        bool failed = true;
    }
break;
}
  #4  
Old 03-May-2005, 19:45
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
sorry...
  #5  
Old 03-May-2005, 22:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Please read the tutorial on formatting. Bad formatting is sometimes worse than no formatting.

And forget switch. The if works just fine for now. Output your values as soon as you enter them to make sure they are being read properly.
__________________

Age is unimportant -- except in cheese
  #6  
Old 04-May-2005, 13:11
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
Ok, I reformated the code and added a line to read back the input choice but my origional problem still remains. Why is the program ending before it reads back the conversion? Here is the code.

CPP / C++ / C Code:
#include <iostream>


float Convert(float);
float Change(float);

using namespace std;

float TempFer;
float TempCel;
int choice;

int main()
{
  cout << "Please select: \n";  // Gives you the option.
  cout << "(1) Fahrenheit to Celsius.\n"; // 1 or
  cout << "(2) Celsius to Fahrenheit.\n"; // 2
  cin >> choice;
  cout << "Your choice was " << choice << "\n";  // Read back the choice

    if (choice == 1)  // Fahrenheit to Celsius
     {cout << "Please enter the temperature in Fahrenheit: ";
     cin >> TempFer;
	 TempCel = Convert(TempFer);
     cout << "\nHere's the temperature in Celsius: ";
     cout << TempCel << endl;
	 return 0;}

     if (choice == 2) // Celsius to Fahrenheit
     {cout << "Please enter the temperature in Celsius: ";
     cin >> TempCel;
	 TempFer = Convert(TempCel);
     cout << "\nHere's the temperature in Fahrenheit: ";
     cout << TempFer << endl;
	 return 0;}

     else  // Wrong number
     {cout << "Sorry, you entered an invalid number.";
	 return 0;}
}


float Change(float TempFer)  // Actual conversion to Celsius
{
  float TempCel;
  TempCel = ((TempFer - 32) * 5) / 9;
  return TempCel;
}


float Convert(float TempCel)  // Actual conversion to Fahrenheit
{
  float TempFer;
  TempFer = (TempCel * 9 / 5) + 32;
  return TempFer;
}
__________________
*****Disclaimer*****

I am in no way a professional programmer nor do I play one on T.V.
  #7  
Old 04-May-2005, 13:27
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
  #8  
Old 04-May-2005, 13:34
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
For F to C conversion you are calling function Convert() same as for C to F.

Shouldn't you be calling Change() for F to C ?

I tried it on my machine, it seems to be working fine.

Quote:
$ ./temp.exe
Please select:
(1) Fahrenheit to Celsius.
(2) Celsius to Fahrenheit.
1
Your choice was 1
Please enter the temperature in Fahrenheit: 68

Here's the temperature in Celsius: 20
  #9  
Old 04-May-2005, 19:52
KenshuuSei's Avatar
KenshuuSei KenshuuSei is offline
New Member
 
Join Date: Apr 2005
Location: Philippines
Posts: 10
KenshuuSei is on a distinguished road

Just a suggestion, its better to have a single return in main or any function..

CPP / C++ / C Code:
int main()
{
  cout  << "Please select: \n";              // Gives you the option.
        << "(1) Fahrenheit to Celsius.\n";   // 1 or
        << "(2) Celsius to Fahrenheit.\n";   // 2

  cin   >> choice;

  cout  << "Your choice was "                // Read back the choice
        << choice
        << endl;

  if (choice == 1)                           // Fahrenheit to Celsius
  {
    cout  << "Please enter the temperature in Fahrenheit: ";
    cin   >> TempFer;

    TempCel = Change(TempFer);

    cout  << "\nHere's the temperature in Celsius: ";
          << TempCel
          << endl;
  }

  else if (choice == 2)                      // Celsius to Fahrenheit
  {
    cout  << "Please enter the temperature in Celsius: ";
    cin   >> TempCel;

    TempFer = Convert(TempCel);

    cout  << "\nHere's the temperature in Fahrenheit: ";
          << TempFer
          << endl;
  }

  else                                       // Wrong number
  {
    cout  << "Sorry, you entered an invalid number."
          << endl;
  }

  return 0;
}
__________________

"True knowledge is knowing that you know nothing."
  #10  
Old 06-May-2005, 13:28
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
where do I put cin.get?
__________________
*****Disclaimer*****

I am in no way a professional programmer nor do I play one on T.V.
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.