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

P

PulsarSL

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
 
J

Jim Langston

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.
 
M

Marcus Kwok

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).
 
P

PulsarSL

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top