How do I pass a whole number directly to print

This is a discussion on How do I pass a whole number directly to print within the C++ Programming forums, part of the General Programming Boards category; How do I send this number to a function and print it to screen.: Code: cout << "The number: " ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    How do I pass a whole number directly to print

    How do I send this number to a function and print it to screen.:

    Code:
       cout << "The number:  " ;
       MyNumber.print(300);

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Why would you need to send it to a function to print ?
    ..but the function could be like..
    Code:
    void MyNumberClass::print(int x)
    {
        std::cout<<x;
    }
    but a simple statement
    Code:
    cout<<"The Number: "<<x;
    would be enough...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I just got use to doing it with pointers and and such a few days ago. It's no fun taking the easy way out with cout all the time. Now I can add this to my experence. It's the easy stuff that seems the hardest and I think I did this once before. Anyway, once I get it to work, I never forget it... Thank you

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    If you want to try out extra features, doing them on what does not require them warrant later problems. Instead find some problems to solve which does need them.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I learn a lot though trial and error even when I might not solve the original problem, like now, but what I did learn will be re-tested doing my next adventure. I been at the finish line for hours, it's hard not to Press "F9" to continue.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    It's really scary how we can create bugs in our project months ago and still might not realize it today.

    Printing a class - C / C++ answers


    I think I'm getting close. This got to be the preferred method because I can set a value like (300) where before it would not even compile. Problem now is it don't show the output. Could you tell me what do I need to add or do to make the result print to screen. My virtual print function is in the base class for my two derived classes, so this is my only issue.

    Thanks in advance

    Code:
    class NumberA : public NUMBER
    {
    public:
        int valueA;
    
            NumberA()
            { 
            valueA = 0;
            }
            virtual int print(int x)
            {
             return valueA;
            }
    };

    Code:
    int main()
    {
     NumberA numberA;
    
      cout << "Number-A: " ;
       numberA.print(300);
    Last edited by sharris; 04-16-2011 at 03:19 PM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    You mean to tell me that nobody got a clue or even interested in one of the greatest C++ questions EVER!!! I needed no help up until now. This is my creation from scratch with this additional new function I'm trying to make work. Those who know me knows I always add something fancy to my projects, every-week and that is what usually, ALWAYS what my questions related to.

    I might not be an expert but I have since enough to know, that what I am missing, is so minor it's a pitty that these expert members here don't have a clue or don't want to share or simply want to know why sharris is always trying new or silly ways of doing things, but they will tell me the why not's, but they never will explore. That a pitty too.

    sharris, sharris, it seems that you are embarking on some new and unknown technology again???

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I got it! I got IT!!!

    Code:
    #include <iostream>
    using namespace std;
    
    
    class NumberA
    {
    public:
        int valueA;
    
            NumberA()
            { 
            valueA;
            }
            virtual int print(int valueA)
    {  cout << valueA;  }   };
    
    
    int main()
    {
    
     NumberA numberA;
    
      cout << "Number-A: " ;
       numberA.print(300);
       
           cout << endl;
           cout << endl;
    
    cin.get();
    	return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print big amount of number?
    By zzatan in forum C Programming
    Replies: 9
    Last Post: 10-02-2009, 07:46 PM
  2. How to print directly from compile screen?
    By mcotter222 in forum C Programming
    Replies: 65
    Last Post: 04-23-2008, 03:05 PM
  3. Cannot Pass String to Main to Print
    By alphascrapper in forum C Programming
    Replies: 9
    Last Post: 07-10-2007, 12:07 AM
  4. Print Maximum number
    By rmathus in forum C Programming
    Replies: 3
    Last Post: 10-07-2003, 06:33 AM
  5. Replies: 0
    Last Post: 03-28-2003, 07:20 AM

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