New to coding, needing some help.

This is a discussion on New to coding, needing some help. within the C++ Programming forums, part of the General Programming Boards category; I'm new to coding and I'm trying to make a program that will simply add two numbers together and give ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    Question New to coding, needing some help.

    I'm new to coding and I'm trying to make a program that will simply add two numbers together and give an answer.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    { 
        float a, b, c;
        c = a+b;
        
        cout <<"Please give me two numbers to add" << endl;
        cin >>a>>b;
        cout <<"The answer is "<< c << endl;
    
        return 0;
    }
    Some assistance with this would be appreciated. I'm using Bloodshed's Dev-C++ as a compiler.

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by Matttan8989
    I'm new to coding and I'm trying to make a program that will simply add two numbers together and give an answer.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    { 
        float a, b, c;
        c = a+b;
        
        cout <<"Please give me two numbers to add" << endl;
        cin >>a>>b;
        cout <<"The answer is "<< c << endl;
    
        return 0;
    }
    Some assistance with this would be appreciated. I'm using Bloodshed's Dev-C++ as a compiler.
    the problem is how you're initialising the variable c. You're trying to initialise it to equal the sum of a and b, but you only ask for values of a and b after wards. Remember, C++ is a procedural language, and you have to initialise c only after you get the values of and b.

    something like this
    Code:
     
    
    cin >> a >> b;
    
    float c = a+b;

  3. #3
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    c = a + b doesn't mean anything until you have a + b. Move that down so that c doesn't contain garbage:

    Code:
        cin >>a>>b;
        c = a + b;
    Hope that helps - I'm new too -

    dra beat me to it!

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Oh, all right! Thanks!

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Hmm, I changed the code to this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    { 
        float a, b, c;
    
        
        cout <<"Please give me two numbers to add" << endl;
        cin >>a>>b;
        c = a+b;
        cout <<"The answer is "<< c << endl;
    
        return 0;
    }
    I don't understand the problem...

  6. #6
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Problem? Are you getting a compile error? That code works correctly for me.
    Programming Your Mom. http://www.dandongs.com/

  7. #7
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Can't see the result? You need to add

    Code:
    cin.ignore();
    cin.get();
    All credit to ILoveVectors.

    -JM

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Ah! Thank you everyone =)

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    lol why do i get credit i didnt even get to respond,
    was watching a movie, i think im addicted to these
    boards i check them like every 30 minute,
    even if im watching a movie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 06:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 08:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 05:32 PM

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