I am in the beginning of my studies. I am now self-teaching structures.
Here's a code I wrote:
#include <iostream>
using namespace std;
struct buscard
{
char a;
int x, y;
};
int main()
{
int num, i, x, y;
char a;
cout << "Enter the number of records: ";
cin >> num;
buscard db[num-1];
for (i=0; i<num; i++)
{
cout << "Enter record title: ";
cin >> a;
cout << "Enter record x: ";
cin >> x;
cout << "Enter record y: ";
cin >> y;
db[i].a = a;
db[i].x = x;
db[i].y = y;
}
cout << "Presenting records: \n";
for (i=0; i<(num-1); i++)
{
a = db[i].a;
x = db[i].x;
y = db[i].y;
cout << "Record #" << i+1 << " " << a << ": " << x << ", " << y << ".\n";
}
}
What it SHOULD DO is:
1. You are asked the number of "records" you are about to enter (each record contains a title - a letter and two numbers - x and y).
2. Then, it lets you fill in the records you created with data.
3. When you are done, it prints all the records.
Troubles:
1. When I type 1 as the number of records, it asks me to fill two records and it presents 1 records.
2. When I type 3 as the number of records, it asks me to fill out 3 records but it prints 4 records (the 4'th being gibberish) and the 3'rd record is bad (the y value is incorrect).
sorry, I solved the problem, I forgot that arrays are defined as n cell-big by array[n] and I got confused and I manipulated the array definition icorrectly.
If the user asked to enter 6 records, why are you only allocating space for 6 - 1 records?
The second is:
for( i = 0; i < (num-1); i++ )
If the user asked to enter 6 records (num==6), then this loop outputs
records #0, 1, 2, 3, and 4, because when i == 5, 5 is not strictly less
than 6 - 1.