Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 19th, 2005, 11:25 AM   #1
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
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:
Originally Posted by Bloodshed DEV C++
C:\Programas\Dev-Cpp\teste dia 1.cpp In function `int main()':
19 C:\Programas\Dev-Cpp\teste dia 1.cpp invalid conversion from `const char*' to `char'
20 C:\Programas\Dev-Cpp\teste dia 1.cpp invalid conversion from `const char*' to `char'
With the following code:
/*
  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
Broax is offline   Reply With Quote
Old Jul 19th, 2005, 11:36 AM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
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
skuinders is offline   Reply With Quote
Old Jul 19th, 2005, 12:06 PM   #3
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
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:
Originally Posted by "Bloodeshed DEV C++
C:\Programas\Dev-Cpp\teste dia 1.cpp In function `int main()':
19 C:\Programas\Dev-Cpp\teste dia 1.cpp expected primary-expression before "const"
19 C:\Programas\Dev-Cpp\teste dia 1.cpp expected `;' before "const"
22 C:\Programas\Dev-Cpp\teste dia 1.cpp `lang' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
Thanks for the help so far, though...
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jul 19th, 2005, 12:30 PM   #4
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
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;
}
2. Typically, you don't use char[] arrays for strings in C++. In C, there is no string data type, only char[] arrays, so you have to use them. But C++ has the string data type, so you would normally use that. Here is the program using string:
#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;
}
Since you didn't seem to be working with strings, I suggested using a char[] array. The char data type is used for representing a single character such as 'a' or '%'. You were assigning a string of characters to a memory location which was expecting a single character. So, by changing it to a char[] array, you can store the string (or array) of characters successfully.
__________________
"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
skuinders is offline   Reply With Quote
Old Jul 19th, 2005, 12:43 PM   #5
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
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
Broax is offline   Reply With Quote
Old Jul 19th, 2005, 12:57 PM   #6
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Quote:
Originally Posted by Broax
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?
Well, you couldn't simply use string and int for everything... sometimes you want to deal with floating point numbers or single characters or the integer value of a char - and the list goes on and on.
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 1:23 PM.
skuinders is offline   Reply With Quote
Old Jul 19th, 2005, 1:15 PM   #7
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
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
Broax is offline   Reply With Quote
Old Jul 19th, 2005, 3:33 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 19th, 2005, 5:13 PM   #9
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
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
Broax is offline   Reply With Quote
Old Jul 20th, 2005, 6:48 AM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
'Course it is. Keep plugging mate - eventually you'll reap the rewards.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC