Newbie Question

This is a discussion on Newbie Question within the C++ Programming forums, part of the General Programming Boards category; Hi Folks, back in '99 i wrote a programm in C++ ,a Temperature Converter ,since i jsut started it sure ...

  1. #1
    Karsten
    Join Date
    Sep 2004
    Posts
    8

    Question Newbie Question

    Hi Folks,

    back in '99 i wrote a programm in C++ ,a Temperature Converter ,since i jsut started it sure wasnt perfect ,since then i didnt had done much ( nothing ) in C/C++ ,so now i start all over again .
    Needless to mention i dont have the source code anymore and therefore try to rewrite it .
    Now my Problem ........
    i cant figure out how to read out a single letter from the keyboard ( and compare if it was hit or not ) ,such as C for celsius for example ,does someone can help me please ??

    here some out of my programm i try to accomplished
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    // Named constant definitions:
    const float FRACTION = 1.80;
    
    // Main program:
    
    void main()
    {
    
       char x;
       float fahrenheit;   // fahrenheit temperature
       float celsius;      // celsius temperature
         {
    // program statements follow
    
           cout << "Choose between"<<" (C)elsius"<<" or"<<" (F)ahrenheit \n";
           cin >> x;
    P.S.: me is using the Dev-C++ compiler

    thanks for any help in advance

    Hoschi
    Last edited by Hoschi; 09-20-2004 at 05:13 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    string input;

    cout<<"enter something: ";
    cin >> input;

    now input hold the string that was typed in.

    search these boards and google for standard c++ keyboard input.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    1)
    Code:
    #include <iostream>
    using namespace std;
    
    //no stdlib or conio
    2)
    Code:
    int main()
    {
       //...
       return 0;
    }
    3) Your input code is correct. Then if you want to check for C or F:
    Code:
    if(x == 'C')
    {
       //do whatever
    }
    else if(x == 'F')
    {
       //do whatever
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Karsten
    Join Date
    Sep 2004
    Posts
    8
    thx it really does help ,now i only have one error message ...lol ,
    which is this :
    30 c:\karsten\dev-c_~1\meiner\test1.cpp
    parse error before `else'

    i checked the brakets and it seems fine so wherer the heck is my mistake ??

    Code:
    #include <iostream>
    using namespace std;
    
    
    // Named constant definitions:
    const float FRACTION = 1.80;
    
    // Main program:
    
    int main()
    {
    
       char x;
       float fahrenheit;   // fahrenheit temperature
       float celsius;      // celsius temperature
    
    
           cout << "Choose between"<<" (C)elsius"<<" or"<<" (F)ahrenheit \n";
           cin >> x;
    
    
           if( x == 'C' || x == 'c');
             {
              cout << "Please enter a temperature in degrees Celsius: ";
              cin >> celsius;
              fahrenheit = (celsius * FRACTION ) + 32.0;
              cout << "That is " << fahrenheit << " degrees Fahrenheit" << endl;
              cout.precision(4);
              }
           else if( x == 'F' || x == 'f');
             {
              cout << "Please enter a temperature in degrees Fahrenheit: ";
              cin >> fahrenheit;
              celsius = (fahrenheit - 32.0 ) / FRACTION;
              cout << "That is " << celsius << " degrees Celsius" << endl;
             }
    
     //  system("PAUSE");
    return 0;
    }
    oh and i need a nice exit and loop in case someone like to try the oposite temp ,well i will think of something

    thanks so far
    Hoschi

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Remove the semicolons after the if statement and the else if statement. The semicolon ends the if block, if you remove it, then the braces define the block of code that is run if the if is true, which is what you want.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    You don't need the ; next to the if and the else if statements
    Woop?

  7. #7
    Karsten
    Join Date
    Sep 2004
    Posts
    8
    wow it works thx folks
    now just one more question ,how come this
    system("PAUSE"); cause an error message when i actiovate it ?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    because it it evil just kidding probably because you forgot to include <cstdlib>, but anyways I would use cin.get(); if i were you instead of system calls. put this before your return 0 just like system pause.
    Code:
    cin.ignore();
    cin.get();
    Woop?

  9. #9
    Karsten
    Join Date
    Sep 2004
    Posts
    8
    i will try this thx ,guess there is still alot to learn for me ( again ),
    thanks for all the help i did get here ,i come back again and perhaps i soon can help others too

    thanks
    Hoschi

  10. #10
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi prog-bman,

    Quote Originally Posted by prog-bman
    because it it evil just kidding probably because you forgot to include <cstdlib>, but anyways I would use cin.get(); if i were you instead of system calls. put this before your return 0 just like system pause.
    Code:
    cin.ignore();
    cin.get();

    This isn't the first time I've heard people recommend against system calls (the first was against system("cls")). But I'm not sure why.

    My first instinct is that it must be slower (because it's being accessed via some other source, ie. the OS?). Is this too far off?


    And Hoschi, glad you got things working!

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It requires another program to be loaded and executed, which takes a long time. It's also more system specific than most other things. "cls" is Windows-only, "clear" Unix-only.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I've also heard that it's vulnerable to security exploits, since it requires that a program called 'pause.exe' or 'pause.com' to be run. If that is replaced by something malicious, you unknowingly execute it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Karsten
    Join Date
    Sep 2004
    Posts
    8

    Talking

    Hi Folks ,

    yes i have finisched my "work" on this converter ,but not as i previously wanted ,i switched over to the "swith/case" thingy instead of the if function ,made it easier in the end and of course i did get me some ideas how to manage it from some other example programs ,which is not cheating as i guess
    so here the final version :

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    void Start ();
    void GetResults();
    
    const float FRACTION = 1.80;       // Named constant definitions:
    
    
       char x;
       float fahrenheit;   // fahrenheit temperature
       float celsius;      // celsius temperature
    
       double cel,far,result;   // some variables
    
    void Start()
       {
    
           cout << " You can choose between those :\n";   // select Temp
           cout << " 1. Celsius \n";
           cout << " 2. Fahrenheit \n";
           cout << " 3. or any other Key to Exit \n";
           cin >> x;
           cout << "\n\n";
    
    
            switch (x)
            {
            case '1' : cel;  // getting the choice
            break;
            case '2' : far;
            break;
            default : exit(0);
            break;
            }
            GetResults();
    }
    void GetResults()
    {
              if (x=='1')                                        // Chooce to convert in Fahrenheit
              {
              cout << " You choose Option ( 1 ) \n";             // calculating Celsius in Fahrenheit
              cout << " Please enter a temperature in degrees Celsius: ";endl;
              cin >> cel;
              result = (cel * FRACTION ) + 32.0;
              cout << " " << cel << " degrees Celsuis are " << result << " degrees Fahrenheit\n";endl;
              cout << "\n";
               }
              if (x=='2')                                        // Choose to convert in Celsius
              {
              cout << " You choose Option ( 2 )\n";              // calculating Fahrenheit in Celsius
              cout << " Please enter a temperature in degrees Fahrenheit: ";endl;
              cin >> far;
              result = (far - 32.0 ) / FRACTION;
              cout << " " << far << " degrees Fahrenheit are " << result << " degrees Celsius\n";endl;
              cout << "\n";
              Start();
              }
               else if (x!= '1')
              {
               Start();
              }
               else if (x!='2')
              {
               Start();
              }
               else if (x!= '3')
              {
               Start();
              }
    
    }
    
    
    
    
          // Main program:
     int main()
    {
           cout.precision(4);
           cout << "*************************************\n";
           cout << "***    Temperature - Converter    ***\n";
           cout << "***         Designed by -         ***\n";
           cout << "***  +---- Karsten Mueller ----+  ***\n";
           cout << "***       in September 2004       ***\n";
           cout << "***            Version            ***\n";
           cout << "***             V 1.3             ***\n";
           cout << "*************************************\n";
           cout << endl;
           cout << endl;
         Start();
         return 0;
    }
    thanks again for all your help and ideas ,i sure will continue and learn more while iam doing as well .

    Hoschi

  14. #14
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    CornedBee and Hunter2,

    Thanks for that, it clears things up a lot.


    Hoschi,

    Glad you got your progam running. Just noticed a couple of things, if you don't mind me commenting. I'm still a beginner too but thought I'd pipe up anyway (if I'm wrong one of the more experienced folk can smack my nose for sticking it in where it doesn't belong yet ):

    - your switch/case doesn't seem to be doing anything; that is, it still looks like the if statements are controling the program flow; I don't think you need both... how about creating one function for going from cel to far and another one for far to cel and then calling them from the switch/case?

    - if you still want to go with ifs how about an "if, elseif, else" set instead of "if, if, elseif,elseif,etc."? Might streamline your code a bit.

    -
    Code:
    cout << " 3. or any other Key to Exit \n";
    this actually doesn't seem to exit the program, just takes it back to the start function

    - you've declared a couple of float variables (farhenheit and celsius) but I don't think you've used them in the program anywhere, though maybe I've missed them


    Ok, I'll shut up for now. Thanks for posting your code. Hope this helps a bit (and isn't too anoying ).

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    Thats very well done.
    The next thing you should do is learn how to use pointers to get rid of those ugly global variables.

    also, a function like this
    Code:
    void foo();
    takes unknown amount of arguments. You should change it to.
    Code:
    void foo(void);

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 05:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21