[prog] Re: more C++

Jacinta Richardson jarich at perltraining.com.au
Mon Dec 15 12:56:44 EST 2003


> Thank you : )  Would anyone happen to be able to show this in a 
> simplified C++ version?
> While I sort of understand some of this, it's a bit over my head for 
> where I am at the
> moment : )  The only #include I've used so far is #include <iostream>  
> (altho I assume a .h means it's a header file of some sort). 

string.h provides a bunch of string manipulation functions.  
stdio.h is the C equivalent of C++'s iostream and provides input/output
functions.  Using libraries with #include should not be too scarey.

> And the first bit of the 
> book I'm still working on
> doesn't  show anything about   sprintf  or strlen(string).

I'd be surprised if your book mentioned sprintf or strlen at all as I'm
fairly certain that C++ has it's own, much nicer way with dealing with
strings.  You probably write something like:

	String string;		// declare that the variable string is
				// a String type object (or is it 
				// CString?)
	string = integer1+"";	// I'm not sure about this, I can't
				// remember how to copy a number into
				// a C++ string
	int length = string.len;	// get the length of string
	int i;

	for (i = 0; i < length; i++) {
		std::cout << string[i] << " ";
	}


and so on.  I can't quite remember.

Note that because C++ does some type conversions for you, you could skip
some of this by reading directly into the string:

	String string;

	std::cout << "Enter a number!\n";

	std::cin >> string;

The for loop above probably won't work for you either (once you use C++
style strings), so you may need to look around for functions like charAt
or substring. 

However, if you were writing in C you would use sprintf and strlen.
sprintf prints it's arguments to a string rather than to STDOUT.  If
you're using a *nix based system then you can learn more about sprintf by
typing "man sprintf".  If you're not then I'm sure you can get information
in other ways.  At worst, just google on "man sprintf(3)".

strlen merely returns the length of the string passed in as its argument.

> Thank you to everyone for the examples and explanations, I'm getting 
> some good notes to use : ) : ) : )

No problem whatsoever.  Do note however that the task you asked us to do
(take a single number and print out each digit separated by spaces) is not
a trivial one in C or C++.  It's easy as pie in Perl other higher level
languages, but not in C or C++.  I'd imagine it to be slightly easier in
C++ than C though.

For comparison purposes the following code performs this problem in Perl:

	#!/usr/bin/perl -w
	use strict;

	print "Please enter a number: ";
	my $input = <STDIN>;

	print "You entered.... " . join(" ", split("", $input)) . "\n";

Cool huh? 
(split splits a string up into pieces.  Because we've given it an empty
string, it'll split everything up into separate characters and return a
list of them. join takes a list of things (in this case what split
returns) and joins them together putting it's first argument between them.
So join(" ", 'a', 'b', 'c') will return "a b c" which is what we want.

Anyway, I hope this helps,

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |






More information about the Programming mailing list