A bit excited here people, just completed my first (well from the book) C++ script code. Its a Temperature Converting code from the C++ for Dummies book. Unfortunately, I haven't even made it through the first chapter and am having problems with the reading. So i need to ask a few questions:

1. What exactly is the purpose of white spaces?
2. What's the connectionbetween variables and declarations?
3. Give me a demonstration of a value being made? (Already know it, just need a better look at it)

Please help me out here.

Re: First C++ Code 80 80

>1. What exactly is the purpose of white spaces?
Whitespace serves two purposes. First, it separates certain tokens that cannot be immediately adjacent. For example, this is valid C++:

int i;

But this is not:


At least, it isn't valid unless you've declared a name called inti prior to that line that could be legally used in such a way. :)

The second use for whitespace is formatting. This is hard to read:

struct node{
int data;node*next;node(int init,node*link): data(init),next(link){}};
node*insert_ordered(node*list,int data)
{if(list==0){//Empty list
list=new node(data,0);}
else if(data<list->data){//Head insertion
list=new node(data,list);}
else{//Internal insertion
node *it=list;//Find the end of the list or the first larger item
it=it->next;//Insert before the first larger item
it->next=new node(data,it->next);}
return list;}
int main()
for(int i=0;i<10;i++)
std::cout<<it->data<<' ';

But this is much easier to follow:

#include <iostream>

struct node {
  int data;
  node *next;

  node ( int init, node *link )
    : data ( init ), next ( link )

node *insert_ordered ( node *list, int data )
  if ( list == 0 ) {
    // Empty list
    list = new node ( data, 0 );
  else if ( data < list->data ) {
    // Head insertion
    list = new node ( data, list );
  else {
    // Internal insertion
    node *it = list;

    // Find the end of the list or the first larger item
    while ( it->next != 0 && data > it->next->data )
      it = it->next;

    // Insert before the first larger item
    it->next = new node ( data, it->next );

  return list;

int main()
  node *list = 0;

  for ( int i = 0; i < 10; i++ )
    list = insert_ordered ( list,rand() % 100 );

  for ( node *it = list; it != NULL; it = it->next )
    std::cout<< it->data <<' ';

The code is identical, and the formatting whitespace makes all the difference even though it can be removed and the program will still compile and run properly.

>2. What's the connectionbetween variables and declarations?
Variables must be declared. That's about as close as the connection gets. One might say that a declaration creates a variable, but that rules out functions, so they would be wrong because a function is not a variable yet it too must be declared. A variable must be declared because until it is, the compiler will not recognize it. A declaration is basically an introduction to the compiler: "Hello, I'm called <name> and <type> is what I am." After that, whenever the compiler sees <name>, it says "Hey, I know you. You're a <type>. That's cool, you can keep going about your business." But if it sees a name that it doesn't know about, it says "Who are you? You look suspicious. Wait there while I call the programmer with an error or warning."

>3. Give me a demonstration of a value being made?
2 is a value, 'a' is a value. When var has been initialized, then var; is also a value. I get the feeling that your terminology is a little off. What's the way you know of to make a value?

Re: First C++ Code 80 80

Thanks, I'll have to re-read to fully understand later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.