Jun 27, 2011 at 4:29pm UTC
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
struct first
{
int customer [100];
};
void inrecords (first data);
int main ()
{
first newcustomer;
inrecords (newcustomer);
}
void inrecords (first data)
{
cout << "testing";
system ("pause");
return;
}
Jun 27, 2011 at 4:41pm UTC
How?? I don't get it. I'm using structures and functions. How can I fix this problem??
Jun 27, 2011 at 4:50pm UTC
You never initialize the member customer in your struct "first". At least do a memset on it:
memset( customer, 0, sizeof ( customer ) );
Last edited on Jun 27, 2011 at 4:55pm UTC
Jun 27, 2011 at 4:50pm UTC
first newcustomer;
That creates an object of type first , named newcustomer . You have to give it a value.
It's like saying
int x;
What's the value of x now? No idea; we have to set it to some value.
first newcustomer;
newcustomer.customer[0] = 1;
Now we have set one of the values. 99 left to set. Good luck.
Last edited on Jun 27, 2011 at 4:51pm UTC
Jun 27, 2011 at 4:54pm UTC
Right: What Moschops said. When you create an object its constructor gets called. Since you weren't initializing first's member variables, the compiler complained.
Jun 27, 2011 at 5:37pm UTC
oh okay, but how come my other program works fine. I coded it just like my previous one.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
ifstream infile;
ofstream outfile;
struct A
{
string name [100];
int telephone [100];
int birth [100];
string address [100];
};
void inrecords (A data, int count, bool switch1);
void userinput (A data, int count, bool switch1);
int main ()
{
A records;
string outputfile;
string inputfile;
char ask;
char rerun;
bool switch1 = false;
int count = 0;
inrecords (records, count, switch1);
userinput (records, count, switch1);
}
void inrecords (A data, int count, bool switch1)
{
int counter;
if (switch1 = true)
{
for(counter = 0; counter < count; counter++)
{
cout << data.name << endl;
cout << data.address << endl;
cout << data.telephone << endl;
cout << data.birth << endl;
}
}
return;
}
void userinput (A data, int count, bool switch1)
{
char check = 'n';
int counter = 0;
do
{
cout << "NAME:\t" << setw(10);
cin >> data.name[counter];
cout << "" << endl;
cout << "TELEPHONE:\t";
cin >> data.telephone[counter];
cout << "" << endl;
cout << "BIRTHDATE:\t" << setw(10);
cin >> data.birth[counter];
cout << "" << endl;
cout << "ADDRESS:\t" << setw(10);
cin >> data.address[counter];
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "Would you like to input another person?";
cin >> check;
counter ++;
}
while (check == 'y' || check == 'Y');
return;
}
Jun 27, 2011 at 5:53pm UTC
It doesn't work fine.
if (switch1 = true)
Did you mean
if (switch1 == true) ?
Ignoring that mistake, you have set count to zero, so your function inrecords doesn't output anything, which is lucky for you because at that point in your code you've done the exact same as in your first code - not put anything in the variable and tried to use it anyway.
Jun 27, 2011 at 6:14pm UTC
Ohhh okay. Thanks for your help dude. Here's what I did. I initialized every element in array by 0, and it works now.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
struct first
{
int customer [100];
};
void inrecords (first data);
int main ()
{
first newcustomer;
int count;
for (count = 0; count < 100; count++)
{
newcustomer.customer[count] = 0;
}
inrecords (newcustomer);
}
void inrecords (first data)
{
cout << "testing";
system ("pause");
return;
}