Re: Function problems in simple C++ prog.

This is a discussion on Re: Function problems in simple C++ prog. within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm having a bit of trouble here. I have a really easy program using a large number of ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Unhappy Re: Function problems in simple C++ prog.

    Hey guys, I'm having a bit of trouble here. I have a really easy program using a large number of functions in one program. The program simulates a race between a hare and a tortoise and then outputs the to the screen. It *should* look something like this:

    .....H.......T...................................
    ............H....T...............................
    ...................T......H......................
    ..........................T.............H........
    ..................................T..............H
    The hare wins.

    But my output's getting screwed up. If you wanna take a crack and help me with what's wrong, I'd be quite thankful.
    The code I have so far is attached as csc141_ass4.cpp.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    33

    What tha...

    Im getting :

    c:\windows\desktop\csc141_ass4.cpp(127) : fatal error C1010: unexpected end of file while looking for precompiled header directive

    That what you see to? Am I missing a file here or something?
    "Who ya gonna call?"

    AWAX

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    well, to get you started, you have to update your pos_tortoise, pos_hare....this could be as simple as passing them by reference. Actually you should pass tortoise and hare, and completely drop your pos_ variables. Another thing, if you have your for loop running until count = 70, incrementing by one, your tortoise and hare could run to 210 and 420, respectively b/c it doesn't end when one of them hits 70. I would change it to a while loop along with the aforementioned changes, it might look something like this:

    // Define this as a global const, so it can be used in other functions
    const int track_len = 70;

    // Make it int, good practice and I said so
    int main() {

    int tort = 0, hare = 0, randNum;
    srand(time(NULL));

    while(hare < track_len && tort < track_len) {
    randNum = rand()%9+1;
    move_tort(tort, randNum);
    move_hare(hare, randNum);
    display(tort, hare);
    }

    results(tort, hare);

    return 0;
    }

    void move_tort(int &t, int n) {
    switch( n ) {
    // A few things here, 1. you add 1 to your random number, you'll never
    // get a 0 case, 2. you don't need to repeat code as much, watch:
    case 1:
    case 2:
    case 3:
    case 4: t+= 3; break; // If your random number is 1, 2, 3, or 4, it will add
    // 3 to your tortoise position. In addition, since
    // tortoise was passed by reference, it will be
    // incremented by 3 back in main()
    // In the next set of cases, you might want to account for the position
    // being <6, which would cause a negative, ie.
    case 5:
    case 6: t>=6?t-=6:t=0; break;
    // If you don't know how t>=6?t-=6:t=0 works, you could use:
    // if(t >= 6) t-=6; else t = 0;

    case 7:
    case 8:
    case 9: t+=1; break;
    default: cout << "WHAT THE HELL IS GOING ON?\n"; break; }
    }

    void move_hare(int &h, int n) {/*You can figure this out*/}

    // I'm sure there are flaws in this function, I was writing it as I went
    void display(int t, int h) {
    // I do this differently than you did, but that's just personal opinion

    int diff, *first, *second;
    bool tied = false;

    h =
    if(h==t) tied = true;
    first = (t > h ? t : h);
    second = (t > h ? h : t);
    diff = track_len - *first;
    if(diff < 0) diff = 0;

    for(int i = 0; i<*second && i<track_len; i++)
    cout << ".";
    if(tied) cout << "B";
    else if(h == *second) cout << "H";
    else cout << "T";

    for(int i = *second; i<*first && i<track_len; i++)
    cout << ".";
    if(tied);
    else if(h == *first) cout << "H";
    else cout << "T";

    for(int i=0; i<diff; i++)
    cout << ".";
    }
    Hope this is helpful.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Lightbulb i got a better one....

    check this out....

    in your directives to the preprocessor.....

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;

    you use the old C style headers that end with (.h)
    and use the new C++ style phrease (using namespace std

    when you call on function rand() the compiler exits due to the ambiguity of the two (2) styles.....
    what i mean is that the compiler gets confused whether u are using the rand() or std::rand()

    my advice is just earase the (.h) part of your header files and put instead of just rand() ... you should put std::rand(), i think that should get rid of the fatal error which is te unexpected end of file

    i just tried it and it runs..... i didn't

    ps. hint....... by using your header file <stdlib>

    and instead of writing your pause function yourself put this

    system("pause");

    in that place that will pause the system where ever you want and will ask you to press any key to continue....

    i hope that helps.....

    to tell you the truth .... the outcome doesn't look like what you have expected but i will leave that up to you..... look at the previous post reply.... it should help you


    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with virtual function calls, please help
    By e66n06 in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2007, 04:12 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. problems running simple prog
    By scar in forum C++ Programming
    Replies: 11
    Last Post: 10-29-2005, 06:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. sin() function problems.
    By Lifedragn in forum C Programming
    Replies: 4
    Last Post: 09-28-2004, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21