reStructure of code; include 2 void functions
Jul 28, 2009 at 3:41am UTC
does anyone have any advice? i created a program and it works fine.
my teacher, however, is asking me to have my INPUT and OUTPUT to be subprograms such as the VOID function.
i guess my algorithmn has to be..
// struct
// void for inputs
// void for printing
// my main
i have no idea how to go about on doing that.
ive created programs with void functions before, but its different, a lot different.
i tried making this program into a void, but i got a whole lot of errors.
anyone have any ideas or pointers? thanks
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
#include <iostream>
#include <string>
using namespace std;
//Structure
struct student
{
int zip;
int id;
float gpa;
string name;
string address;
string city;
string state;
string gender;
}stud[3]; //Array size
int main()
{
cout<< "\n----------Enter Student Information---------\n" ;
int i = 0;
//for loop
for (i=0; i<3; i++)
{
//Input
cout << "\nFirst name of student #" << ": " ;
getline(cin, stud[i].name);
cout << "Address of student #" << ": " ;
getline(cin, stud[i].address);
cout << "City of student #" << ": " ;
getline(cin, stud[i].city);
cout << "State of student #" << ": " ;
getline(cin, stud[i].state);
cout << "Zip code of student #" << ": " ;
cin >> stud[i].zip;
cin.ignore(1000, 10);
cout << "Student gender [M,F] #" << ": " ;
cin >> stud[i].gender;
cout << "ID number of student #" << ": " ;
cin >> stud[i].id;
cin.ignore(1000, 10);
cout << "GPA of student #" << ": " ;
cin >> stud[i].gpa;
cin.ignore(1000, 10);
}
cout<< "\n----------Student Information---------\n" ;
for (i=0; i<3; i++)
{
//Output
cout << "\nName of student # " << i << ": " << stud[i].name;
cout << "\nAddress of student # " << i << ": " << stud[i].address;
cout << "\nCity of student # " << i << ": " << stud[i].city;
cout << "\nState of student # " << i << ": " << stud[i].state;
cout << "\nZip code of student # " << i << ": " << stud[i].zip;
cout << "\nGender of student # " << i << ": " << stud[i].gender;
cout << "\nID of student # " << i << ": " << stud[i].id;
cout << "\nAge of student # " << i << ": " << stud[i].gpa<<"\n" ;
}
cin.ignore();
cin.get();
return 0;
}
Jul 29, 2009 at 2:26am UTC
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
#include <iostream>
#include <string>
using namespace std;
//Structure
struct student
{
int zip;
int id;
float gpa;
string name;
string address;
string city;
string state;
string gender;
}stud[3]; //Array size
void getStudents(){
for (i=0; i<nStudents; i++)
{
//Input
cout << "\nFirst name of student #" << ": " ;
getline(cin, stud[i].name);
cout << "Address of student #" << ": " ;
getline(cin, stud[i].address);
cout << "City of student #" << ": " ;
getline(cin, stud[i].city);
cout << "State of student #" << ": " ;
getline(cin, stud[i].state);
cout << "Zip code of student #" << ": " ;
cin >> stud[i].zip;
cin.ignore(1000, 10);
cout << "Student gender [M,F] #" << ": " ;
cin >> stud[i].gender;
cout << "ID number of student #" << ": " ;
cin >> stud[i].id;
cin.ignore(1000, 10);
cout << "GPA of student #" << ": " ;
cin >> stud[i].gpa;
cin.ignore(1000, 10);
}
}
void printStudents(){
for (i=0; i< nStudents; i++)
{
//Output
cout << "\nName of student # " << i << ": " << stud[i].name;
cout << "\nAddress of student # " << i << ": " << stud[i].address;
cout << "\nCity of student # " << i << ": " << stud[i].city;
cout << "\nState of student # " << i << ": " << stud[i].state;
cout << "\nZip code of student # " << i << ": " << stud[i].zip;
cout << "\nGender of student # " << i << ": " << stud[i].gender;
cout << "\nID of student # " << i << ": " << stud[i].id;
cout << "\nAge of student # " << i << ": " << stud[i].gpa<<"\n" ;
}
}
int main()
{
int i, nStudents = sizeof (stud); //NOT ENTIRELY SURE IF SIZEOF WILL WORK :S
cout<< "\n----------Enter Student Information---------\n" ;
getStudents();
cout<< "\n----------Student Information---------\n" ;
printStudents();
cin.ignore();
cin.get();
return 0;
}
[edit] fixed the function call, I was attempting to multitask at the time >_< [/edit]
Last edited on Jul 29, 2009 at 10:01pm UTC
Jul 29, 2009 at 7:08am UTC
You are quite on track in logically splitting the functions.
Get the compilation errors away - do read up the c++ manual to know how to 'call' the functions.
You must be getting closer
good lucj
Topic archived. No new replies allowed.