Whats wrong with my code? ::sigh::

This is a discussion on Whats wrong with my code? ::sigh:: within the C++ Programming forums, part of the General Programming Boards category; I am not good with c++ but i did take one class on it and that was enough to see ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    22

    Unhappy Whats wrong with my code? ::sigh::

    I am not good with c++ but i did take one class on it and that was enough to see it wasnt for me. I made this project for my work it worked fine in codewarrior but i couldnt compile itno an executable because it was student version. So i came to this forum which I had recieved help when was in my class still to find a free compiler then to download Dev C++. When I pasted the exact same code that worked fine in codewarrior into Dev C++ it wouldnt compile

    Any help would be greatly appreciated,

    Charlie



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


    int main ()

    {

    int rmeters;
    float lspeed;
    float rtime;
    float htime;







    cout << endl;
    cout << endl;
    cout << " Run Time Calculator \n";

    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << " Enter the number of meters \n";

    cin >> rmeters ;
    cout << endl;
    cout << " What is the estimated average line speed? \n";
    cin >> lspeed;

    rtime=rmeters/lspeed;
    htime=(rmeters/lspeed)/60;
    cout << endl;
    cout << endl;
    cout << " You have aproximately " << rtime <<" minutes \n";
    cout << " or " << htime << "hours of runtime";

    cout << endl;
    cout << endl;
    cout << endl;

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Worked for me.

    You must include the "return 0;" and the closing "}".

    Also,
    Instead of this:
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    Use
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    And use code tags please

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    22
    Thanks for the help it compiled but now it doesnt show the meters remaining it just closes after I insterd the meters and line speed.

    Any ideas?

  4. #4

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    the last line of ur code before the return 0, add a cin.get();

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    Quote Originally Posted by jlf029
    the last line of ur code before the return 0, add a cin.get();
    I made a little program today at school, showing a friend of mine a little more advanced programming, He's on the "Hello World" first program thingy, and I tried using the cin.get(); , but it didn't work. Why not? I did it on the last line before the return 0;

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Perhaps neither of you have read the FAQ

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    Quote Originally Posted by Salem
    Perhaps neither of you have read the FAQ
    I have, but I'm not familliar with the other things. only cin.get();

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You probably have an return character in the buffer (from when you previously hit enter on your cin), so when it goes to cin.get() it has an enter in it.


    Use:

    Code:
    cin.ignore();
    cin.get();
    Or just do cin.ignore() after every cin input line.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    It'd also be a good idea to use "cout << endl << endl" instead of using a new cout for each endl. Or do as I do, and if you have more than one endl right next to eachother, drop both of them and use "count << "\n\n" ". Same thing as "cout << endl << endl;" except you can get rid of that second <<.

    Here's your code, a bit cleaned up.
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    
    int main ()
    {
    
    int rmeters;
    double lspeed, rtime, htime; //Doubles are better than floats
    
    cout << "\n\nRun Time Calculator";
    cout << "\n\nEnter the distance in meters: ";
    cin >> rmeters ;
    cin.ignore();
    cout << "\nWhat is the estimated speed? ";
    cin >> lspeed;
    cin.ignore();
    
    rtime = rmeters / lspeed;
    htime = ( rmeters / lspeed ) / 60;
    
    cout << "\n\nYou have aproximately " << rtime <<" minutes ";
    cout << " or " << htime << " hours of runtime.";
    
    //Those last few cout << endl; s were useless.
    cin.get();
    return 0;
    }
    Last edited by linkofazeroth; 12-14-2005 at 01:13 PM.
    It's Link, not linkofazeroth. The latter's just my username. The former's my online identity. Thank you.

  11. #11
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >Same thing as "cout << endl << endl;" except you can get rid of that second <<.
    Not quite. std::endl actually flushes the stream too, while '\n' doesn't. To get the similar functionality you'd have to do this:
    Code:
    cout << "\n\n" << flush;
    Which doesn't get rid of the second operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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