i can not cin

This is a discussion on i can not cin within the C++ Programming forums, part of the General Programming Boards category; i can not cin ambiguous overloading Code: cin >> "Text added to input stream here " >> variable >> endl ...

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    i can not cin

    i can not cin

    ambiguous overloading
    Code:
        cin >> "Text added to input stream here " >> variable >> endl ;
    ok i get it you can only have
    Code:
        cin >> variable ;
    why does cin not allow you to add text <constant string> to the input stream like you can in other langs ?

    second cin question
    Code:
    /* testcin.cpp*/
    
    #include "testcin.h"
    #include <iostream>
    
    using namespace std;
    
    // char made global so <in theory> it can be use with the for function inside the header.h 
    // <sarcasmmeow> it only works in theory </sarcasmmeow>
    
        char a;
        char b;
    
    int main() {
              
                cout <<  "input text a  " ;
                cin >>  a ;
                cout << endl ;
                cout <<  "input text b " ;
                cin >> b ;
                cout << endl ;
    
               dtext( &a , &b ) ;
    
    // also if i use   cout << dtext (&a , &b) << endl ; i get errors
    }  // end main
    Code:
     
    /* testcin.h */
    #ifndef TESTCIN_H
    #define TESTCIN_H
    
    #include <iostream>
    using namespace std;
    void dtext ( char *pt , char *ytc ) {
     
        cout << "    *pt ==> " << pt << endl ;
        cout << "    *ytc ==> " << ytc << endl ;
    
        // also as soon as i add a for loop here i loose the vlaues of both variables inside the for loop
    
         for( int i = 0 ; i < 1000 ; i++ ) {
                 cout << "    *pt ==> " << pt << endl ;
                 cout << "    *ytc ==> " << ytc << endl ;
                 cout << endl ;
                 } // end for loop
    
    // after for loop and after loss of value of the variables return answer is not correct
    // see above cout note
    
    } //end dtext
    
    #endif
    output
    input text a g
    input text b v

    *pt ==> gv
    *ytc ==> v

    ...
    note the two letters. it was only supposed to print one. another error. why ? mehow ?
    Last edited by kryptkat; 01-18-2010 at 07:26 AM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Location
    bangalore
    Posts
    2

    hi

    ur compilor may hav data size probs...y dont u declare the variables as long......

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> why does cin not allow you to add text <constant string> to the input stream like you can in other langs ?
    What would you expect it to do?

    >> note the two letters. it was only supposed to print one. another error. why ?
    You are outputting pt the pointer, not the character it points to. You just get (un)lucky that when you output a character pointer it is treated like a string and so all characters are shown one at a time from memory until it finds a null character (which in C is how the end of a string is indicated). You get even (un)luckier for ytc, because apparently the character after it in memory is a null.

    The solution is to not output the pointer, but output the character it points to. Do you know how to dereference the pointer to get the character?

    >> also if i use cout << dtext (&a , &b) << endl ; i get errors
    dtext is a function that returns void. This code outputs the return value of the function, but since the function doesn't return anything it fails to compile. Either call dtext or cout a and b, don't mix the two. If a and b have been changed by the function, just outputting them will show it.
    Last edited by Daved; 01-18-2010 at 10:59 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by kryptkat
    why does cin not allow you to add text <constant string> to the input stream like you can in other langs ?
    Because operator>> was not overloaded to perform such a thing, and for what you want to do (add text to the input stream?) it does not make sense anyway.

    Quote Originally Posted by Daved
    What would you expect it to do?
    I would expect it to attempt to match the object
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    thank you daved that was interesting about the two char output. input formatting was what i was trying to do with the cin overloading.
    Do you know how to dereference the pointer to get the character?
    i am going to say no. but that is what i thought i did. cin << pt ; so i need something like a = pt ; cin << a ?
    in thinking cpp the only thing i recall about dereferenceing is how to change the var ie
    Code:
    int a = 57
    int  *ptra = &a;
    deff
    *ptra = 100 // change value dereff
    dtext sends both char a and b to the function. does something with the chars. then returns the result. to do add result return. the main problem was the loss of the chars a and b when they were sent to the function.

    thank you laserlight. i was reading about vector and think it can be done with vector. and other string functions. after the input is obtained.

    i was shocked to see more than one char on the output. so add "\0" to string after cin ?
    surprisingly it compiled.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    It is not surprising that you get this result. It is to be expected. The values are never lost.
    The call is correct. But how you print the chars is incorrect.
    Remember that the pointer only contains the address where the actual data is stored. Therefore, printing pt will not work as expected. In fact, it works in a rather strange way.
    Usually, in C, strings are represented by a char*, where the pointer points to the first char in the string and the last char in the string is detonated by \0 to mark the end of the string.
    The 'b' in your string just happens to be lying in memory after the 'a'. So cout prints the a, goes forward 1, prints the b and then just happens to find a \0.
    You want to print the char it points to! So you will have to dereference it first! Like so: *pt.
    Or you can use references

    void dtext ( char& pt , char& ytc )
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> so add "\0" to string after cin ?
    No, if you're only trying to do single characters, then don't bother with the '\0', it is just trying to fix faulty code with more faulty code.

    Instead, just dereference pt and ytc when you output them. Hopefully by now you've looked up how to dereference a pointer, but to give you a hint you do it in this code:
    Code:
    *ptra = 100 // change value dereff
    >> dtext sends both char a and b to the function. does something with the chars. then returns the result.
    Is this result a single character, or a change to a and b? If it is a change to a and b, then you don't have to actually return anything, they will get changed automatically. Just call the dtext function in one line and on the next line call cout for a and b.

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    The call is correct. But how you print the chars is incorrect.
    i am getting that.
    Therefore, printing pt will not work as expected. In fact, it works in a rather strange way.
    i am getting that.
    You want to print the char it points to! So you will have to dereference it first! Like so: *pt.
    like
    Code:
     cout << "    *pt ==> " << *pt << endl ;
    ?
    still working on how to deference with out changing value. opened up thinking in cpp ebook but it gave no examples of how. all it said was dreff same way as initialize pointer. a little confused.

    Code:
    void dtext ( char& pt , char& ytc )
    i was going to try that next. opened up the thinking in cpp ebook saw that.
    this is an example prog of the prob. the actual prog is a vigenere prog. char a and char b aka row and column return single char answer.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> still working on how to deference with out changing value.

    Dereferencing and changing the value are two separate things. You dereference a pointer when you want to access the value, but what you do with the value when you access it doesn't matter. So you could dereference to access a value so you can change it, or you can dereference a pointer to access a value just to output it. In this case you are just doing it for output, so it won't change.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Quote Originally Posted by kryptkat View Post
    like
    Code:
     cout << "    *pt ==> " << *pt << endl ;
    ?
    That is correct.
    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.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    I would expect it to attempt to match the object
    Code:
    class RequireMatch
    {
    public:
        RequireMatch( const std::string &pattern ) : mPattern( pattern ) {}
    
    private:
        std::string mPattern;
        friend std::istream &operator>>( std::istream &, const RequireMatch & );
    };
    
    std::istream &operator>>( std::istream &stream, const RequireMatch &match )
    {
        // cleverness goes here
    }
    
    ...
    
    std::cin >> RequireMatch( "This text must be matched" ) >> ...;
    It comes in handy.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by brewbuck
    It comes in handy.
    Yes, in response to a comment you made I posted a similiar working example last year. When I submitted an interest check to the Boost mailing list, Scott McMurray pointed out that it would be simpler if this was actually integrated into appropriate overloads of operator>> instead of being a manipulator (but it would not be so good as overloads outside of the standard library itself due to the overloads possibly not being found by argument dependent lookup).

    Going further off topic, would you like to work with me on a proposal to get this included into the C++ standard library?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    ok got it working. thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin memory leak?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2009, 02:38 AM
  2. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  3. problem with cin
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 01:50 PM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 03:57 PM

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