-
Structs
im having some trouble with this code im learning from. Maby some one can help. I mean i understand it all but i cant figure out what is wrong
anyways here it is:
#include "stdafx.h"
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include "STRUCTUR.h"
void displayRecord(int, mailingListRecord mlRec);
int main(int argc, char* argv[])
{
//
// create an array of mailingListRecord structures
//
mailingListRecord listArray[3];
cout << endl;
int index = 0;
//
// get three records
//
do {
cout << "First Name: ";
cin.getline(listArray[index].firstName,
sizeof(listArray[index].firstName)-1);
cout << "Last Name: ";
cin.getline(listArray[index].lastName,
sizeof(listArray[index].lastName)-1);
cout << "Adress: ";
cin.getline(listArray[index].address,
sizeof(listArray[index].address)-1);
cout << "City: ";
cin.getline(listArray[index].city,
sizeof(listArray[index].city)-1);
cout << "State: ";
cin.getline(listArray[index].state,
sizeof(listArray[index].state)-1);
char buff[10];
cout << "Zip: ";
cin.getline(buff, sizeof(buff)-1);
listArray[index].zip = atoi(buff);
index++;
cout << endl;
} while (index<3);
//
// clear the screen
//
clrscr();
//
// display the three records
//
for (int i=0;i<3;i++) {
displayRecord(i, listArray[i]);
}
//
// ask the user to choose a record
//
cout << "Choose a Record: ";
char rec;
//
// be sure only 1,2 or 3 was selected
//
do {
rec = getch();
rec -= 49;
} while (rec < 0 || rec > 2);
//
// assign the selected record to a temporary variable
//
mailingListRecord temp = listArray[rec];
clrscr();
cout << endl;
//
// display selected record
//
displayRecord(rec, temp);
cout << endl;
return 0;
}
void displayRecord(int num, mailingListRecord mlRec)
{
cout << "Record " << num + 1 << ":" << endl;
cout << "Name: " << mlRec.firstName << " ";
cout << mlRec.lastName;
cout << endl;
cout << "Adress: " << mlRec.address;
cout << " ";
cout << mlRec.city << ", ";
cout << mlRec.state << " ";
cout << mlRec.zip;
cout << endl << endl;
}
and here is the header;
#ifndef _STRUCTUR_H
#define _STRUCTUR_H
struct mailingListRecord {
char firstName[20];
char lastName[20];
char address[50];
char city[20];
char state[20];
int zip;
};
#endif
its weird everything seems to check out
-
whats your error?
what error are you getting when you run your program?
or does it run and just display the wrong values?
i just started programming too.. so not sure how much i can help but...
i try to avoid char when using strings but maybe an easier way you could write your declarations is...
char firstName[3][20]; // firstName will have 3 fields each can
// contain a string of 20 chars.
mailingListRecord listArray;
cout << "Testing";
cin.getline(listArray.lastName[0], 20); //change 0 to 1 or 2 to fill
//3 names.
cout << listArray.lastName[0]; //should display "Testing"
i can't remember but i made a similar program before..
and when i tried to define an 'ARRAY' as 'CHAR' like
char array[3];
it wouldn't work. but when i used
char array[3][50];
it worked..
your program might work.. i've never tried to declare an array as struct which contains arrays of chars...
don't know what your program problem was so i probably going off on something unrelated.. but oh well.... this may help someone..
-
everything but clrscr()
i found that i could compile and run this program no prob .....well kinda..... if i just remove the clrscr()
but as for the arrays, the point of the whole program was how to make arrays of structs and handle individual char arrays
when i do actually get it to run using alternate soloutions than clrscr() that part of the code seems to work fine its clrscr() is the culprit.......why though?
-
your compiler probably doesn't support clrscr(). Many compilers do not support conio.h, which is where I believe clrscr() is. In particular, I don't VC++ doesn't support conio.h and since you are using a header specific for VC++ (stdafx.h) although you don't indicate you are using VC++, I suspect you are.
-
Yeah, all the code works fine.. That unregistered person was going off on some tangents, lol :)
This is what I remember about clrscr( ). clrscr( ) is supported by DOS compilers. The only non-DOS compilers that support it's use are the Borland compilers. So if you want to compile this code with the clrscr( )'s, go download Borland Turbo C 2.X (it's free and it's a DOS compiler).
Have fun
Oh yeah, when I compiled the code, I took out the stdafx.h header b/c I saw no use for it. Also, try and indent :) it's good practice and makes code a little more readable.
-
ya i did alot of cutting and pasting of the code back and forth between compilers so there might of been some extras I did originally started it out on vc++
i will try and use borlands but im afraid ill have to learn a whole new enviroment again but oh well ill just bite the bullet
oh ya my code did look all nice untill i posted it here and it rearranged it all well not rearranged but layed it out differently
thanks for all help!!
-
if ya wanna stay with VC++ just learn another way to clear the screen. There are several ways listed in the FAQ area.