473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help with a void function. For school

Hey everyone... I'm in a beginning C++ class right now and below is the code I've written for my current assignment... What I need to do is put all of the input code (basically the huge chunk of stuff from main, into a void function. I'm not quite sure how to do this. I was just introduced to structs and arrays, and had trouble understanding functions when we were taught them a week or two ago so I'm a lagging a little bit. I don't expect you guys to finish my homework for me, but if someone could point me in the right direction and give me a sample or an idea of what to do, I would greatly appreciate it. Thanks, Adam.

#include <string>
#include <iostream>
using namespace std;

struct Student
{
string name;
string address;
string city;
string state;
int zip;
char gender;
int id;
float gpa;
};




void printData(Student& x)
{
cout << "Name=" << x.name << ", Address=" << x.address << ", City=" << x.city << ", State=" << x.state << ", ZIP Code=" << x.zip << ", Gender=" << x.gender << ", Student ID=" << x.id << ", GPA=" << x.gpa << endl;
}




int main()
{
Student a;
Student b;
Student c;

cout << "I need the following information for all 3 students " << endl;
cout << "Name: " ;
getline(cin, a.name);
cout << "Address: ";
getline(cin, a.address);
cout << "City: ";
getline(cin, a.city);
cout << "State: ";
getline(cin, a.state);
cout << "ZIP Code: ";
cin >> a.zip;
cin.ignore(1000,10);
cout << "Gender [M] or [F]: ";
cin >> a.gender;
cin.ignore(1000,10);
cout << "ID Number: ";
cin >> a.id;
cin.ignore(1000,10);
cout << "GPA: ";
cin >> a.gpa;
cin.ignore(1000,10);

cout << endl;
cout << endl;

cout << "Name: " ;
getline(cin, b.name);
cout << "Address: ";
getline(cin, b.address);
cout << "City: ";
getline(cin, b.city);
cout << "State: ";
getline(cin, b.state);
cout << "ZIP Code: ";
cin >> b.zip;
cin.ignore(1000,10);
cout << "Gender [M] or [F]: ";
cin >> b.gender;
cin.ignore(1000,10);
cout << "ID Number: ";
cin >> b.id;
cin.ignore(1000,10);
cout << "GPA: ";
cin >> b.gpa;
cin.ignore(1000,10);

cout << endl;
cout << endl;

cout << "Name: " ;
getline(cin, c.name);
cout << "Address: ";
getline(cin, c.address);
cout << "City: ";
getline(cin, c.city);
cout << "State: ";
getline(cin, c.state);
cout << "ZIP Code: ";
cin >> c.zip;
cin.ignore(1000,10);
cout << "Gender [M] or [F]: ";
cin >> c.gender;
cin.ignore(1000,10);
cout << "ID Number: ";
cin >> c.id;
cin.ignore(1000,10);
cout << "GPA: ";
cin >> c.gpa;
cin.ignore(1000,10);

cout << endl;
cout << endl;

printData(a);

cout << endl;

printData(b);

cout << endl;

printData(c);

return 0;
}
Nov 20 '06 #1
1 1336
sivadhas2006
142 100+
Hi Adam,

This may satisfy your need.

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct structStudent
  6. {
  7.    string strName;
  8.    string strAddress;
  9.    string strCity;
  10.    string strState;
  11.    int nZipCode;
  12.    char chGender;
  13.    int nID;
  14.    float fGPA;
  15. };
  16.  
  17. void GetData(structStudent& a)
  18. {   
  19.    cout << "Name: " ;
  20.    getline(cin, a.strName);      
  21.  
  22.    cout << "Address: ";
  23.    getline(cin, a.strAddress);
  24.  
  25.    cout << "City: ";
  26.    getline(cin, a.strCity);
  27.  
  28.    cout << "State: ";
  29.    getline(cin, a.strState);
  30.  
  31.    cout << "ZIP Code: ";
  32.    cin >> a.nZipCode;
  33.  
  34.    cout << "Gender [M] or [F]: ";
  35.    cin >> a.chGender;
  36.  
  37.    cout << "ID Number: ";
  38.    cin >> a.nID;   
  39.  
  40.    cout << "GPA: "; 
  41.    cin >> a.fGPA;   
  42. }
  43.  
  44. void PrintData(structStudent& x)
  45. {
  46.    cout << "======================================" << endl
  47.         << "Description\tValue" << endl
  48.         << "======================================" << endl
  49.         << "Name\t" << x.strName << endl
  50.         << "Address\t\t" << x.strAddress << endl
  51.         << "City\t\t" << x.strCity << endl
  52.         << "State\t\t" << x.strState << endl
  53.         << "ZIP Code\t" << x.nZipCode << endl
  54.         << "Gender\t\t" << x.chGender << endl
  55.         << "Student ID\t" << x.nID << endl
  56.         << "GPA\t\t" << x.fGPA << endl;
  57.  
  58. int main()
  59. {
  60.    structStudent 
  61.       *pStudent = NULL;
  62.    int
  63.       nIndex = 0,
  64.       nNoOfStudents = 0;
  65.  
  66.  
  67.    cout << "Enter the number of students : ";
  68.    cin >> nNoOfStudents;
  69.  
  70.    // allocate the memory.
  71.    pStudent = new structStudent[nNoOfStudents];
  72.    if(pStudent == NULL)
  73.    {
  74.       cerr << "Failed to allocate memory for the structure.";
  75.       return 1;
  76.    }
  77.  
  78.    // Get the data from the user.
  79.    for(nIndex = 0; nIndex < nNoOfStudents; nIndex++)
  80.    {
  81.       cout << "\nEnter the information of the " << nIndex + 1 << " student:-" << endl;
  82.       GetData(pStudent[nIndex]);
  83.    }
  84.  
  85.    // Display the data to the user.
  86.    for(nIndex = 0; nIndex < nNoOfStudents; nIndex++)
  87.    {    
  88.       cout << "\nThe information of the " << nIndex + 1 << " student is:-" << endl;
  89.       PrintData(pStudent[nIndex]);
  90.    }
  91.  
  92.  
  93.    // Free the memory.
  94.    if(pStudent != NULL)
  95.    {
  96.       delete [] pStudent;
  97.       pStudent = NULL;
  98.    }   
  99.  
  100.    return 0;
  101. }
  102.  
Regards,
M.Sivadhas.
Nov 20 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PB | last post by:
i have a class called school.java and i cant figure out how to construct another class called course.java that adds 2 people to a course. Can anyone help me out on how to set up constructors and...
7
by: BT | last post by:
Ok, for a school assignment we have to use a pointer for an array of ints, intstead of the usual X way, it compiles fine but when i run it I am getting a seg fault that i can't figure out how to...
1
by: Ryan Kaskel | last post by:
Hi! I am new to this newsgroup and need help implementing a hash table. This assignment is for school but only concerns one method. Basically we have to write methods like put(), get(),...
2
by: pretear_yuki | last post by:
I'm a 14 year old student in Singapore. I am doing a programming for my Research Studies in school, I got the source codes of my programme from my mentor in the National University of Singapore...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
1
by: n355a | last post by:
hi everyone....if anyone has time... can anyone help me with virtual functions... I have class student, Graduate, Undergraduate. There's a printInfo() in each class, and the one in student is...
5
by: Lockwood | last post by:
someone check out this program and tell me what im doing wrong please...every time i fix some errors i get even more than before (right now theres 10). note that im a beginner/noob, and that this...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
1
by: laila2ethan | last post by:
Need someones help , I am having a very difficult time understanding parameters and functions, can someone help me answer these questions 1. Write a program that helps an elementary school student...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.