The variable 'newcustomer' is being used without being initialized.

#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;
}
It certainly is!
How?? I don't get it. I'm using structures and functions. How can I fix this problem??
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
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
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.
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;
}



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.
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;
}
Topic archived. No new replies allowed.