getline(cin,string); cout << string; Doesn't Work

Discussion in 'C++' started by PulsarSL@gmail.com, Mar 29, 2007.

  1. Guest

    Hi

    I'm working on improving the "Simpletron" computer described in Deitel
    & Deitel's "C++ How to Program" 3rd edition. Check out
    http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're
    curious.

    I'm trying to add my own opcodes for string input/output using the
    string type from string.h

    At the top of my program, directly underneath my definition of the
    normal (signed integer) memory array declaration I've got this:

    "string smem[100];" (without quotes, of course)

    In my switch statements that perform the operations defined by the
    opcodes, I've got this:

    case SIN:
    cout << "s? ";
    getline(cin, smem[opData]);
    break;
    case SOT:
    cout << "Output:" << smem[opData];
    break;

    (note that I have const int SIN = 50; and const int SOT = 51; up with
    my other opcode definitions. opData is equal to the memory location
    specified by the second two numbers in each instruction.)

    Running the program and entering the following program I get this
    output:

    00: 5001
    01: 5101
    02: -9999 [the sentinel to end program entry]

    ---Beginning Program execution---

    s? Joe
    Output:

    ---Program Execution Complete---

    Press any key to continue . . .


    No matter what I do, I can't get it to output the string.

    Any clue what I'm doing wrong?

    Thanks
    Pulsar
    , Mar 29, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm working on improving the "Simpletron" computer described in Deitel
    > & Deitel's "C++ How to Program" 3rd edition. Check out
    > http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're
    > curious.
    > [..]
    > No matter what I do, I can't get it to output the string.
    >
    > Any clue what I'm doing wrong?


    I believe it's covered in the FAQ 5.8.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 29, 2007
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi
    >
    > I'm working on improving the "Simpletron" computer described in Deitel
    > & Deitel's "C++ How to Program" 3rd edition. Check out
    > http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're
    > curious.
    >
    > I'm trying to add my own opcodes for string input/output using the
    > string type from string.h
    >
    > At the top of my program, directly underneath my definition of the
    > normal (signed integer) memory array declaration I've got this:
    >
    > "string smem[100];" (without quotes, of course)
    >
    > In my switch statements that perform the operations defined by the
    > opcodes, I've got this:
    >
    > case SIN:
    > cout << "s? ";
    > getline(cin, smem[opData]);
    > break;
    > case SOT:
    > cout << "Output:" << smem[opData];
    > break;
    >
    > (note that I have const int SIN = 50; and const int SOT = 51; up with
    > my other opcode definitions. opData is equal to the memory location
    > specified by the second two numbers in each instruction.)
    >
    > Running the program and entering the following program I get this
    > output:
    >
    > 00: 5001
    > 01: 5101
    > 02: -9999 [the sentinel to end program entry]
    >
    > ---Beginning Program execution---
    >
    > s? Joe
    > Output:
    >
    > ---Program Execution Complete---
    >
    > Press any key to continue . . .
    >
    >
    > No matter what I do, I can't get it to output the string.
    >
    > Any clue what I'm doing wrong?


    The error is probably somewhre else in your code, which you haven't shown
    us. So I"ll guess and say it's on line 42.

    Most likely, your switch statement is only executing once so SOT is never
    executed, but without seeing your code I can't tell you.
    Jim Langston, Mar 29, 2007
    #3
  4. Marcus Kwok Guest

    wrote:
    > I'm working on improving the "Simpletron" computer described in Deitel
    > & Deitel's "C++ How to Program" 3rd edition. Check out
    > http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're
    > curious.
    >
    > I'm trying to add my own opcodes for string input/output using the
    > string type from string.h


    There are no string types defined in <string.h>. <string.h> contains
    functions for working with C-style strings. The std::string string type
    is in <string> (note: no .h).

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Mar 29, 2007
    #4
  5. Guest

    Re: getline(cin,string); cout << string; Doesn't Work

    On Mar 29, 3:38 pm, (Marcus Kwok) wrote:
    > There are no string types defined in <string.h>. <string.h> contains
    > functions for working with C-style strings. The std::string string type
    > is in <string> (note: no .h).


    oops

    And it's fixed. There was still a \n in the buffer from cin.
    cin.ignore() fixes it.
    , Mar 29, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Poof
    Replies:
    1
    Views:
    372
  2. Fred H
    Replies:
    6
    Views:
    7,385
    Kevin Goodsell
    Jan 23, 2004
  3. Simon Gibson

    cin.getline problems in case

    Simon Gibson, Feb 26, 2004, in forum: C++
    Replies:
    6
    Views:
    580
    Simon Gibson
    Feb 26, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,656
    Alex Vinokur
    Mar 6, 2005
  5. goofolution

    getline(cin, string)

    goofolution, Nov 6, 2006, in forum: C Programming
    Replies:
    1
    Views:
    4,508
    goofolution
    Nov 8, 2006
Loading...

Share This Page