473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 '07 #1
4 5915
Pu******@gmail.com 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
Mar 29 '07 #2
<Pu******@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr 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.
Mar 29 '07 #3
Pu******@gmail.com 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.hcontains
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
Mar 29 '07 #4
On Mar 29, 3:38 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
There are no string types defined in <string.h>. <string.hcontains
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 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
1
by: Peter van der Goes | last post by:
Had this question from a student and confirmed the behavior in VC++ .NET 2003: > cout << "Please enter your name: "; > getline(cin,myName); > cin.ignore(100,'\n'); > > The only problem with...
1
by: m3zmeriz3d | last post by:
i typed a function to enter data into a file to be recalled later, now i have done something here and as soon as this function begins it skips the first 'zips' getline and makes it a blank space and...
1
by: l46kok | last post by:
Just wondering, for character of arrays, you can set the delimiter manually like cin.getline(character array, size, delimeter) But for the STL string, apparently, you have to use the following...
10
by: baje | last post by:
//new to c++ //have a pointer char *name; //want to enter a name *name = "dave"; //initialize cout<<"enter a name" cin>>name; cout<<" name entered is"<<name; //get a compiler error
6
by: bryant058 | last post by:
#include<iostream> #include<cstring> #include<string> #include<iomanip> using namespace std; int main() { string s; char *tokenptr; int space;
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.