| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Compilation error and question in regards to iteration
Yeah, I know it's common for a newbie to start a "typical newbie thread." However, I'm not your typical newbie (well, sorta, but you'll get my drift later on). Err, okay!
Anyway, I am fairly new (read: BRAND new) to programming in C source. I've actually had a lot of fun with the semester so far. My favorite aspect of programming is the fact that you can pretty much mold any code, whether it be loops, functions, string literals, arrays, etc., into cooperative structural form. Basically what I'm saying is, you can do ANYTHING with programming, and though I'm still a "hatchling" (since I just now started in C), I think it's fun. Anyway, I'm glad I found this site. I've been searching for help for a few days now, over some (hopefully) simple stuff. Maybe it's stuff I overlooked, I'm not sure. Hopefully someone will be able to help me briefly; I promise not to be too much of a burden! My instructions were to create a program that did the following: (1) calculate hotel occupancy as a percentage, (2) how many floors the hotel has, (3) how many guest rooms on each floor, (4) how many OCCUPIED guest rooms on each floor, and (5) it is not allowed to accept a number of floors entry of less than 1 or less than 10 rooms on any floor. Along with this is something I've had a problem with. Since it's traditional that hotels do not include a 13th floor, we're instructed to set the loop in the iteration to SKIP the entire 13th iteration. As I said, I'm fairly new to this, and the only thing that rings a bell is the loop control variable, which, I was told, was partially true. So anyway, here's my program (that also doesn't compile for some reason), and any help is appreciated (forgive me if I messed anything up by pasting this): Code:
char Again = 'y';
int main()
{
while (Again == 'y')
{
HotelCalculations();
print "Do again? y/n\n";
read Again;
}
}
void HotelCalculations()
{
int temp = 0;
int temp2 = 0;
int count = 2;
int FloorOccupancy = 0;
int roomsOccupied = 0;
int rooms = 0;
int floors = 0;
int HotelOccupancy = 0;
print "How many floors are in the hotel?\n";
print "Floors?\n";
read floors;
if (floors > 1)
{
print "Since the lobby is on the first floor, the 1st floor will be skipped.\n";
while (count <= floors)
{
print "How many guest rooms on floor " + count +".\n";
print "Rooms?\n";
read temp;
if (temp >= 10)
{
print "How many of out of the " + temp + " guest rooms are occupied?\n";
read temp2;
if (temp >= temp2)
{
rooms = rooms + temp;
roomsOccupied = roomsOccupied + temp2;
FloorOccupancy = (temp2 * 100)/temp;
print "Floor " + count + " has " + FloorOccupancy + "% Occupancy.\n";
count = count + 1;
}
else
{
print "ERROR -- Rooms occupied is greater than the number of rooms on floor " + count + ".\n";
}
}
else
{
print "ERROR -- Need 10 or more rooms on each floor.\n";
}
}
}
else
{
print "ERROR -- " + floors + " floor is an invalid entry.\n";
}
if (count - 1 == floors && count - 1 > 1)
{
HotelOccupancy = (roomsOccupied * 100)/rooms;
print "The occupancy of the hotel is " + HotelOccupancy + "%.\n";
}
}
My compiler returns the following: "Error: non-function call value". Not only that, but I don't understand how else to "skip" the 13th iteration in the loop without using a loop control variable. I don't know why it's so important, but it's part of the instruction, so I have to do it. Again, any help is appreciated, and a friendly hello to all you happy programmers. |
|
#2
|
|||
|
|||
|
[QUOTE Anibal]
PS: Always remember to use the # on the edition bar which wraps the text between the code tags, making the code readable and tabed! [/QUOTE] In the name of the Lord....doesn't anybody ever read? Use the code tags. How can we read a code that is not tabbed? Come on!! Anibal. |
|
#3
|
|||
|
|||
|
Seems I've run into yet another problem. "typedef" isn't recognized by my compiler (VCflat). Visible C-flat is the compiler my instructor suggested we use, since he's the one who created it.
I understand the purpose of typedef, to define a structure and to also create several data types, but if the compiler doesn't recognize it, then I have to find another way around it. Believe me, I had the entire code written out, and just as I hit "compile," 4 hours of hard work = BYE BYE! ![]() Argh, I still need help. :-/ |
|
#4
|
|||
|
|||
|
Ask your intrsuctor for typede substitute.
Are all other directives supportted by your C-Flat compiler? |
|
#5
|
|||
|
|||
|
Yeah, any other directives are supported by the compiler. I just get an error when trying to compile with "typedef" since it's expecting a type-declaration.
|
|
#6
|
|||
|
|||
|
The only purpose of typedef is to define a new type of data. Instead, just use struct like this:
Code:
struct rooms{
int roomsPerFloor;
int emptyPerFloor;
}
Off course, when referenced use struct rooms instead of rooms like this: Code:
struct rooms <function name>{
struct rooms thisRooms;
...............................
...............................
...............................
}
Struct is ANSI. If that doesn't work, tell your instructor to get himself another compiler and throw that away!! Anibal. |
|
#7
|
|||
|
|||
|
Addition to Anibal's advice. For all user defined types, generate a seperate and dedicated header file.Include that file while using user defined type.
It will make things easier for you. |
|
#8
|
|||
|
|||
|
Thanks for the help, but I still don't quite understand how to make this work.
Before I explain, here's a somewhat edited version of the program: Code:
// occupancy percentage
void procedure()
{
int rooms = 0;
int occupied = 0;
int occupied2 = 0;
int calc = 0;
int rooms2 = 0;
int floors = 0;
int var2 = 0;
int var = 2;
print "\nNumber of floors in hotel?\n";
read floors;
if(floors <= 1)
{
print " \nPlease enter more than 1.\n";
}
else
{
print "\nFirst floor has no rooms & will be skipped.\n";
}
while(var <= floors)
{
print "Rooms on floor " + var +":\n";
read rooms;
if(rooms >= 10)
{
print "Number occupied out of " + rooms + ":\n";
read rooms2;
}
else
{
print "Each floor must have at least 10 rooms.\n";
}
if(rooms >= rooms2)
{
var2 = var2 + rooms;
occupied = occupied + rooms2;
calc = (rooms2 * 100)/rooms;
print "\nPercentage occupancy for room " + var + " is " + calc + "%.\n";
var = var + 1;
if(var == 13)
{
print "Floor 13 will be skipped.\n";
var = var + 1;
}
if(var - 1 == floors && var - 1 > 1)
{
occupied2 = (occupied * 100)/rooms;
print "Occupancy is " + occupied2 + "%.\n";
}
}
}
}
void main()
{
char symbol = 'n';
char doagain = 'y';
while(doagain == 'Y' || doagain == 'y')
{
if(symbol == 'N' || symbol == 'n')
{
doagain = 'n';
}
else
{
doagain = 'y';
}
procedure();
print "Try again?\n";
read doagain;
}
}
Well as you can see, I didn't particularly achieve much. A lot of it's due to the fact that, not only does our instructor's compiler fail to recognize structured data types, but certain operators that'd make this a lot easier (for example, the member operator, which is nothing more than a simple "."). I also don't have much experience with array declarations and passing them into the functions. Earlier today, I experimented with: int floors; int rooms = 0; int counter = 0; print "Number of hotel floors?\n"; read floors; int roomsAry[floors]; And that's as far as I got. I didn't get much help from the text, as it doesn't clearly (or completely) explain the process of delcaring an array to be passed to a calculating function. For example, it would be one thing if I had: someValue[1] is 25 someValue[2] holds 36 someValue[3] holds 47 Those are defined elements. What I'm trying to say is that I don't understand how to create an array to pass to the calculating function without having a defined element. Ugh, hopefully someone understands me. :-( Any help is greatly appreciated. |
|
#9
|
|||
|
|||
|
My god!! USE THE CODE TAGS (#)........why doesn't anybody read? It's imposible to read the code that way!!
Example: Code:
#include <stdio.h>
#include <stdlib.h>
int *setRooms(int);
int main(){
int *blabla;
int floors;
printf("How many floors mate:?");
scanf("%d", &floors);
blabla = setRooms(floors);
/* now you have blabla as an int array with as many floors as entered.
You can do with it as you pleased!...call a function to process it, for example */
}
//this useless function creates the array...you could as well fill it with the info. Hell I don't know.
int *setRooms(int floors){
int *myArray;
myArray = (int*) malloc(floors);
/*do whatever you want with the array!! Process it, eat it.....fold it and save it in the closed */
return myArray;
}
Good Luck! ANIbal. |
|
#10
|
|||
|
|||
|
What's to read? I read the FAQ and saw nothing about code tags. It's not a huge deal, but thanks for the help anyway.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Compilation error and question in regards to iteration |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|