HorizonForumsone horizon
Log inSign up

[ms] · Est. May 2002

Music madethis place,we just kept itgoing...

One durable person. One social graph. Many communities—and every way people speak, share, listen, write, gather and remember.

Forums / Computers / Electronics / Games

Any C++ programmers out there?

11 durable postsStarted 2011-03-07Latest 2011-03-09
#95626Post 1 of 11

I've started learning this recently and received a book "C++ from the ground up" (I was also playing around with c code for a little while previous). Using Visual Studio Pro 2010 which may seem a little overkill when I'm going to be compiling single sources of code but I like the IDE and it's suiting me fine.

My query is this. I've had to make a few changes to the examples given in the book to make it compile and I'm just trying to find out why this is, the book was published in 1994 so I'm wondering has C++ changed somewhat since then, is it the compiler I am using, or is it to do with Windows itself?

Anyhoo, one of the code examples in the book is:

[code] #include <iostream.h>

main() { int gallons, litres;

cout << "Enter the number of gallons: ";
cin >> gallons; // takes an input from the user

litres = gallons * 4; // convert to litres

cout << "Litres: " << litres;

return 0;

} [/code]

And to make it compile and work I've had to amend it to this:

[code] #include // as opposed to iostream.h using namespace std; // this has been added

int main() // else reports back with error { int gallons, litres;

cout << "Enter the number of gallons: ";
cin >> gallons; // takes an input from the user

litres = gallons * 4; // convert to litres

cout << "Litres: " << litres;

return 0;

} [/code]

The std namespace is quite important but if it wasn't needed back in 1994, why should I need to add it now? Surely all previous code should still compile regardless, yes? However, all this helps my understanding and learning and I like to know why things aren't working.

#1470491Post 2 of 11

Re: Any C++ programmers out there?

What line does the compiler error on? I assume the first line is what's causing the error

There are a lot of differences between then and now. This will make a dos/command-line app... is that what you're intending to do?

#1470494Post 3 of 11

Re: Any C++ programmers out there?

There's quite a few differences in the code in the book and the changes I've made, from what I've read there were revisions around 1998 to the C++ standard so it may be I need to get a newer book, although so far I've been ok with the examples as long as I do the same sort of changes here and there.

Right now I'm in a learning process. The last time I ever programmed my own stuff was in BASIC and that was around 15 years ago! So I reckon command line stuff is poss the easiest way to grasp the basics of how C++ works, communicates with the system and such before I move on to trying to develop GUI based programs. Unless you think otherwise?

#1470547Post 4 of 11

Re: Any C++ programmers out there?

<iostream.h> is different from and some time after 1994 it was deprecated. The second one is apparently more 'standards compliant' and puts things in namespaces which are awesome.

[CODE]#include [/CODE] gives you access to std::cout and all the others [CODE]using namespace std;[/CODE] puts them all in the global namespace so it is just cout.

I'm not a C++ programmer, but as a Python man I don't like my namespaces being polluted and the above is similar to a wildcard import which is bad but you can also do [CODE]using std::cout[/CODE] which will only bring cout into the namespace, but that may not be the way C++ people do it...?

In theory, if your system has a legacy iostream.h somewhere the first will compile with some warnings maybe:

[CODE]warning #1224: #warning directive: This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the <X.h> header for C++ includes, or instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.[/CODE]

#1470552Post 5 of 11

Re: Any C++ programmers out there?

Cheers for the response there. If I use [code] #include using namespace std; [/code]

to the examples then it seems to do the trick for the most part. Maybe I should just get a more up to date guide.

Oooh, one more thing, does the "main()" now have to have int, float, etc prefixing it to work? All the code examples just have main() on it's own which doesn't work.

#1470557Post 6 of 11

Re: Any C++ programmers out there?

I recommend PHP instead :) It's web-based and requires no compiling, so the entire world is your audience instead of trying to convince someone to download/install something! It's what I created our [ms] DJ Database in. It's very similar to C, it doesn't require casting any variables/strings, you don't need to include anything to use the commands, and because it outputs HTML you're essentially making web-based command line apps that are outputting text (html).

