Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > getline(cin,string); cout << string; Doesn't Work

Reply
Thread Tools

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

 
 
PulsarSL@gmail.com
Guest
Posts: n/a
 
      03-29-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-29-2007
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


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-29-2007
<> wrote in message
news: oups.com...
> 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.


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      03-29-2007
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
 
Reply With Quote
 
PulsarSL@gmail.com
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 3:38 pm, ricec...@gehennom.invalid (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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: cout vs std::cout Hendrik Schober C++ 7 10-07-2008 12:02 AM
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57