473,569 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a void function. For school

1 New Member
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(Stude nt& 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 1346
sivadhas2006
142 New Member
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
1949
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 such. Here is school.java import java.text.DecimalFormat; public class School {...
7
1469
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 fix. It occurs at this line: *d = rand() % 99 + 1 Here is the code for the first part of the program, the line that causes the seg fault is
1
2538
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(), expandCapacity(), etc. The hash table is an array of pointer to HashNodes. Below is the header file for the HashTable and HashNode classes: > #ifndef...
2
7467
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 Infocomm Research department. However, when I tried to compile the codes, I get the error message that says: "no matching function for call to...
66
5315
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 occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of...
1
1568
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 made pure virtual. I had to create a vector of students that had a combination of Graduates and Undergraduates in main. Then I had to pass the vector...
5
2228
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 doesnt work when i click run java application, either. im not sure what exactly i should put in the main method either... import javax.swing.*;...
2
9684
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 constructor such that each test score is assumed to initially be zero. Provide a method called setTestScore that accepts two parameters: the test number (1...
1
2816
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 learn multiplication. Use rand to produce two positive one-digit integers. It should then type a question such as How much is 6 times 7?...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.