Compiler won't take my cin

This is a discussion on Compiler won't take my cin within the C++ Programming forums, part of the General Programming Boards category; When I compile this: Code: #include <iostream> #include <fstream> #include <cstdlib> using namespace std; using std::cin; int main(int arcg, char ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Exclamation Compiler won't take my cin

    When I compile this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    using std::cin;
    
    int main(int arcg, char *arvg[]) {
    char* coda;  
    char* wait; 
    ofstream outFile("message1.cod", ios::out);    
        for(;;){
                cin<<coda;
                if(coda == "a")
                wait=("Z");
                else if(coda == "b")
                wait=("Y");
                else if(coda == "c")
                wait=("X");
                else if(coda == "d")
                wait=("W");
                else if(coda == "e")
                wait=("V");
                else if(coda == "f")
                wait=("U");
                else if(coda == "g")
                wait=("T");
                else if(coda == "h")
                wait=("S");
                else if(coda == "i")
                wait=("R");
                else if(coda == "j")
                wait=("Q");
                else if(coda == "k")
                wait=("P");
                else if(coda == "l")
                wait=("O");
                else if(coda == "m")
                wait=("N");
                else if(coda == "n")
                wait=("M");
                else if(coda == "o")
                wait=("L");
                else if(coda == "p")
                wait=("K");
                else if(coda == "q")
                wait=("J");
                else if(coda == "r")
                wait=("I");
                else if(coda == "s")
                wait=("H");
                else if(coda == "t")
                wait=("G");
                else if(coda == "u")
                wait=("F");
                else if(coda == "v")
                wait=("E");
                else if(coda == "w")
                wait=("D");
                else if(coda == "x")
                wait=("C");
                else if(coda == "y")
                wait=("B");
                else if(coda == "z")
                wait=("A");
                else
                break;
    outFile<<wait<<endl;
                }
    cout<<"Message status, SENT"<<endl;
    outFile.close();
    }
    I get an error that says this:
    Quote Originally Posted by Dev-C++_Compiler
    no match for 'operator<<' in 'std::cin << coda'
    Why is it doing that?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    because << should be >> for cin

    Think of the >> or << telling you where the information is going

    cin >> bob
    The information is coming from cin and going to bob

    cout << bob
    The information is coming from bob and going to cout

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > char* coda;
    You'll be better off with char coda[100]; to save you trashing some space which you don't own.

    > if(coda == "a")
    And these need to be strcmp() calls as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Sorry it was so stupit, I allready knew that, I just didn't catch it this time.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    > char* coda;
    You'll be better off with char coda[100]; to save you trashing some space which you don't own.
    Or the good ol' std::string

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or a single char, since that seems to be all you're reading. If you do that, you can also forego the endless if-else chain and do some trickery with the character values. (Although, admittedly, that makes you unportable to systems that don't use a continuous-alphabetics character set, like I believe EBCDIC is.)

    Specifically, you could write:
    Code:
    char coda, write;
    for(;;) {
      cin >> coda;
      if(coda < 'a' || coda > 'z') {
        break;
      }
      wait = 'A' + ('z' - coda);
    }
    Last edited by CornedBee; 04-23-2005 at 04:10 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Intersting way to do it.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    hmm couldn't you use:
    Code:
    char coda, write;
    for(;;) {
      cin >> coda;
      if ( islower( static_cast<unsigned char>(coda) ) )
        write = toupper( static_cast<unsigned char>(coda) ) ;
      else
        break;
      outFile<<wait<<endl;
    }
    Now you get the same affect and you aren't using implmentation specific details

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Thantos
    hmm couldn't you use:
    No, because he assigns 'Z' for 'a', 'Y' for 'b', ...
    Your code assigns 'A' for 'a'.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    opps! Just gave his code a glance over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 05:42 AM
  4. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM
  5. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM

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