Here's a snippet of the DJDB code that builds a list of artists & photos (output would be what you'd see [url=http://www.mercuryserver.com/forums/djdb.php?item=member&id=168]here[/url]):

[code]$height=250; $item_results_count=mysql_num_rows($items_sql_results); if ($item_results_count==3) $height=200; if ($item_results_count>3) $height=140;

if ($item_results_count >0) { //Tagged DJs exist
$tag_table.='

';

while ($item_record = mysql_fetch_array($items_sql_results, MYSQL_NUM)) {
	$item_photo=get_picture_path($item_type,$item_record[0]).$item_record[2];
	$i++;
	if ((($i-1) % 5 == 0) && ($i>1)) $tag_table.='</tr><tr>'.$n;
	$tag_table.='<td>'.($i>5?'<br>':'').'<center>';
	$tag_table.='<a href="'.$page_name.'?id='.$item_record[0].$item_param.'"><img src="'.$item_photo.'" style="max-width: '.$height.'px; max-height: '.$height.'px; border: 2px #414141 solid;" hspace=10><br>';
	$tag_table.=$item_record[1].'</a>';
	if ($mark_favorites) if ($item_record[3] != NULL) $tag_table.=' <img src="images/misc/subscribed.png" title="You are subscribed to this '.$item_type.'">';
	if ($is_djdb_editor_stored && $untag_from!='') $tag_table.=' <a href="'.$page_name.'?action=submit&'.$untag_params.$item_record[0].'"><img src="djdb/delete.png" title="Remove this '.$item_type.' from this '.$untag_from.'"></a>';
	$tag_table.='</center></td>'.$n;
}	

$tag_table.='</tr></table></center></div>';

} else { //No tagged DJs exist if ($no_results_message) $tag_table.='No '.$item_type.'s are tagged to this '.$untag_from.'.'; } return $tag_table; [/code]

i don't know if this means anything to you or helps push my message, but it kind of shows that it has a text-only type attitude and its language is similar to your C++ command line approach :)

#1470561Post 7 of 11

Re: Any C++ programmers out there?

Used php quite a few times for various websites I've ran, so it's not totally alien to me. However it's nowhere near powerful enough for things I may be wanting to try out in the future. I'll be wanting to build applications from the ground up and also have the knowledge to write something should I need my computer to something for me.

#1470592Post 8 of 11

Re: Any C++ programmers out there?

def want to take some classes next semester. any other languages vital to know right now? have only got algebra, geo and trig done, but what else would help significantly?

#1470602Post 9 of 11

Re: Any C++ programmers out there?

[QUOTE=Simon Preston;934687]Maybe I should just get a more up to date guide.[/QUOTE]

Probably this is the best solution :) I think I used "Thinking in C++" back in the day, but have forgotten most of it now. Fraid I don't have any other recommendations.

[QUOTE=Simon Preston;934687]Oooh, one more thing, does the "main()" now have to have int, float, etc prefixing it to work? All the code examples just have main() on it's own which doesn't work.[/QUOTE]

I think, at least from convention but probably standards, it should be "int main" so that you return your EXIT_SUCCESS and EXIT_FAILURE (which are usually just 0 and 1) when the program terminates and keep the shell happy.

PHP is just hideous (imo, of course)! For anything scripting it is Python all the way for me. If done properly it is beautiful, powerful and quite easy.

#1470812Post 10 of 11

Re: Any C++ programmers out there?

[QUOTE=picklemonkey;934692]I recommend PHP instead :) It's web-based and requires no compiling, so the entire world is your audience instead of trying to convince someone to download/install something! It's what I created our [ms] DJ Database in. It's very similar to C, it doesn't require casting any variables/strings, you don't need to include anything to use the commands, and because it outputs HTML you're essentially making web-based command line apps that are outputting text (html).

Here's a snippet of the DJDB code that builds a list of artists & photos (output would be what you'd see [url=http://www.mercuryserver.com/forums/djdb.php?item=member&id=168]here[/url]):

[code]$height=250; $item_results_count=mysql_num_rows($items_sql_results); if ($item_results_count==3) $height=200; if ($item_results_count>3) $height=140;

if ($item_results_count >0) { //Tagged DJs exist
$tag_table.='

';

while ($item_record = mysql_fetch_array($items_sql_results, MYSQL_NUM)) {
	$item_photo=get_picture_path($item_type,$item_record[0]).$item_record[2];
	$i++;
	if ((($i-1) % 5 == 0) && ($i>1)) $tag_table.='</tr><tr>'.$n;
	$tag_table.='<td>'.($i>5?'<br>':'').'<center>';
	$tag_table.='<a href="'.$page_name.'?id='.$item_record[0].$item_param.'"><img src="'.$item_photo.'" style="max-width: '.$height.'px; max-height: '.$height.'px; border: 2px #414141 solid;" hspace=10><br>';
	$tag_table.=$item_record[1].'</a>';
	if ($mark_favorites) if ($item_record[3] != NULL) $tag_table.=' <img src="images/misc/subscribed.png" title="You are subscribed to this '.$item_type.'">';
	if ($is_djdb_editor_stored && $untag_from!='') $tag_table.=' <a href="'.$page_name.'?action=submit&'.$untag_params.$item_record[0].'"><img src="djdb/delete.png" title="Remove this '.$item_type.' from this '.$untag_from.'"></a>';
	$tag_table.='</center></td>'.$n;
}	

$tag_table.='</tr></table></center></div>';

} else { //No tagged DJs exist if ($no_results_message) $tag_table.='No '.$item_type.'s are tagged to this '.$untag_from.'.'; } return $tag_table; [/code]

i don't know if this means anything to you or helps push my message, but it kind of shows that it has a text-only type attitude and its language is similar to your C++ command line approach :)[/QUOTE]

I'm sorry but that snippet looks ugly! Maybe apply some MVC? ;)

#1470814Post 11 of 11

Re: Any C++ programmers out there?

I'm not trying to go for pretty, it just wanted to show that PHP was similar to C :lol: I don't see any reason to add MVC just to hide a couple HTML tags from that code :)

I'd say about 75% of DJDB HTML is MVC, but the rest is dynamically built in functions like what you see above

Log in to reply