Thread: What is wrong with my code? My first program......

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    What is wrong with my code? My first program......

    Ok so this is my first program ive written. Its a Celsius to Fahrenheit converter. Its the first code example in the book I bought C++ without Fear. I did the code exactly as the book showed, the program loads up and ask me to enter a celsius #... when I enter the number and press enter it closes the dos screen. Heres what my code looks like, does anyone know whats wrong with it? Heres the code:
    Code:
    #include <iostream>
    using namespace std;
          
    int main() {
        double ctemp, ftemp;
                     
        cout << "Input a Celsius temp and press Enter: ";
        cin >> ctemp;
        ftemp = (ctemp * 1.8) + 32;
        cout << "Fahrenheit temp is: " << ftemp;
        
        return 0;
        
    }
    Sorry, im new and I can't see whats wrong. Can anyone help me? Thanks

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    Oh yea the complier im using is Dev C++ 4.9.9.2 beta if that matters.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    You should have read the FAQ. In fact, when you begin and end up on a problem, always read the FAQ because the questions people ask the most often are beginner questions. The simplest solution to solve this problem is to open your program through the console. To do is, click on start and then on run and type 'cmd'. Then, type 'cd directory_of_your_project' and finally 'executable_name' and your program will run just fine. You can also add those two lines at the end of your program (before return 0, which is useless, by the way) :

    Code:
    cin.ignore();
    cin.get();
    The first line says that you want to flush some crap that might be left after using cin and the second one says that you want to accept input through the keyboard. Why do we have to do that ? Because when the program has nothing else to do (no more actions, instructions) it closes. Your program actually calculates what you tell it to and it displays the text as well but since it has no other instructions to follow, it closes. The most common way to avoid this problem is those two lines because they require the user to press enter before the program quits (since there are no other instructions after requiring the user to press enter). The first solution is cleaner, though, because it could annoy some users who are using a console to run your program and having to press enter every time they are done with your program might be annoying and unprofessional. When you are testing your program and want to make a quick test, though, adding those 2 lines is a good way to check the execution of your program without using a console.

    =)

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    Thanks Desolation. So your saying if I add those two lines I can run the program without typing cmd etc? Where would I insert those two lines? Thanks

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    At the end. It keeps your program from closing prematurely as it is still stuck waiting for final input from cin.get().

    But you should really get into the habit of writing code for the command-line -- that's what the program is made for, to be run from a terminal. When you learn about Unix pipes and redirects, you'll fully grasp what we're trying to tell you here. Until then just take our word for it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    why is my book not teaching me to do this if this isnt the correct way? Maybe that comes later in the book?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    I cant seem to get the run cmd way to work. If my file is named convert.cpp and is located in my documents then would it be mydocuments/convert.cpp
    ?

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    I got it!! I inserted this code like Desolation said:
    Code:
    cin.ignore();
    cin.get();
    What I wanna know is why did my C++ without fear not advise me to enter that code at the end so I could run the program? I don't wanna learn info that doesnt work, surely there is a reason why my book didnt teach me this?

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    One thing I do notice is I can only enter one number for conversion then when I press enter again it closes, maybe thats why the book didnt tell me those two lines of code? Thanks alot for the help guys.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What I wanna know is why did my C++ without fear not advise me to enter that code at the end so I could run the program?
    Some development environments pause the program for you. If you run it from the command line, you will actually find that extra "pause" rather irritating.

    One thing I do notice is I can only enter one number for conversion then when I press enter again it closes, maybe thats why the book didnt tell me those two lines of code?
    Change to use:
    Code:
    cin.ignore(1000, '\n');
    cin.get();
    The idea is that the ignore discards whatever remains in the buffer, and then you wait for user input (i.e. the enter). Note that 1000 is not a magic number, but rather some reasonably large number... there are a few possibly better alternatives.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Try adding: #include <conio.h> at the top and getch(); the line before return.

    Code:
    #include <conio.h>
    #include <iostream>
    using namespace std;
          
    int main() {
        double ctemp, ftemp;
                     
        cout << "Input a Celsius temp and press Enter: ";
        cin >> ctemp;
        ftemp = (ctemp * 1.8) + 32;
        cout << "Fahrenheit temp is: " << ftemp;
    
        getch();
        return 0;
    }
    Or Ctrl + F5 with your existing code.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ctrl-F5? The OP's using Dev-C++, not MSVC!

    getch() is a bad idea anyway. It's unportable. See the FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  3. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  4. Where is this program going wrong?
    By Kirby1024 in forum C Programming
    Replies: 8
    Last Post: 09-01-2002, 03:32 AM
  5. whats wrong with my code?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-31-2001, 08:44 PM