Follow us on Twitter!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 25
Guests Online: 22
Members Online: 3

Registered Members: 82838
Newest Member: w1zarrd
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 02:39
line38: expected primary-expression before "int"

Code


   
    #include <iostream>
    #include <string>
   #include <cmath>
   using namespace std;
   
   //function prototypes
    int getInteger();
   int convertIn(int, int);
   
   //functions
   void getInteger(int)
    {
         int x;
         cin >> x;
         return;
    }
   
   
   void convertInches(int ft, int in)
    {
       in = ft*12 + in;
   }
   
        
   int main()
    {
       int rooms = 0, tile = 0;
        int widthFt = 0, widthIn = 0;
        int lengthFt = 0, lengthIn = 0;
       int totWidth = 0, totLength = 0, tileLength = 0, tileWidth = 0;
        int extra = 0, tilesNeeded = 0, boxes = 0;
        int totalTiles = 0, totalBoxes = 0, totalLeft = 0;
   
       cout<<"Enter number of rooms:" << endl;
       rooms = getInteger(int);
   
       cout<<"Enter size of tile in inches:" << endl;
       tile= getInteger(int);
   
       for(int i=0; i<rooms; i++)
        {
           cout<<"Enter room width (feet and inches, separated by a space):" << endl;
           cin >> widthFt >> widthIn >> endl;
   
   
           cout<<"Enter room length (feet and inches, separated by a space):" << endl;
           cin >> lengthFt >> lengthIn >> endl;
   
   
           totWidth = convertInches(widthFt, widthIn);
           totLength = convertInches(lengthFt, lengthIn);
           tileLength = totLength / tile;
           if (totalLength % tile > 0) // rounds tileLength up
           {
                            tileLength++;
            }
           tileWidth = totWidth / tile;
           if (totalWidth % tile > 0) // rounds tileWidth up
           {
                           tileWidth++;
            }
           tilesNeeded = tileLength * tileWidth;
           totalTiles += tilesNeeded;

           cout<<"Tiles needed: "<< tilesNeeded << endl;
   
           boxes = tilesNeeded / 20;
           if (tilesNeeded % 20 > 0) // rounds boxes up
           {
                            boxes++;
            }
           totalBoxes += boxes;

   
           cout<<"Number of boxes needed: " << boxes << endl;
   
           extra = (20 * boxes) - tilesNeeded;
           totalLeft += extra;
   
           cout<<endl<<"Leftover tiles: " << extra << endl;
   
       }
       cout << "Total Number of Tiles: " << totalTiles << endl;
       cout << "Total Number of Boxes: " << totalBoxes << endl;
       cout << "Total Leftovers: " << totalLeft << endl;
   return 0;
   }











Edited by on 27-02-11 02:49
Author

RE: C++ functions and tiles program

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 27-02-11 03:15
You can't name a variable "int".


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 04:56
ynori7 wrote:
You can't name a variable "int".

There's plenty more wrong here than just that.
For instance, what good is this?

Code
void convertInches(int ft, int in)
{
    in = ft*12 + in;
}
...
totWidth = convertInches(widthFt, widthIn);



How do you not get an error on that?


Author

RE: C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 10:03
ynori7 wrote:
You can't name a variable "int".


I didnt know I named a variable int. where is that at?


Author

RE: C++ functions and tiles program

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 27-02-11 11:05
You don't, as far as I can see. I think ynori7 means this:
Code
rooms = getInteger(int);



When you call a function, you should provide variable names as arguments, not just a type:
Code
int bla;
rooms = getInteger(bla);



The above is still wrong however, because the function is completely wrong, as COM already pointed out.
Code
void getInteger(int)
{
    int x;
    cin >> x;
    return;
}



Let me explain what you're trying to do in getInteger. You say void, which means that getInteger doesn't have a useful return-value. Then you say (int) which in this case means that you expect exactly one argument, being of type int. Yet you don't name it, which means that you won't be using it. Then there are two lines that are actually correct: you declare a local integer called x, and you put a number from cin in x. (You don't check whether the user actually gave a valid number, but let's just forget about that for now.) Then you return, which means the function just stops. Exactly the same would have happened if you had not typed that line, because of the }, stating the end of the function. Now the local variable x is thrown away and nothing is done with the result.

