Just learning C++ and am having trouble

This is a discussion on Just learning C++ and am having trouble within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, this is my first time learning a language (apart from HTML and CSS) and I am having a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    Just learning C++ and am having trouble

    Hey everyone, this is my first time learning a language (apart from HTML and CSS) and I am having a few problems. I have read the first few parts of the tutorial on this site, and I decided to create a basic program that multiplies two numbers that the user types in.

    I am having a problem with outputting text. Here's the code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
     float number1, number2, a;
    
     cout<<"Please enter a number: ";
     cin>> number1;
     cin.ignore();
     cout<<"What number would you like to multiply " << number1 << " by: ";
     cin>>number2;
     cin.ignore();
     cout<< "" <<number1<< " multiplied by " << number2 << "equals" (number1 * number2);
     cin.get();
    
     return 1;
    }
    According to my compiler, Dev-C++, I cannot use "equals" as a function, although I just want this text to be displayed on the screen.

    What is it I'm doing wrong?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    Code:
    cout<< "" <<number1<< " multiplied by " << number2 << "equals" (number1 * number2);
    treat (number1 * number2) using the same technique as you did with your other variables, number1 and number2

  3. #3
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    a simpler easier way would be something like this;

    Code:
    float Number1, Number2;
    
    cout << "First Number : ";
    
    cin >> Number1;
    
    cout << "To be multiplied by : ";
    
    cin >> Number2;
    
    cout << Number1 << " x " << Number2 << " = " << Number1 * Number2 << endl;

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    Quote Originally Posted by goldz
    Code:
    cout<< "" <<number1<< " multiplied by " << number2 << "equals" (number1 * number2);
    treat (number1 * number2) using the same technique as you did with your other variables, number1 and number2
    The thing is, (number1 * number2) still outputs the correct product of number1 and number2, but the thing is my compiler wont let me have the word "equals" in between <<number2<< and (number1 * number2). I just need to know how to have the word "equals" display properly.

  5. #5
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    you forgot the '<<' at the end.

    edit: in between the "equals" << ( etc) << endl;

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    what is endl?

  7. #7
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by Timbo8
    what is endl?
    just the end of the line, it makes sure that anything else displayed goes on a new line.

    edit: you dont really need it on the end one though, but you do need the extra <<'s

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    thanks for your help i have it working now. I dont need the ( and ) around the number1 * number2, but i needed << and <<, but i also need \n, which also puts the next text ona new line. So the final code is
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
     float number1, number2;
    
     cout<<"Please enter a number: ";
     cin>> number1;
     cin.ignore();
     cout<<"What number would you like to multiply " << number1 << " by: ";
     cin>> number2;
     cin.ignore();
     cout<< "" <<number1<< " multiplied by " << number2 << " equals " << number1 * number2 << "\n";
     cin.get();
    
     return 1;
    }

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    endl is a manipulator for output streams (like cout, or an instance of ofstream). it outputs the newline character - '\n' and then flushes the stream.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    so at the end do i put endl or \n, or both?

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Either endl or '\n' are perfectly fine. I think most of the time endl is just used to add clarity to the code (since there's rarely a case that I can think of when 'cout' needs flushing)

Popular pages Recent additions subscribe to a feed

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