Any C++ programmers out there?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Preston
    Gold Gabber
    • Jun 2004
    • 845

    Any C++ programmers out there?

    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;
    }
    And to make it compile and work I've had to amend it to this:

    Code:
    #include <iostream> // 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;
    }
    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.
    http://soundcloud.com/simonpreston/simon-preston-summer-2011
  • picklemonkey
    Double hoodie beer monster
    • Jun 2004
    • 15373

    #2
    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?
    Last edited by picklemonkey; March 7, 2011, 02:34:17 PM.

    Comment

    • Simon Preston
      Gold Gabber
      • Jun 2004
      • 845

      #3
      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?
      http://soundcloud.com/simonpreston/simon-preston-summer-2011

      Comment

      • demonAfro
        Are you Kidding me??
        • Jun 2004
        • 3488

        #4
        Re: Any C++ programmers out there?

        <iostream.h> is different from <iostream> 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 <iostream>
        gives you access to std::cout and all the others
        Code:
        using namespace std;
        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
        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 <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

        Comment

        • Simon Preston
          Gold Gabber
          • Jun 2004
          • 845

          #5
          Re: Any C++ programmers out there?

          Cheers for the response there. If I use
          Code:
          #include <iostream>
          using namespace std;
          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.
          http://soundcloud.com/simonpreston/simon-preston-summer-2011

          Comment

          • picklemonkey
            Double hoodie beer monster
            • Jun 2004
            • 15373

            #6
            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 here):

            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.='<div><center><table style="table-layout: fixed;" border=0 width=100%><tr>';
            
            	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;
            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

            Comment

            • Simon Preston
              Gold Gabber
              • Jun 2004
              • 845

              #7
              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.
              http://soundcloud.com/simonpreston/simon-preston-summer-2011

              Comment

              • i!!ustrious
                I got some N64 Games Yo!!
                • Mar 2008
                • 12308

                #8
                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?
                (((( }-d|-__-|b-{ ))))

                Comment

                • demonAfro
                  Are you Kidding me??
                  • Jun 2004
                  • 3488

                  #9
                  Re: Any C++ programmers out there?

                  Originally posted by Simon Preston
                  Maybe I should just get a more up to date guide.
                  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.

                  Originally posted by Simon Preston
                  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.
                  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.

                  Comment

                  • heretic
                    Addiction started
                    • Jun 2004
                    • 325

                    #10
                    Re: Any C++ programmers out there?

                    Originally posted by picklemonkey
                    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 here):

                    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.='<div><center><table style="table-layout: fixed;" border=0 width=100%><tr>';
                    
                    	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;
                    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
                    I'm sorry but that snippet looks ugly! Maybe apply some MVC?
                    I was in coma.

                    Comment

                    • picklemonkey
                      Double hoodie beer monster
                      • Jun 2004
                      • 15373

                      #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 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

                      Comment

                      Working...