Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


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
8 replies to this topic

#1 Kripis   Members   -  Reputation: 132

Like
0Likes
Like

Posted 25 July 2012 - 07:41 PM

#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] http://i.imgur.com/bZkyV.png 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, 25 July 2012 - 08:24 PM.


Sponsor:

#2 JWalsh   Moderators   -  Reputation: 463

Like
0Likes
Like

Posted 25 July 2012 - 08:18 PM

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!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

#3 DZee   Members   -  Reputation: 190

Like
-1Likes
Like

Posted 25 July 2012 - 08:39 PM

#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, 25 July 2012 - 08:45 PM.

I "surf" the web, literally.


#4 Kripis   Members   -  Reputation: 132

Like
0Likes
Like

Posted 25 July 2012 - 08:43 PM

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

#5 DZee   Members   -  Reputation: 190

Like
0Likes
Like

Posted 25 July 2012 - 08:50 PM

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, 25 July 2012 - 08:56 PM.

I "surf" the web, literally.


#6 Kripis   Members   -  Reputation: 132

Like
0Likes
Like

Posted 25 July 2012 - 08:54 PM

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!

#7 JWalsh   Moderators   -  Reputation: 463

Like
1Likes
Like

Posted 25 July 2012 - 08:56 PM

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!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

#8 Kripis   Members   -  Reputation: 132

Like
0Likes
Like

Posted 25 July 2012 - 09:13 PM

Thanks!

#9 Servant of the Lord   Crossbones+   -  Reputation: 20966

Like
0Likes
Like

Posted 25 July 2012 - 10:25 PM

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, 25 July 2012 - 10:56 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





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