Jump to content

  • Log In with Google      Sign In   
  • Create Account

taking input directly to an ADT such as a stack.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 11 April 2012 - 11:58 AM

Is it possible to get a single line of int valus (0-9) using the cin >> function to be read into a stack one by one without first storing them in an array?
Like, for example, if I wanted to have:

(some created class) x;
cin >> x;

so if the input was: 012345678901234567890123456789
the stack would be filled with those numbers one digit at a time, like: push(0); push(1); push(2); etc.
I can't seem to find any way to do this other than making x an array/string and then pulling each digit out of that array one by one.

Sponsor:

#2 yckx   Prime Members   -  Reputation: 1209

Like
0Likes
Like

Posted 11 April 2012 - 12:37 PM

I'd read it into a std::string and iterate through the string, but based on your question it seems you'd find that too similar to using an array very do you want to avoid using an array?

#3 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 11 April 2012 - 12:54 PM

I'm doing a project for learning data structures and one of the requirements is we cannot use arrays or the standard template library. The thing is, i've got the general idea of the program down fine except for inputing the data. Isn't a string just a character array? I dunno, I may be overthinking this but if it's possible, i would like to try to do it without strings or arrays.

#4 bd36   Members   -  Reputation: 118

Like
1Likes
Like

Posted 11 April 2012 - 01:06 PM

Since cin is an object of stream could you use the get() function to only get one number out of the buffer at a time?

http://www.cplusplus...am/istream/get/

Not sure if it will work, but looks like it could possibly work, although it may not be the most elegant way

#5 Álvaro   Crossbones+   -  Reputation: 13311

Like
1Likes
Like

Posted 11 April 2012 - 02:03 PM

Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
#include <iostream>
#include <vector>
#include <cctype>

struct MyStack {
  std::vector<int> v;
};

std::istream &operator>>(std::istream &is, MyStack &s) {
  char c;
  while (isdigit(is.peek()) && is >> c)
	s.v.push_back(c-'0');

  return is;
}

std::ostream &operator<<(std::ostream &os, MyStack const &s) {
  for (std::vector<int>::const_iterator it = s.v.begin();
	   it != s.v.end(); ++it)
	os << *it << ' ';

  return os;
}

int main() {
  MyStack x;
  std::cin >> x;
  std::cout << x << '\n';
}


#6 Washu   Senior Moderators   -  Reputation: 5180

Like
1Likes
Like

Posted 11 April 2012 - 02:17 PM

Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):


Alternatively, just use std::stack Posted Image Note that overloading >> for this is a damn silly idea, but...
#include <iostream>
#include <stack>
#include <cctype>

std::istream &operator>>(std::istream &is, std::stack<int> &s) {
  char c;
  while (isdigit(is.peek()) && is >> c)
	s.push(c-'0');

  return is;
}

int main() {
  std::stack<int> x;
  std::cin >> x;
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#7 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 12 April 2012 - 05:06 PM

Thank you :)

#8 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 13 April 2012 - 01:40 AM

I've ran into a problem doing this. Do I need to clear a buffer or something before I call cin >> a second time? My problem is, if I take code like Washu's but in a class and then call cin >> twice, it lets me input numbers for the first cin call but not the second. It skips the second call like its not even there.

for example: (>> is overloaded and is a friend of myclass)

myclass a,b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ;
cin>>b;


the program will run as:
Enter first number: 123456
Enter second number: (program exits with code 0)

#9 rip-off   Moderators   -  Reputation: 8217

Like
0Likes
Like

Posted 13 April 2012 - 02:37 AM

Show us your code for "myclass".

#10 Satharis   Members   -  Reputation: 983

Like
1Likes
Like

Posted 13 April 2012 - 03:56 AM

I've ran into a problem doing this. Do I need to clear a buffer or something before I call cin >> a second time? My problem is, if I take code like Washu's but in a class and then call cin >> twice, it lets me input numbers for the first cin call but not the second. It skips the second call like its not even there.

for example: (>> is overloaded and is a friend of myclass)

myclass a,b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ;
cin>>b;


the program will run as:
Enter first number: 123456
Enter second number: (program exits with code 0)

I usually throw a cin.ignore(); under cin statements because cin only grabs the number off the stream, it leaves the return key on the buffer. If you use cin again directly after it'l grab the \n off the buffer and basically skip over it, making the variable blank.

#11 Álvaro   Crossbones+   -  Reputation: 13311

Like
1Likes
Like

Posted 13 April 2012 - 07:08 AM

Perhaps operator>> should be modified to do something closer to what it does for standard types (e.g., std::string). It probably should skip over whitespace. Not that overloading an operator to do this was a good idea to begin with or anything...

#12 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 13 April 2012 - 01:12 PM

using cin.ignore(256, '\n'); at the end of my function fixed it :) Thank you.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS