Public Group

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

This topic is 2251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#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;
}

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

##### 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!

##### Share on other sites

#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;
}

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.

 #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; } 

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

##### 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

##### 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

 //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; } 

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 : http://www.cplusplus...rial/variables/

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

##### 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!

##### 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!

Thanks!

##### 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

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

When your post is posted, it turns into this:
 MY CODE GOES HERE
...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 'int' is a number (standing for integer, a math term). You are assigning a letter (the letter 'k') to a number, and are understandably getting weird results. It'd be better to assign to a std::string.
std::string (or just 'string' if you use 'using namespace std') 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:
 #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; } Edited by Servant of the Lord

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632630
• Total Posts
3007521
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!