![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
|
Invalid Conversion??
Hello all!
I'm starting c++ from scratch and after the 2nd lesson I decided to make a nice little program just to test what I've learned so far... But I got the following error: Quote:
/*
Name: Test - Day 2
Copyright: Broax.com
Author: Broax
Date: 19-07-05 01:57
Description: Simple C++ program that tests my knowledge of simple program structure, variables, data types and constants.
*/
//Pre-Processor Instructions:
#include <iostream> //iostream: Library that allows Input and Output of data into a stream of information.
#define dc42 "Forty-Two" //Defines a value as a constant macro.
//Global Variable Declaration (Obsolete in this case)
int my_age = 20;
int main () //start of "main" function:
{
//Local Variables:
const char lang = "C++";
char me = "Broax";
//instructions:
std::cout << me << " is learning " << lang << " at the age of " << my_age << "...\bWhy?...\b\t" << dc42;
//End of instruction set:
return 0;
//End of function:
}Anyway, I've ran it through my head a million times, but I don't understand where I went wrong!! I've left the comments in so that you can see what I was trying to do when... BTW, I didn't use the "using namespace std" thing, because that's a bit down the road of the tutorial and I thought I'd leave it 'till then. I don't like to do stuff that I don't fully understand... One last thing... Just for the fun I've made this! ![]() #include <iostream>
int teste = 42;
int main ()
{
std::cout << "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc2\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";
std::cout << "\xb3 Check out the cute little \xb3 Box!!! \xb3\n";
std::cout << "\xc0\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc1\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xd9\n";
std::cout << "\n";
std::cin.get();
return 0;
}
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
what you want is a char array, since a char is only a single character.
const char lang[4] = "C++"; char me[6] = "Broax";
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
|
Well, I took the liberty of jumping a few chapters in order to get to the array section so that I could understand what you where saying, but from what I get from this tutorial, arrays are only good to make table-like variable storage.
Maybe I could be mistaken, but so far I don't see a connection. Although I would appreciate if you could care to explain it a little.. ![]() In any case I tried exactly as you said and got the following errors: Quote:
![]()
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
1. My fix works fine for me. Here is all of the code:
#include <iostream>
#define dc42 "Forty-Two"
int my_age = 20;
int main ()
{
const char lang[4] = "C++";
char me[6] = "Broax";
std::cout << me << " is learning " << lang << " at the age of " << my_age << "...\bWhy?...\b\t" << dc42;
return 0;
}#include <iostream>
#define dc42 "Forty-Two"
int my_age = 20;
int main ()
{
std::string lang = "C++";
std::string me = "Broax";
std::cout << me << " is learning " << lang << " at the age of " << my_age << "...\bWhy?...\b\t" << dc42;
return 0;
}
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Sorry, sorry, sorry!
My mistake! I typed const char[4] lang instead of const char lang[4].:p The other error I made was confusing string with int.... lol I'm such a noob! ![]() But just answer one last question... Why are there so many data types? like short, long, int, string, etc. Is it to save memory on larger and more complex programs? Why not just use string and int for everything? Thanks for the help, though! ![]()
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Quote:
Things like long, short, float, double are basically used to limit the amount of memory assigned to store the value. These can be used to save memory or sometimes limit the size or precision of a number. -This is a very simple response as I expect you will figure out the more subtle features of data types in your studies. Happy hacking!
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 Last edited by skuinders; Jul 19th, 2005 at 2:23 PM. |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
Thank you very much!
Actually that's the kind of answer I wasw hopping for, since I can't understand much of the more advanced concepts but it shows me that it's usefull to learn that stuff...Thanks again, mate! ![]()
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Weakly typed languages that can do everything with a single "type" don't make code shorter, they make it longer. While it IS easier on a neophyte programmer, thar be b'ar in them woods....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
That kinda makes me think if I can ever be a good programmer. I mean.. I have a little project in mind that I'd like to make some day, but I'd like it to be useable.
It doesn't need to be Doom3 like. But I really enjoy those simple, yet highly adictive games. For that you need excelent gameplay which ussualy needs a great programmer. I'd also like to inovlve myself with a couple of projects (in sourceforge for example), but I'd like to be *at least* an average addition to the team, and not a drag pulling the whole project down. I mean... the whole programming scene sometimes gets a bit intimidating. In "real life* I'm a firefighter/nursing student and although I'm pretty confortable with PC hardware and windows in most aspects (and strugling to change to Linux due mainly to the open source filosophy), I'm afraid I'm just not cut out to be a programmer... The same thing goes to linux... It just looks so tough to learn... =/ Do you think it's possible for the average joe to learn decent programming skills by himself?
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|