Sign in to follow this  
Kripis

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

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



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

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!

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

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

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

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!

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!

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

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