|
-
Dec 13th, 2000, 10:47 PM
#1
Is there like a For...Next Statement in Borland C++?
Something like:
Code:
void main()
{
int a;
cout<<"1-100:\n\n\";
'For a = 0 to 100
cout<< a;
'Next a
cin>> a;
}
So I can print a list of numbers down the screen?
(1\n\2\n\3\n\4\n\5\n\6\n\7\n\8\9\10\n\.....)
Hope you get what I mean.
-
Dec 14th, 2000, 08:18 AM
#2
Frenzied Member
Code:
for(int intCount, intCount =<100, intCount++)
{
cout << intCount << endl;
}
-
Dec 14th, 2000, 08:34 AM
#3
Frenzied Member
Close
Code:
for(int intCount = 0; intCount <= 100; intCount++)
{
cout << intCount << endl;
}
you have to put semicolons between the parts of the for statement.
this translates into something like this
Code:
int intCount = 0; //First Statement
StartLoop: //a label
if !(intCount <= 100) //second Statement
{
goto EndLoop; //I don't think there's a goto in C++
} //but I'm just saying what th for loop does
cout << intCount << endl; //the code inside the brackets
intCount++; //third statement
goto StartLoop;
EndLoop:
//code after for loop
the statements can be anything you like, so the for statement can be a do while ,a do until loop or whatever, the statements have to be separated by semicolons not commas tho.
If it wasn't for this sentence I wouldn't have a signature at all.
-
Dec 14th, 2000, 08:45 AM
#4
Frenzied Member
a crap 
i havent coded c++ in a while 
yep your right
-
Dec 14th, 2000, 08:18 PM
#5
Frenzied Member
Megatron right on both counts
(just out of interest if you can do gotos how do you do labels, cos mine clearly don't work.)
Matthew Sorry about the confusion, I find being able to think of for loops "written out" really helpful, so I thought I'd put it in.
Did you declare intCount before your for loop? if you did then you've declared it twice, try getting rid of the int in the for statement
Code:
for(intCount = 0; intCount <= 100; intCount++) {
cout << intCount << endl;
}
see what happens.
If it wasn't for this sentence I wouldn't have a signature at all.
-
Dec 14th, 2000, 09:07 PM
#6
Argh! 
It still don't work. Same errors.
Is it me? Or is it Turbo C++ 3.0?
Maybe I need to upgrade to a higher C++ (possibly VC++ or Borland C++ 4.x) .
-
Dec 15th, 2000, 07:54 AM
#7
Code:
#include <iostream.h>
int main()
{
for(int i = 1; i <= 10; i++)
cout << i;
return 0;
}
that exact code works perfect for me..
and the same as everything else(while loop, if statement, else statement, etc) you only need the curly braces if you need more than one statement executed.
by the way.....
when you do this:
Code:
for(int i = 1; i <= 10; i++)
cout << i;
it goes from 1 to 10,
when you do this:
Code:
for(int i = 0; i < 10; i++)
cout << i;
it goes from 0 to 9...
I am sure you knew this already, but just pointing that out.. 
[Edited by denniswrenn on 12-15-2000 at 07:57 AM]
-
Dec 15th, 2000, 02:47 PM
#8
It works!
Thanks Dennis and everyone else that helped clear things up for me.
Just one more thing, I want the numbers to show up like:
1
2
3
4
5
6
7
8
9
10
.
.
.
.
...100
In C++, what is the & operator?
Something like: ?
Code:
#include <iostream.h>
int main()
{
for(int i = 1; i <= 10; i++)
cout << i & "\n\";
return 0;
}
-
Dec 15th, 2000, 02:57 PM
#9
Code:
#include <iostream.h>
int main()
{
for(int i = 1; i <= 10; i++)
cout << i << "\n";
return 0;
}
there is no \ after \n, if you left it like that, the compiler would think you were escaping the quote, and it would give you an error about a missing end quote, and probably missing semicolon too.
and the & is used like the VB addressof operator, but it can be used on variables too...
if you want 2 things to be output from 1 cout statement, use <<
the logical and operator is &&,
ex:
Code:
#include <iostream.h>
int main()
{
int a = 1, b = 2, c = 3;
if ((a == 1) && (b == 2) && (c == 3))
cout << "Ok";
else cout << "not ok";
return 0;
}
-
Dec 15th, 2000, 02:57 PM
#10
Monday Morning Lunatic
The & operator is similar to the AddressOf operator in VB, but more useful. It gets the address of anything - variable, function, etc.
So, for example:
Code:
void func(char *str) {
cout << str << endl;
}
void other() {
char *mystr = "This is a string";
func(&(mystr[1]));
}
Calling other displays "his is a string".
To do what you want, just add another << item insertion:
Code:
cout << "i = " << i << endl;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 15th, 2000, 03:49 PM
#11
Frenzied Member
Well & is not only the AddressOf operator, it's also the bitwise And operator, and in C++ it's also the ReferenceTo operator (not sure if it's called that). It depends on the context.
Code:
CClass *var = &otherVar; // address-of
var& //a reference
var1 = var2 & var3; // bitwise and
Harry.
"From one thing, know ten thousand things."
-
Dec 15th, 2000, 04:48 PM
#12
[QUOTE]Originally posted by Sam Finch
[B]Megatron right on both counts
(just out of interest if you can do gotos how do you do labels, cos mine clearly don't work.)
This should work:
Code:
#include <iostream.h>
void main() {
int n;
StartPgm:
cout << "Press '1' to exit or press 2 to display this message again" << endl;
cin >> n;
if( n == 2 )
goto StartPgm;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|