This is what you probably wanted:
Code
int getInteger()
{
    int x;
    cin >> x;
    return x;
}



The function doesn't need any parameters, because you're not using them anyway. Next, the function actually returns an integer, meaning that you can call it like this:
Code
int foo = getInteger();




Other possibility:
Code
void getInteger(int& x)
{
    cin >> x;
}

int main()
{
    int y;
    getInteger(y);
    cout << "You entered: " << y << endl;
    return 0;
}


But I suggest you'd stay away from call-by-reference parameters for now.



...
- - -
Author

RE: C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 12:05
Edit: Fixed, and updated code! tell me what you guys think!

Code

   
    #include <iostream>
    #include <string>
   #include <cmath>
   using namespace std;
   
   //function prototypes
   int convertInches(int, int);
   void PrintLength();
   void PrintWidth();
   
   //functions
   void PrintLength()
    {
    cout << "Enter room Length (feet and inches, separated by a space):";
    }
   
    void PrintWidth()
    {
    cout << "Enter room Width (feet and inches, separated by a space):";
    }

   
        
   int main()
    {
       int rooms = 0, count = 0, tile = 0;
        int widthFt = 0, widthIn = 0;
        int lengthFt = 0, lengthIn = 0;
       int totWidth = 0, totLength = 0, tileLength = 0, tileWidth = 0;
        int extra = 0, tilesNeeded = 0, boxes = 0;
        int totalTiles = 0, totalBoxes = 0, totalLeft = 0;
   
       cout<<"Enter number of rooms:";
       cin >> rooms;
   
       cout<<"Enter size of tile in inches:";
       cin >> tile;
   
        while (count < rooms)
        {
             
           
           PrintWidth();
           cin >> widthFt >> widthIn;
   
   
           PrintLength();
           cin >> lengthFt >> lengthIn;
   
   
           totWidth = (widthFt * 12) + widthIn;
           totLength = (lengthFt * 12) + lengthIn;
           tileLength = totLength / tile;
           if (totLength % tile > 0) // rounds tileLength up
           {
                            tileLength++;
            }
           tileWidth = totWidth / tile;
           if (totWidth % tile > 0) // rounds tileWidth up
           {
                           tileWidth++;
            }
           tilesNeeded = tileLength * tileWidth;
           totalTiles = totalTiles + tilesNeeded;
           cout << "Tiles needed: " << tilesNeeded << endl;
   
           boxes = tilesNeeded / 20; // uses extras or rounds up boxes
           if (tilesNeeded % 20 > 0 && tilesNeeded % 20 < extra)
            {
                 
                 extra = extra - (tilesNeeded % 20);
            }
                 else if (tilesNeeded % 20 > 0 && tilesNeeded % 20 > extra)
            {
               boxes++;
               extra = (20 * boxes) - tilesNeeded;
            }
           totalBoxes = (totalTiles + extra) / 20;
           if (extra > 20)
           {
                      extra = extra - 20;
                      boxes = boxes - 1;
            }
           cout<<"Number of boxes needed: " << boxes << endl;
   
          
           
           cout << "Extra tiles: " << extra << endl << endl;
           count++;
   
       }
       cout << "Total Number of Tiles: " << totalTiles << endl;
       cout << "Total Number of Boxes: " << totalBoxes << endl;
       cout << "Total Extra: " << extra << endl;
       system("pause");
   return 0;
   }










Edited by on 27-02-11 12:56
Author

RE: C++ functions and tiles program

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 27-02-11 13:57
Sure, I'll give you some feedback...

i) Do you really need two functions that just do single cout statements?
ii) system("pause"); is not portable. Others have discussed various other methods to acchieve the same thing. Just go try compiling that on GC++C under Linux - it won't work.
iii) If you're going to indent, be consistent, use either TAB or single spaces don't mix them.
iv) Your function prototype "int convertInches(int, int)" is never defined and is unused, also it is not really valid syntax - as far as I know you need to specify parameter the variable names.

