Members Online
Total Online: 21 Web Spiders: 15
Guests Online: 21
Members Online: 0
Registered Members: 70170 Newest Member: bahmx
|
View Thread
| Author |
C++ functions and tiles program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
line38: expected primary-expresblockedsion 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;
}

Edited by vegeta_man111 on 27-02-11 02:49 |
|
| Author |
RE: C++ functions and tiles program |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
You can't name a variable "int".
|
|
| Author |
RE: C++ functions and tiles program |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
ynori7 wrote:
You can't name a variable "int".
There's plenty more wrong here than just that.
For instance, what good is this?
void convertInches(int ft, int in)
{
in = ft*12 + in;
}
...
totWidth = convertInches(widthFt, widthIn);
How do you not get an error on that?
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C++ functions and tiles program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
|
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: God |
|
You don't, as far as I can see. I think ynori7 means this:
rooms = getInteger(int);
When you call a function, you should provide variable names as arguments, not just a type:
int bla;
rooms = getInteger(bla);
The above is still wrong however, because the function is completely wrong, as COM already pointed out.
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:
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:
int foo = getInteger();
Other possibility:
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 |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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;
}

Edited by vegeta_man111 on 27-02-11 12:56 |
|
| Author |
RE: C++ functions and tiles program |
j4m32
Member
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
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, |
|
| Author |
RE: C++ functions and tiles program |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
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 descblockedriptive 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.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Edited by COM on 27-02-11 14:18 |
|
| Author |
RE: C++ functions and tiles program |
j4m32
Member
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
Ahh, eeek!
Thanks for the clarification on that one COM!
My bad. 
Jim, |
|
| Author |
RE: C++ functions and tiles program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
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 
Jim, |
|
|
|
|