Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ functions and tiles program


ghost's Avatar
0 0

line38: expected primary-expression before "int"


    
    #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;
	}




ynori7's Avatar
Future Emperor of Earth
0 0

You can't name a variable "int".


ghost's Avatar
0 0

ynori7 wrote: You can't name a variable "int". There's plenty more wrong here than just that. For instance, what good is this?

{
    in = ft*12 + in;
}
...
totWidth = convertInches(widthFt, widthIn);```
How do you not get an error on that?

ghost's Avatar
0 0

ynori7 wrote: You can't name a variable "int".

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


GTADarkDude's Avatar
Member
0 0

You don't, as far as I can see. I think ynori7 means this: markuprooms = getInteger(int); When you call a function, you should provide variable names as arguments, not just a type:

rooms = getInteger(bla);```
The above is still wrong however, because the function is completely wrong, as COM already pointed out.
```markupvoid 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:
```markupint 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:
```markupint foo = getInteger();```

Other possibility:
```markupvoid 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.

ghost's Avatar
0 0

Edit: Fixed, and updated code! tell me what you guys think!

    
    #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;
	}



j4m32's Avatar
Member
0 0

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:

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

#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,


ghost's Avatar
0 0

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.


j4m32's Avatar
Member
0 0

Ahh, eeek!

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

Jim,


ghost's Avatar
0 0

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.


j4m32's Avatar
Member
0 0

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. :) Sorry if I come across abrupt sometimes too, I don't mean to be :D

Jim,