Answers:

i) No, so replace those function calls in the while loop with the cout statements.

ii) Use a construct such as:
Code

cout << "Press any key to continue..." << endl;
cin.ignore(1,'\x10');
while(!(cin.get() > 0)) {}





iii) N/A

iV) Function isn't used, so remove the prototype.

Otherwise a well written program.

Cleaned code

Code

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//iv)
        
int main()
{
   int rooms = 0, count = 0, tile = 0;
   int widthFt = 0, widthIn = 0;
   int lengthFt = 0, lengthIn = 0;
   int totWidth = 0, totLength = 0, tileLength = 0, tileWidth = 0;
   int extra = 0, tilesNeeded = 0, boxes = 0;
   int totalTiles = 0, totalBoxes = 0, totalLeft = 0;

   //i)
   cout << "Enter number of rooms:";
   cin >> rooms;

   //i)
   cout << "Enter size of tile in inches:";
   cin >> tile;
   
   while (count < rooms)
   {


      cout << "Enter room Width (feet and inches, separated by a space):";
      cin >> widthFt >> widthIn;


      cout << "Enter room Length (feet and inches, separated by a space):";
      cin >> lengthFt >> lengthIn;


      totWidth = (widthFt * 12) + widthIn;
      totLength = (lengthFt * 12) + lengthIn;
      tileLength = totLength / tile;

      if ( (totLength % tile) > 0) // rounds tileLength up
      {
         tileLength++;
      }

      tileWidth = totWidth / tile;
      if ( (totWidth % tile) > 0) // rounds tileWidth up
      {
         tileWidth++;
      }

      tilesNeeded = tileLength * tileWidth;
      totalTiles = totalTiles + tilesNeeded;
      cout << "Tiles needed: " << tilesNeeded << endl;

      boxes = tilesNeeded / 20; // uses extras or rounds up boxes
      if ( (tilesNeeded % 20) > 0 && (tilesNeeded % 20) < extra)
      {
          extra -= (tilesNeeded % 20);
      }
      else if ( (tilesNeeded % 20) > 0 && (tilesNeeded % 20) > extra)
      {
         boxes++;
         extra = (20 * boxes) - tilesNeeded;
      }
      totalBoxes = (totalTiles + extra) / 20;
      if (extra > 20)
      {
            extra -= 20;
            boxes -= 1;
      }

      cout << "Number of boxes needed: " << boxes << endl;
      cout << "Extra tiles: " << extra << endl << endl;
      count++;

   }

   cout << "Total Number of Tiles: " << totalTiles << endl;
   cout << "Total Number of Boxes: " << totalBoxes << endl;
   cout << "Total Extra: " << extra << endl;

   //ii)
   cout << "Press any key to continue..." << endl;
   cin.ignore(1,'\x10');
   while(!(cin.get() > 0)) {}

   return 0;

}





Hope that helps.

Jim,
Author

RE: C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 14:15
j4m32 wrote:
also it is not really valid syntax - as far as I know you need to specify parameter the variable names.

Not in the declaration; the declaration merely needs to know the function name, return value and what types the parameters are going to be.
So you can, but it's unnecessary. It would be mostly good for larger projects where you want descriptive parameter names to be in the collection of declarations for easier overview and use.

Edit:
This is consistent and thus is the same for class methods and not just standalone functions.




Edited by on 27-02-11 14:18
Author

RE: C++ functions and tiles program

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 27-02-11 14:18
Ahh, eeek!

Thanks for the clarification on that one COM!
My bad. Pfft

Jim,
Author

RE: C++ functions and tiles program


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-02-11 21:44
the only reason I had those functions in there is because it was mandatory for the program and chapter we are on in class, otherwise i wouldnt use those. Thanks for your input Jim. Seems the small time you have been here you have done alot, especially for me and I do appreciate it.


Author

RE: C++ functions and tiles program

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 27-02-11 22:49
Not a problem!

I am pretty much self taught, just read around a lot of things, as you say best you stik to your breif to get your marks though - and that's fair enough. Smile
Sorry if I come across abrupt sometimes too, I don't mean to be Grin

Jim,