Thread: Help with Structures

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    19

    Question Help with Structures

    **Cue usual spiel about being a noob and new to programming**

    Here is the problem: Create an array of space ship objects and write a program that continually updates their positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768pixels.

    Build errors are at the bottom of the code...


    I'm sure this is something obvious but I cannot figure out what I need to do here... Any hints or help would be appreciated.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    
    
    using namespace std;
    
    
    struct spaceShip
    {
        int x_coord;
        int y_coord;
    };
    spaceShip ships[20];
    
    
    
    
    spaceShip createShips (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = rand() % 1024;
            ships[i].y_coord = rand() % 768;
        }
    }
    spaceShip updateLoc (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = ships[i].x_coord + rand() % 100;
            ships[i].y_coord = ships[i].y_coord + rand() % 50;
        }
    }
    void displayTest (spaceShip ships[], int size)
    {
        cout << "{";
        for (int i = 0; i < size; i++)
        {
            if ( i != 0 )
            {
                cout << "\n";
            }
            cout << ships[i].x_coord;
        }
        cout << "}";
    }
    spaceShip findHighest (spaceShip ships[], int size)
    {
        int highestValue = 0;
        for (int i = 0; i < size; i++)
        {
            if (spaceShip ships[i] > highestValue)
            {
                highestValue = spaceShip ships[i];
            }
        }
        return highestValue;
    
    
    int main ()
    {
        srand(time(NULL));
        createShips(ships, 20);
        displayTest(ships, 20);
        findHighest(ships, 20);
        while (highestValue < 1024)
        {
            updateLoc(ships, 2)
            findHighest(ships, 20);
        }
        cout << "All Ships have left the screen!\n";
    }
    
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected primary-expression before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected ')' before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected primary-expression before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected ';' before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|58|error: a function-definition is not allowed here before '{' token|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|69|error: expected '}' at end of input|
    ||=== Build finished: 7 errors, 2 warnings ===|

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First count your braces, does every starting brace have a matching ending brace?

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    spaceShip createShips (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = rand() % 1024;
            ships[i].y_coord = rand() % 768;
        }
        // C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
        // C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
        // It's complaining that you don't have
        // return something;
    }
    But since you're
    a) just writing the results into the supplied array
    b) you call the function AND ignore the result
    then perhaps you should just make it a void function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Ok I feel like an idiot, found the missing brace but it seems to have made it worse.... Now it says I am missing braces and semicolons everywhere but I cant find any missing...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    
    
    using namespace std;
    
    
    struct spaceShip
    {
        int x_coord;
        int y_coord;
    };
    spaceShip ships[20];
    int highestValue;
    
    
    spaceShip createShips (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = rand() % 1024;
            ships[i].y_coord = rand() % 768;
        }
    }
    spaceShip updateLoc (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = ships[i].x_coord + rand() % 100;
            ships[i].y_coord = ships[i].y_coord + rand() % 50;
        }
    }
    void displayTest (spaceShip ships[], int size)
    {
        cout << "{";
        for (int i = 0; i < size; i++)
        {
            if ( i != 0 )
            {
                cout << "\n";
            }
            cout << ships[i].x_coord;
        }
        cout << "}";
    }
    spaceShip findHighest (spaceShip ships[], int size)
    {
        highestValue = 0;
        for (int i = 0; i < size; i++)
        {
            if (spaceShip ships[i] > highestValue)
            {
                highestValue = spaceShip ships[i];
            }
        }
        return highestValue;
    }
    
    
    int main ()
    {
        srand(time(NULL));
        createShips(ships, 20);
        displayTest(ships, 20);
        findHighest(ships, 20);
        while (highestValue < 1024)
        {
            updateLoc(ships, 2);
            findHighest(ships, 20);
        }
        cout << "All Ships have left the screen!\n";
    }
    
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected primary-expression before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected ')' before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected primary-expression before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected ';' before 'ships'|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
    ||=== Build finished: 5 errors, 2 warnings ===|

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    I'm not quite sure what you mean by ignore the results, I use the results to see where the ships are laid initially right? I am lost

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at this
    ships[i].x_coord = ships[i].x_coord + rand() % 100;

    Now look at this
    if (spaceShip ships[i] > highestValue)

    It seems you copied the whole type and variable name from the function parameter declaration.

    > I'm not quite sure what you mean by ignore the results, I use the results to see where the ships are laid initially right? I am lost
    Code:
    int good ( ) {
      return 42;
    }
    int bad ( ) {
      return 1234;
    }
    int ugly ( ) {
      // now what is returned here? garbage? worse?
    }
    
    int main ( ) {
      int a = good();
      bad();  // now why exactly did you return 1234?
      ugly(); // garbage returned, but ignored anyway (we hope)
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    sorry I am new to this and am not quite sure why I need to have a result output in that function... and if I do need one then what should it be? I tried making it a void and it didnt really change anything program still doesn't compile.

    forgive me so would correct syntax be

    if (ships[i].x_coord > highestValue) ?
    Last edited by rTeapot; 07-16-2012 at 11:42 AM.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Ok so I changed my if statement in the highestValue function and now just getting the return errors that I just don't understand...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    
    
    using namespace std;
    
    
    struct spaceShip
    {
        int x_coord;
        int y_coord;
    };
    spaceShip ships[20];
    int highestValue;
    
    
    spaceShip createShips (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = rand() % 1024;
            ships[i].y_coord = rand() % 768;
        }
    }
    spaceShip updateLoc (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = ships[i].x_coord + rand() % 100;
            ships[i].y_coord = ships[i].y_coord + rand() % 50;
        }
    }
    void displayTest (spaceShip ships[], int size)
    {
        cout << "{";
        for (int i = 0; i < size; i++)
        {
            if ( i != 0 )
            {
                cout << "\n";
            }
            cout << ships[i].x_coord;
        }
        cout << "}";
    }
    spaceShip findHighest (spaceShip ships[], int size)
    {
        highestValue = 0;
        for (int i = 0; i < size; i++)
        {
            if (ships[i].x_coord > highestValue)
            {
                highestValue = ships[i].x_coord;
            }
        }
        return highestValue;
    }
    
    
    int main ()
    {
        srand(time(NULL));
        createShips(ships, 20);
        displayTest(ships, 20);
        findHighest(ships, 20);
        while (highestValue < 1024)
        {
            updateLoc(ships, 2);
            findHighest(ships, 20);
        }
        cout << "All Ships have left the screen!\n";
    }
    
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
    C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
    ||=== Build finished: 1 errors, 2 warnings ===|

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, but seriously, do you understand how functions work? Do you understand how to return values from a function? Do you understand how to specify return types for functions?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Elysia, actually I know how to return values but honestly have no idea what you mean by return types...(**edit :this must have something to do with why I had to change the spaceShip lowestValue to int lowestValue because the return was an int and spaceShip isn't an int? and they have to match?) You were right Salem, voids were better since the values weren't really being used elsewhere, I did have an error left with spaceShip findHighest, apparently I can't do that since I have it return highestValue which is an int and spaceShip is a non-scalar? Didn't know that one but I changed it to the below function and it now compiles fine and works... Also figured out what I really wanted was the lowest value not the highest so I fixed that as well, now it does exactly what I wanted... Sorry for my stupidity guys and thanks for bearing with me as I am still very much a beginner...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    
    
    using namespace std;
    
    
    struct spaceShip
    {
        int x_coord;
        int y_coord;
    };
    spaceShip ships[20];
    int lowestValue;
    
    
    void createShips (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = rand() % 1024;
            ships[i].y_coord = rand() % 768;
        }
    
    
    }
    void updateLoc (spaceShip ships[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            ships[i].x_coord = ships[i].x_coord + rand() % 100;
            ships[i].y_coord = ships[i].y_coord + rand() % 50;
        }
    
    
    }
    void displayTest (spaceShip ships[], int size)
    {
        cout << "{";
        for (int i = 0; i < size; i++)
        {
            if ( i != 0 )
            {
                cout << "\n";
            }
            cout << ships[i].x_coord;
        }
        cout << "}";
    }
    int findLowest (spaceShip ships[], int size)
    {
        lowestValue = 1024;
        for (int i = 0; i < size; i++)
        {
            if (ships[i].x_coord < lowestValue)
            {
                lowestValue = ships[i].x_coord;
            }
        }
        return lowestValue;
    }
    
    
    int main ()
    {
        srand(time(NULL));
        createShips(ships, 20);
        displayTest(ships, 20);
        findLowest(ships, 20);
        while (lowestValue < 1024)
        {
            updateLoc(ships, 20);
            findLowest(ships, 20);
            displayTest(ships, 20);
        }
        cout << "All Ships have left the screen!\n";
    }

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    honestly have no idea what you mean by return types...(**edit :this must have something to do with why I had to change the spaceShip lowestValue to int lowestValue because the return was an int and spaceShip isn't an int? and they have to match?)
    If you don't know what a function return type is, you should probably review the following links: Functions I and Functions II.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link Lists of Structures within Structures...
    By Zocheyado in forum C Programming
    Replies: 3
    Last Post: 04-17-2012, 04:21 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread