![]() |
|
#1
|
|||
|
|||
Ineffective "if then" loop on my first programHey 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:
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
|
|||
|
|||
Re: Ineffective "if then" loop on my first programQuote:
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:
and Code:
Your code prints the value of the first variable. Note that the following have no effect: CPP / C++ / C Code:
If you want to print stuff to cout, you can use cout << every time. So CPP / C++ / C Code:
The '<<' operator is defined in such a way that any number of writes can be strung together: CPP / C++ / C Code:
Of course they could be all on the same line: CPP / C++ / C Code:
Output Code:
Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Ineffective "if then" loop on my first programThanks a lot! That fixed my problem, although my compiler still likes the c<< in front of every output.
|
|
#4
|
|||
|
|||
Re: Ineffective "if then" loop on my first programQuote:
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:
2. No semicolon except after the last line. All of the lines together make one statement: CPP / C++ / C Code:
So the effect of the second example is exactly the same as if I had written CPP / C++ / C Code:
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
|
|||
|
|||
Re: Ineffective "if then" loop on my first programAh! You're right - I was overeager with my semi-colons.
Thanks. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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 | 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 | 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