C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-13-2006, 10:52 PM   #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
coreyt1111 is offline   Reply With Quote
Old 11-13-2006, 10:57 PM   #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.
coreyt1111 is offline   Reply With Quote
Old 11-13-2006, 11:03 PM   #3
Registered User
 
Join Date: May 2006
Posts: 894
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.

=)
Desolation is offline   Reply With Quote
Old 11-14-2006, 06:27 AM   #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
coreyt1111 is offline   Reply With Quote
Old 11-14-2006, 06:33 AM   #5
Registered User
 
Join Date: Mar 2006
Posts: 726
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;}
jafet is offline   Reply With Quote
Old 11-14-2006, 06:54 AM   #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?
coreyt1111 is offline   Reply With Quote
Old 11-14-2006, 06:56 AM   #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
?
coreyt1111 is offline   Reply With Quote
Old 11-14-2006, 07:02 AM   #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?
coreyt1111 is offline   Reply With Quote
Old 11-14-2006, 07:03 AM   #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.
coreyt1111 is offline   Reply With Quote
Old 11-14-2006, 07:41 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,319
Quote:
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.

Quote:
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-14-2006, 01:52 PM   #11
MB1
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.
MB1 is offline   Reply With Quote
Old 11-14-2006, 02:03 PM   #12
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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