Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 For Loop Trouble . . .
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Thu Apr 28, 2005 8:35 am   Post subject: (No subject)

Just take it slow. C++ has a pretty steep learning curve, but it'll all make sense eventually.
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Thu Apr 28, 2005 8:46 am   Post subject: (No subject)

I've been reading through all of this, and for cornflakes/martin's method, how do you go from the second part to teh first part. I mean, how do you skip the calculator, and jump right to main?

And for wtd, I pretty much understand it, Its just I cant get it to run, I added the
c++:
#include <iostream>
#include <string> 
But are there any others I'm missing?

I'm tryin to take it slow, but i just recently realized how far behind I am programming wise, [ lol all i know is turing ]. So that's why I'm really pushin this c++ for me.
Martin




PostPosted: Thu Apr 28, 2005 9:05 am   Post subject: (No subject)

int main is the entry point for the program. Unlike turing which just begins from the top of the code and works its way down, a C++ program starts at the int main method. Calculate() is just a function that you can call.

c++:

#include <iostream>
using namespace std;

void somefunction ()
{
      cout << "This code never gets called\n";
}

int main ()
{
     cout << "Hello World\n";
     return 0;
}

void someotherfunction ()
{
      cout << "This code never gets called\n";
}


The output for this program will be "Hello World"
jamonathin




PostPosted: Thu Apr 28, 2005 11:10 am   Post subject: (No subject)

ohhhhhhhhh, i thought main was like Calculator, just a word, ok thats makes more sense, thanks Nuty Eyes .
jamonathin




PostPosted: Thu Apr 28, 2005 11:40 am   Post subject: (No subject)

I hope I'm not buggin you guys too much, if I am, just kick me or something.
I was looking through the tutorials, and Hacker Dan suggested to just redo some old turing programs into C++.
I found an example of a for loop in the tuts section, but I can't quite figure it out.
c++:
#include <iostream>

int main ()
{
    int n, t = 1;
   
    std::cout << "Enter Any Number.\n";
    std::cin >> n;
   
    for (int i = 1; int i < n; i++)
    {
        t *= i;
    }

    std::cout << "The Result Is " << t;
       
    int wait;
    std::cin >> wait;
       
    return 0;
}

Also, if anyone knows where a good help file is, (like Turing's F10), can ya tell me about it, cuz I dun wanna annoy you guys. Thanks Very Happy

EDIT: It's kinda wierd wtd and I posted on the same minute, but on his 2000th post! Head Bang
wtd




PostPosted: Thu Apr 28, 2005 11:40 am   Post subject: (No subject)

Martin wrote:
int main is the entry point for the program. Unlike turing which just begins from the top of the code and works its way down, a C++ program starts at the int main method. Calculate() is just a function that you can call.

c++:
#include <iostream>
using namespace std;

void somefunction ()
{
      cout << "This code never gets called\n";
}


Martin, while technically correct, the more idiomatic C++ solution is not to use a literal newline, but rather std::endl.

c++:
#include <iostream>
using namespace std;

void somefunction ()
{
      cout << "This code never gets called" << endl;
}
wtd




PostPosted: Thu Apr 28, 2005 11:44 am   Post subject: (No subject)

c++:
for (int i = 1; int i < n; i++)


This is your problem.

The first "int" is used to declare the variable "i". Subsequent uses of "i" do not require it. Corrected, it looks like:

c++:
for (int i = 1; i < n; i++)
jamonathin




PostPosted: Thu Apr 28, 2005 11:52 am   Post subject: (No subject)

Something's still wrong though. In Turing, if I enter 4, i get 24, but in this program, i get 6. This is my for loop in Turing.
Turing:
for i : 1 .. n
    t *= i
end for
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Apr 28, 2005 11:57 am   Post subject: (No subject)

In your Turing loop, you say "loop from 1 to n". In the C++ loop you're saying "loop from i = 1 while i is less than n, incremeneting i by one each time".

Thus the loop goes from 1 to 3. To change this, you need to use the less than or equals operator. I'm just using a bit of a different initialization syntax for "i", and I'm using the prefix increment operator.

c++:
for (int i(n); i <= n; ++i)
Martin




PostPosted: Thu Apr 28, 2005 12:18 pm   Post subject: (No subject)

it should be int i (1), not (n).
wtd




PostPosted: Thu Apr 28, 2005 12:25 pm   Post subject: (No subject)

Ah yes.
Martin




PostPosted: Thu Apr 28, 2005 12:28 pm   Post subject: (No subject)

Got your back Wink
wtd




PostPosted: Thu Apr 28, 2005 12:38 pm   Post subject: (No subject)

Martin wrote:
Got your back Wink


That's what I'm afraid of. Wink
jamonathin




PostPosted: Thu Apr 28, 2005 12:48 pm   Post subject: (No subject)

lol . . . works good guys, thanks Smile
wtd




PostPosted: Thu Apr 28, 2005 12:56 pm   Post subject: (No subject)

Congrats on accidentally implementing a factorial function. Now you need only embrace the zen of:

Haskell:
factorial = product . enumFromTo 1


Smile
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 30 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: