Ignoring assignment

This is a discussion on Ignoring assignment within the C++ Programming forums, part of the General Programming Boards category; In the given program, value of x does not change as per input given by user. It assigns x= 2 ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Ignoring assignment

    In the given program, value of x does not change as per input given by user. It assigns x= 2 automatically and prints the same. What is the reason ?

    Code:
    #include <iostream>
    
    using namespace std;
    template <class vt>
     
        
    void combio(char *message,vt x)
    {
        
         cout << message;
         cin >> x;
        
    }
    
    int main()
    {
       int x;
       
       combio<int>("please enter a value",x);
        cout << x;
    
    
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    x is passed by value. Pass by reference instead.
    Code:
    void combio(char *message,vt& x)
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2

    Smile

    thanks......that worked.....

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Consider using std::string rather than char* for strings, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Elysia View Post
    Consider using std::string rather than char* for strings, too.
    Specifically..


    Code:
    #include <string>
    void combio(std::string const& message, vt& x)
    Also consider not using system. You could use cin.get to pause, as long as the stream is empty, which is what cin.ignore does. An on-the-go method would be cin.ignore(1000, '\n'); cin.get(); but something like this works:

    Code:
    #include <limits>
    #include <ios>
    
    void pause()
    {
      if(!std::cin.good())
        std::cin.clear();
    
      std::cout << "Press the return key to continue..." << std::endl;
    
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      std::cin.get();
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    Quote Originally Posted by Dae View Post

    Also consider not using system. You could use cin.get to pause, as long as the stream is empty, which is what cin.ignore does. An on-the-go method would be cin.ignore(1000, '\n'); cin.get(); but something like this works:
    Just out of curiousity, what is the reasoning for not using system? Not to be argumentative, just genuinely curious. I use two commands with system, namely CLS and PAUSE, mostly because it is a convienient, one line command, that works correctly each time. If there's a reason not to use it, I should probably learn another way to do that

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    Quote Originally Posted by kalor_alros View Post
    Just out of curiousity, what is the reasoning for not using system? Not to be argumentative, just genuinely curious. I use two commands with system, namely CLS and PAUSE, mostly because it is a convienient, one line command, that works correctly each time. If there's a reason not to use it, I should probably learn another way to do that
    1) It's horribly inefficient. You are starting another program just to wait for user input. That's a lot of overhead compared to the alternative.

    2) it's a security vulnerability, because under certain conditions the programs called by system can be made to refer to a malicious program, instead of the intended one.
    Last edited by King Mir; 09-01-2009 at 08:22 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    3) It's usually not portable since you have to target specific executables located on specific OSes instead of relying on standard C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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