#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    14
    Rep Power
    0

    While Loop Error


    I am very new at programming in general, but I tend to pick up things by following examples...

    Anyways, I have been in charge of tweaking a bot written in python and I feel that it is time to convert it over to C++ if the thing is to keep up with the demands it is needed for.

    I searched google.com and I found a couple things to turn python into C++.

    Right now I am working on converting a sample python program to a C++ sample program and when I goto compile it, I get errors...

    Here is the Original Sample Code

    And after some trial and error, this is where I am at:

    // this is a test
    #include <iostream.h>
    int x = -1;
    while (x) // loop until 0 spam
    {
    cout << "\nHow much spam?"; cin >> x ;
    if (x)
    {
    cout << "We have:" << endl;
    for (i=0; i<x; i++)
    {
    cout << "spam" << " ";
    if (i == x-2) // (don't forget the beans!)
    {
    cout << "baked beans and" << " ";
    }
    }
    }
    else
    {
    cout << "Enjoy!" << endl;
    }
    }
    // all done!


    Now I am getting the error 'Declaration terminated incorrectly' in this line:

    while (x) // loop until 0 spam

    and everything I have seen using goole.com tells me it is correct.

    Can somebody shed some light on the subject for me please?
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11
    Is your code inside a function, like main() or something?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    14
    Rep Power
    0
    No, the code I have in the orange/red is exactly how I have it.
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11
    OK, you need a main() function somewhere, something along the following lines:

    Code:
    #include <iostream.h> 
    
    int main()
    {
        int x = -1; 
        while (x) { 
            cout << "\nHow much spam?"; cin >> x ; 
            if (x) { 
                cout << "We have:" << endl; 
                for (int i=0; i<x; i++) { 
                    cout << "spam" << " "; 
                    if (i == x-2) { 
                        cout << "baked beans and" << " "; 
                    } 
                } 
            } else { 
                cout << "Enjoy!" << endl; 
            } 
        } 
    }
    This should be covered very early on in any beginners book, see if you can get your hands on one.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    14
    Rep Power
    0
    Ahhhh...

    That is what I was doing wrong :)

    I tried to put do this before:

    int main()
    int x = 1;

    Thanks for your help

    As far as trying to learn, i am trying all sorts of tutorials on the internet and such...

    One of these days I will have to buy a book on C++
  10. #6
  11. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11
    You can download a free copy of Bruce Eckel's Thinking In C++ here:

    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

    but I believe it expects you to have at least a little programming experience at the start so you may need to work through a few tutorials first.

IMN logo majestic logo threadwatch logo seochat tools logo