GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Sep-2007, 06:36
ftmthy412 ftmthy412 is offline
New Member
 
Join Date: Sep 2007
Posts: 8
ftmthy412 is on a distinguished road

Ineffective "if then" loop on my first program


Hey all

Yesterday I tried to write my first program that included an "if then" loop with C++. I compiled it using g++, received some errors and fixed them, but in the process I somehow ended up rendering the loop ineffective. The program was just supposed to have you input two numbers and tell you which one was greater. The program is very small, so I'll post all the code because I'm not sure which parts are relevant.

Here's the code:
CPP / C++ / C Code:
//This program is to test an "if then" loop
//The user enters two numbers and the program says which is greater
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()

{

//declare the variable

int nVariable;
//tell the user to enter a number

cout<< "Please enter a number  ";

//define the input

cin>> nVariable;

//tell the user to enter a second number

cout<< "Please enter a second number  ";

//declare the second variable and define the input

int nVariableTwo;

cin>> nVariableTwo;


//Here's the loop I'm having trouble with

	if (nVariable > nVariableTwo)
	{
cout<< nVariable; "is greater than"; nVariableTwo ;
	}
	else {cout<< nVariable; "is less than"; nVariableTwo; }

return 0;
}


For some reason the loop never "activates" and the program ends once the second number is given.
Does anyone know what's causing my problem?
Thanks a lot!
  #2  
Old 12-Sep-2007, 07:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Ineffective "if then" loop on my first program


Quote:
Originally Posted by ftmthy412
Hey all
Hey.


1. Your program doesn't have any loops.

2. When I run your program (after compiling with g++) I get things like the following:

Code:
Please enter a number 1 Please enter a second number 2 1

and

Code:
Please enter a number 2 Please enter a second number 1 2

Your code prints the value of the first variable.

Note that the following have no effect:

CPP / C++ / C Code:
        "is greater than";
        nVariableTwo;
.
.
.

        "is less than";
        nVariableTwo;

If you want to print stuff to cout, you can use cout << every time.

So
CPP / C++ / C Code:
        cout << nVariable;
        cout << " is greater than ";
        cout << nVariableTwo;
        cout << endl;

The '<<' operator is defined in such a way that any number of writes can be strung together:

CPP / C++ / C Code:
        cout << nVariable
             << " is less than "
             << nVariableTwo
             << endl;

Of course they could be all on the same line:
CPP / C++ / C Code:
        cout << nVariable << " is less than " << nVariableTwo << endl;

Output
Code:
Please enter a number 1 Please enter a second number 2 1 is less than 2

Code:
Please enter a number 2 Please enter a second number 1 2 is greater than 1

Regards,

Dave
  #3  
Old 12-Sep-2007, 08:39
ftmthy412 ftmthy412 is offline
New Member
 
Join Date: Sep 2007
Posts: 8
ftmthy412 is on a distinguished road

Re: Ineffective "if then" loop on my first program


Thanks a lot! That fixed my problem, although my compiler still likes the c<< in front of every output.
  #4  
Old 12-Sep-2007, 08:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Ineffective "if then" loop on my first program


Quote:
Originally Posted by ftmthy412
my compiler still likes the c<< in front of every output

Well, as much as I like to anthropomorphize things, I am not a compiler, so I don't know what mine likes.

However...

Note the difference between the following:

1. A semicolon makes each line a complete statement. cout << begins each line
CPP / C++ / C Code:
        cout << nVariable;
        cout << " is greater than ";
        cout << nVariableTwo;
        cout << endl;

2. No semicolon except after the last line. All of the lines together make one statement:

CPP / C++ / C Code:
        cout << nVariable
             << " is less than "
             << nVariableTwo
             << endl;

So the effect of the second example is exactly the same as if I had written
CPP / C++ / C Code:
        cout << nVariable << " is less than " << nVariableTwo << endl;

All of these forms are equally acceptable to a C++ compiler. It's a matter of style. (Use the form that satisfies your human sensibilities.)


Details! Details! The devil is in the details!

Regards,

Dave
  #5  
Old 12-Sep-2007, 09:17
ftmthy412 ftmthy412 is offline
New Member
 
Join Date: Sep 2007
Posts: 8
ftmthy412 is on a distinguished road

Re: Ineffective "if then" loop on my first program


Ah! You're right - I was overeager with my semi-colons.
Thanks.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc/free usage, and general good programming behavior lavaka C Programming Language 7 14-Jun-2007 19:50
Text-Based Roulette Game mfm1983 CPP / C++ Forum 5 29-Nov-2006 12:20
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Pipeline freeze simulation darklightred CPP / C++ Forum 6 27-Jul-2006 19:37
Total newbie to programming C! Need help... majortom C Programming Language 2 17-Nov-2005 09:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.