Thread: "I love you" program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    "I love you" program

    so i just started programming and am tottaly loveing it
    (my first problem was to make a program to conver fahrenheit to kelvin and celcius
    if you wan to know here is what i got but thats just if you are curious in how i did it
    (yes i did this by myself using all of my noob c++ knowledge)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        float Celcius,Fahrenheit,Kelvin;
        
        cout << "Enter Fahrenheit degrees to get Kelvin and Celcius";
        cin >> Fahrenheit;
        cin.get();
        
        Celcius=(Fahrenheit-32)/1.8;
             if (Celcius <-273.15) cout << "This temperature is impossible" << endl;
             else
        cout << "Celcius is" << Celcius;
        cout << "" << endl ;// This makes the program eaiser to read
        Kelvin=(Celcius+273.15);
             if (Kelvin<0) cout << "This is below absolute zero" << endl;
             else
        cout << "Kelvin is" << Kelvin << endl;
        cin.get();
    
        return 0;
    }
    but (that was my homework and im done with it) but now i want to write another program
    (just for fun)
    so here is what i want to do
    write a program that ask the user if he/she loves the computer
    ex
    cout << "Hello whats your name?";
    either (cin >> name; ) or (getline (cin, name; )
    but i can't us e
    if or else if or any of those
    becouse i want to cout different answers for different cin's
    any ideas??
    (just for fun nothing else)

    heres what I tried and failed in miserably so far

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string name,answer; // is this right??
      cout << "Hello, What's your name? ";
      cin >> name;
      cout << "Hello " << name << "do you love me?\n";
      cin >> answer;
          if (answer=yes) // and obciously this didnt work
      cout << "I love you too... Marry ME!\n";
           /*else if (blabla
           cout << bla bla
           else if (blab lblablab*/
           // you can see my reasoning but well this is not ints or floats
           // so i have no idea how to approach this
    
      return 0;
    }
    please note im not going for AI just a simple little in and out transaction
    something that my friends and i can mess around with( ahhh im falling in love with programing)

    I had my doubts about if/else working but a man can hope

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    = is assignment, == is test for equality.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Also, a string literal needs to be enclosed in double quotes. For example, you output "Hello, What's your name? " correctly because you put it in quotes. But when you test the answer for equality against the string yes, you forgot to put quotes around yes.

    A note about your first program. The default floating point type in C++ is double. When you type 1.8, that is a double. You are using float, which is a smaller precision. In general, you should just use double unless you have a reason not to, so I would switch your datatypes from float to double in that code.

    Another note is that in C++, it is better practice to declare your variables only when you are ready to use them and can initialize them. For example, the Celsius and Kelvin variables aren't needed until later in the program, so you should declare them there. You should always try to initialize variables when you declare them, so that is another reason to declare them later when you know what you want to initialize them with.

    One last piece of advice is to consider revising your indentation habits. Your code is hard to read, I might revise the indentation like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        float Celcius,Fahrenheit,Kelvin;
        
        cout << "Enter Fahrenheit degrees to get Kelvin and Celcius";
        cin >> Fahrenheit;
        cin.get();
        
        Celcius=(Fahrenheit-32)/1.8;
        if (Celcius <-273.15)
            cout << "This temperature is impossible" << endl;
        else
            cout << "Celcius is" << Celcius;
        cout << "" << endl ;// This makes the program eaiser to read
    
        Kelvin=(Celcius+273.15);
        if (Kelvin<0)
            cout << "This is below absolute zero" << endl;
        else
            cout << "Kelvin is" << Kelvin << endl;
    
        cin.get();
        return 0;
    }
    Last edited by Daved; 08-24-2007 at 08:16 PM.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    thanks

    hey thanks for the indention advice ya guess i should get in a good clean habit early.
    no need for double just a simple program for school(nothing fancy)
    all that it needed to do was calculate fahrenehit to celcius and kelvin
    as for decalring variables until later i undersand that if this was a big long program

    but since its simple i put them right there simply becouse they can be seen along with the rest of the program easily.

    double is the default floating type??? well guess ill start using that now in that instance

    so i tried including == and "yes" but right after it asks "do you love me" and i try and enter in yes or no it closes withough doing anything else??
    thanks for all the advice too really thanks i appreciate it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so i tried including == and "yes" but right after it asks "do you love me" and i try and enter in yes or no it closes withough doing anything else??
    Look at your temperature code. You wrote (or copied from your teacher) these two lines:
    Code:
    cin >> Fahrenheit;
    cin.get();
    What is the cin.get() for? When you enter the temperature, the number and the newline from the enter are placed in the buffer. The cin >> Fahrenheit removes the number, but the newline is left in the buffer. cin.get() removes this newline so it does not affect subsequent input. A more descriptive alternative would be to use cin.ignore(), which in this context would do the same thing. Of course, a possibly even better solution is to run the code from the command line, thus sidestepping the issue altogether.

    The FAQ has some treatment on this topic: How do I stop my Windows Console from disappearing everytime I run my program?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM