arrays

closed account (LhRjy60M)
Help with arrays
Last edited on
help you can have, once you try, and once you show some of your effort.

I will get you started.

arrays are really simple. you can make an array of anything.

int x[100]; //an array of 100 integers.

struct foo
{
int x; double d; string s;
};

foo f[23]; //an array of 23 foos.

arrays are indexed from 0 to the size -1.
so the above foo array can be used

f[0].s = "hey";
f[22].x = 3;
but going out of bounds will crash the program:
f[25].d = 2.718; //crash, 25 is bigger than foo.

you cannot declare arrays with a variable:
int x = 10;
double d[x]; //not allowed, you need pointers to do this, its another topic. you can use a compile time constant, and that is highly recommended.

the c++ language vector class replaces arrays with an OOP class. As you learn them, keep this in mind, as you should not be using arrays much, if at all, once you learn vectors.


using what I just said, you should be able to make a hotel room struct or class and an array of them. see what you can do.

closed account (LhRjy60M)
alright
Last edited on
It's critical to indent your code so that it matches the actual block structure. If you do this, you'll immediately find that addCustomer(), initials() and status() are "defined" inside main().

Using incremental development...
This means that you should have gotten each part of the program working before moving on to the next part. This is a powerful development method because you get things working one part at a time. In contrast, if you try to write a large program all at once before doing any testing, then you'll likely find that nothing works and you don't know where to start looking for the problems.

Below is your code with some syntax and other errors fixed.
- Line 49, 64: you can't call main() from within your program Use a loop instead.
- Line 46: Shouldn't that be inside the "if"? And once you add a name, shouldn't you break out of the loop?
Function initials(): I think you have misinterpreted the instructions. I think this function should set all rooms to empty without any user intervention. That's how you initialize the state of the rooms.
- Line 77: You can't return a value from a function that returns void.
- main(): You'll need a loop to prompt the user for multiple commands.
- Modify the menu to accept a "Q" command to quit.
- Modify main to process all legal commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

string names[10] = { };

int n;

void dmenu();
void status();
void initails();
void addcustomer();

void
dmenu()
{
    cout << "-_-_-_-_-_-_-_- MENU _-_-_-_-_-_-_-_-" << endl;
    cout << endl;
    cout << "V: view rooms" << endl;
    cout << "A: Add customer to all rooms" << endl;
    cout << "I: Initialise room to 0" << endl;
}

char
getchoice()
{
    char choice;
    cout << "enter your choice" << endl;
    cin >> choice;
    return choice;

}

void
addcustomer()
{
    cout << endl << endl;
    for (n = 0; n <= 9; n++) {
	if (names[n] == "/0") {
	    cout << "we have a position available in room" << n +
		1 << ", please enter a name " << endl;

	}
	cin >> names[n];

    }
    // main();
}

void
initials()
{
    char e;
    for (n = 0; n <= 9; n++) {
	cout << "do you want to modify room " << n + 1 << "to empty, enter e yes" << endl;
	cin >> e;
	if (e == 'e') {
	    names[n] = "/0";
	}
    }
    cout << endl << endl;
    // main()
}

void
status()
{
    for (n = 0; n <= 9; n++) {
	if (names[n] == "/0") {
	    cout << "status of room" << n + 1 << "room is empty" << endl;
	} else {
	    cout << "Status of room" << n + 1 << "is taken " << endl;
	}
    }
    // return 0;
}

int
main(void)
{
    dmenu();
    char choice;
    choice = getchoice();

    if (choice == 'v') {
	status();
    } else if (choice == 'I') {
    }
}

Topic archived. No new replies allowed.