• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Kevn

taking input directly to an ADT such as a stack.

11 posts in this topic

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

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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?

[url="http://www.cplusplus.com/reference/iostream/istream/get/"]http://www.cplusplus...am/istream/get/[/url]

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

Share this post


Link to post
Share on other sites
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
[code]#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';
}
[/code]
1

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1334174605' post='4930351']
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
[/quote]

Alternatively, just use std::stack [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] Note that overloading >> for this is a damn silly idea, but...
[code]#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;
}
[/code]
1

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
[quote name='Kevn' timestamp='1334302853' post='4930889']
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)
[/quote]
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.
1

Share this post


Link to post
Share on other sites
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...
1

Share this post


Link to post
Share on other sites
using cin.ignore(256, '\n'); at the end of my function fixed it :) Thank you.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0