• 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
Kripis

Please help, I don't know if it's compiler's problem or my problem!

8 posts in this topic

#include <iostream>
using namespace std;

int main()
{

int a;

cout << "Please type a letter which will then be printed out: ";

cin >> a;

cout << a;

return 0;
}



Aaaaaand I get this: [1] [url="http://i.imgur.com/bZkyV.png"]http://i.imgur.com/bZkyV.png[/url] please help!

EDIT: Sorry I didn't notice that the code thing so so badly done so I just put the code there raw, please help. Edited by Kripis
0

Share this post


Link to post
Share on other sites
There are a couple problems with this.

1. The code sample you're showing there doesn't match your output, so it's impossible to determine whether the problem lies in the code listed, or the code you actually have in your program.

2. The 'out' object overrides the << operator, not ">". So you're going to encounter problems with it.

3. If you're truly outputting the value of "a" before putting something in there, it's an uninitialized local variable, and the value printed to screen is going to be whatever is lying in memory at the address "a" has been created at. Though generally, the compiler won't let you do that.

To get a more a more targeted answer, show the source you're actually using. Just move the console window in your screenshot so we can see what's behind it. Also, in response to your original question, it's ALMOST always the programmer, not the compiler that's causing the problem.

Cheers and good luck!
0

Share this post


Link to post
Share on other sites
[quote name='Kripis' timestamp='1343266901' post='4963133']
#include <iostream>
using namespace std;

int main()
{

int a;

cout << "Please type a letter which will then be printed out: ";

cin >> a;

cout << a;

return 0;
}

[/quote]

1- Use code tags.
2- Why are you asking for a letter when you are declaring an int?
3- Use a proper main function.
4- Make it a habit to initialize local variables in function scope to a default value. Otherwise the compiler fills it with whatever might be lying in the memory at that moment.

[CODE]
#include <iostream>
using namespace std;
int main( int argc, char* args[] )
{
//Buffer
char a[250] = {};

cout << "Please type a letter which will then be printed out: ";
cin >> a;
cout << a;

cin.get();cin.get();

return 0;
}
[/CODE]

If you don't feel at ease with arrays you can also use std::string, that is if you need dynamic length strings. Edited by DZee
-1

Share this post


Link to post
Share on other sites
Thanks @DZee but I'm a C++ newbie (this is "for begginer") and I was wondering if you or anyone else could help me understand your suggestion I mean wth is code tags and some of the functions you put in your code well .. I don't know them :| pls help
0

Share this post


Link to post
Share on other sites
[quote name='Kripis' timestamp='1343270599' post='4963158']
Thanks @DZee but I'm a C++ newbie (this is "for begginer") and I was wondering if you or anyone else could help me understand your suggestion I mean wth is code tags and some of the functions you put in your code well .. I don't know them :| pls help
[/quote]

[CODE]
//Including the iostream library
#include <iostream>

//Using the namespace that allows you to omit std
//in the file scope(As in everywhere in this file including in functions).
using namespace std;

//A default main function.
//Parameters are for passing values to your executable trough the command line.
//argc = Number of arguments(space delimited)
//args = Array of pointers containing all of the command line phrases
int main( int argc, char* args[] )
{
//Buffer
//Declaring an array of char containing 250 spaces(Simply expecting that much).
//Initializing with brackets let's the compiler optimize the initialization.
char a[250] = {};
cout << "Please type a letter which will then be printed out: ";
cin >> a;
cout << a;

//Allows the shell to stay put, depends on your environment. Might need one, or two depending on IDE.
//There are other ways to handle this, as you know program execution runs and will reach the mains end
//very fast so this is just a way to keep your console stalled.
cin.get();cin.get();
return 0;
}
[/CODE]

Basically what you need to understand that C++ is a typed language(As opposed to PHP for example). That means that when you declare an integer, you mean to actually store a certain range of numbers. Different types allow to store a higher and lower value depending on the bytes(int is 4 bytes on most machines).

A char variable allows you to store a single character. I've declared an array of characters in case I'd like to store more than one letter.

Start out with this link : [url="http://www.cplusplus.com/doc/tutorial/variables/"]http://www.cplusplus...rial/variables/[/url]

PS: By code-tags I mean use the forum feature that allows you to get formatted code on the forums. Look at my code and look at yours in the first post. It's easier to read. Edited by DZee
0

Share this post


Link to post
Share on other sites
Nice, thanks! oh and btw I use CodeBlock I don't know if it's a bad IDE or something but it came with the compiler so I said, fk it I'm getting the easier one!
0

Share this post


Link to post
Share on other sites
Kripis,

When you declare a variable you identify what type it is. This tells the compiler how much space to allocate on the stack for your data, and how it should interpret the bits at that location in memory.

In this case, you're declaring a variable of type int, a 16 or 32 bit value designed to hold numbers, and are then trying to assign it a character from the keyboard. This is resulting in it incorrectly interpreting the value in memory.

To remedy this, you can either input a number, such as 12345, which will correctly display in the console, or you can simply change your code to the following:

char a;

This will accept a single character from the keyboard, though not a string, such as "Hello, World!".

Cheers and good luck!
1

Share this post


Link to post
Share on other sites
[quote name='Kripis' timestamp='1343270599' post='4963158']
Thanks @DZee but I'm a C++ newbie (this is "for begginer") and I was wondering if you or anyone else could help me understand your suggestion I mean wth is code tags and some of the functions you put in your code well .. I don't know them :| pls help
[/quote]

Code tags are not part of programming, but part of this forum's (and many other forums') features.
They look like this:
[b][ code ][/b] MY CODE GOES HERE [b][ /code ][/b]
...but without spaces between the brackets.

When your post is posted, it turns into this:
[code] MY CODE GOES HERE [/code]
...which is important when posting an entire file of code, since it puts it in a scroll-box instead of making the web page super long.

To re-iterate what JWalsh and the others are saying, an '[b]int[/b]' is a number (standing for [b]integer[/b], a math term). You are assigning a letter (the letter '[b]k[/b]') to a number, and are understandably getting weird results. It'd be better to assign to a [b]std::string[/b].
std::string (or just '[b]string[/b]' if you use '[b]using namespace std[/b]') is a group of characters (letters, symbols, or numbers). If you want to use std::string, you need to #include the <string> header.

Your code would then look like this:
[CODE]
#include <iostream>
#include <string> //Don't forget the <string> header!

using namespace std;


int main()
{
string text;
cout << "Please type some letters, symbols, or numbers which will then be printed out: ";
cin >> text;

cout << text;

return 0;
}[/CODE] Edited by Servant of the Lord
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