My program seems to only be running through 2 courses then it ceases and displays the second course 3 times.
I am stumped and pulling my hair out, any suggestions would help a lot.
I also apologize in advance for the long post but I feel all parts are needed to identify the problem.
Here's the current output:
Current output
My expected output is:
Title: Title Course ID: 12345 Session: 54321 Units: 1.2
Here's where the error is:
#include <fstream>
#include <string>
#include <iomanip>
#include "Course.h"
using namespace std;
int main()
{
string title;
double units;
int course;
int session;
cout << "Enter the title of a course:";
cin >> title;
cout << "Enter the course ID number";
cin >> course;
cout << "Enter the number of units for the course";
cin >> units;
cout << "Enter the session number for the course";
cin >> session;
Courses Class_1(title, session, units, course);
cout << endl << "Class title: " << Class_1.getTitle() << endl <<
"title: " << setw(5) << Class_1.getTitle() <<
setw(5) << "course ID: " << setw(5) << Class_1.getCourseID() <<
setw(5) << "Session number: " << setw(5) << Class_1.getSessionNumber() <<
setw(5) << "Units: " << setw(5) << Class_1.getNumOfUnits() << endl;
cout << "Enter the title of a course:";
cin >> title;
cout << "Enter the course ID number";
cin >> course;
cout << "Enter the number of units for the course";
cin >> units;
cout << "Enter the session number for the course";
cin >> session;
Courses Class_2;
Class_2.setTitle(title);
Class_2.setCourseID(course);
Class_2.setSessionNumber(session);
Class_2.setNumOfUnits(units);
cout << endl << "Class title: " << setw(5) << Class_2.getTitle() << endl <<
"title: " << setw(5) << Class_2.getTitle() <<
setw(5) << "course ID: " << setw(5) << Class_2.getCourseID() <<
setw(5) << "Session number: " << setw(5) << Class_2.getSessionNumber() <<
setw(5) << "Units: " << setw(5) << Class_2.getNumOfUnits() << endl;
cout << Class_1.getTitle() << endl;
// -----------------------------------------------------------------
Courses Class_3;
Class_3.setTitle(title);
Class_3.setCourseID(course);
Class_3.setSessionNumber(session);
Class_3.setNumOfUnits(units);
cout << endl << "Class title: " << setw(5) << Class_3.getTitle() << endl <<
"title: " << setw(5) << Class_3.getTitle() <<
setw(5) << "course ID: " << setw(5) << setw(5) << Class_3.getCourseID() <<
setw(5) << "Session number: " << setw(5) << setw(5) << Class_3.getSessionNumber() <<
setw(5) << "Units: " << setw(5) << Class_3.getNumOfUnits() << endl;
cout << Class_2.getTitle() << endl;
// -----------------------------------------------------------------------
Courses Class_4;
Class_4.setTitle(title);
Class_4.setCourseID(course);
Class_4.setSessionNumber(session);
Class_4.setNumOfUnits(units);
cout << endl << "Class title: " << setw(5) << Class_4.getTitle() << endl <<
"title: " << setw(5) << Class_4.getTitle() <<
setw(5) << "course ID: " << setw(5) << Class_4.getCourseID() <<
setw(5) << "Session number: " << setw(5) << Class_4.getSessionNumber() <<
setw(5) << "Units: " << setw(5) << Class_4.getNumOfUnits() << endl;
cout << Class_3.getTitle() << endl;
system("pause");
return 0;
}
Here's the header:
#pragma once
#include <iostream>
#include <string>
using namespace std;
#ifndef COURSES_H
#define COURSES_H
class Courses
{
public:
// Default Constructor
Courses();
// Ovverload Constructor
Courses(string title, int course, int session, double units);
// Destructor
~Courses();
int getCourseID() const;
// Gets the course ID.
int getSessionNumber() const;
// Gets the session number.
double getNumOfUnits() const;
// Gets the number of units.
string getTitle() const;
// Gets the title of a course.
// --------------------------------
void setCourseID(int);
// Sets the ID number of a course.
void setSessionNumber(int);
// Sets the session number of a course.
void setNumOfUnits(double);
// Sets the number of units of a course.
void setTitle(string);
// Sets the title of a course.
private:
// Member Variables
string newTitle;
int newCourseID;
int newSessionNumber;
double newNumOfUnits;
};
#endif // !COURSE_H
And here's Course.cpp
#include "Course.h"
Courses::Courses()
{
newCourseID = 0;
newSessionNumber = 0;
newNumOfUnits = 0.0;
}
Courses::Courses(string title, int course, int session, double units)
{
newTitle = title;
newCourseID = course;
newSessionNumber = session;
newNumOfUnits = units;
}
Courses::~Courses()
{
}
string Courses::getTitle() const
{
return newTitle;
}
int Courses::getCourseID() const
{
return newCourseID;
}
double Courses::getNumOfUnits() const
{
return newNumOfUnits;
}
int Courses::getSessionNumber() const
{
return newSessionNumber;
}
// -------------------------------------
void Courses::setTitle(string title)
{
newTitle = title;
}
void Courses::setCourseID(int course)
{
newCourseID = course;
}
void Courses::setNumOfUnits(double units)
{
newNumOfUnits = units;
}
void Courses::setSessionNumber(int session)
{
newSessionNumber = session;
}
Your constructor is declared as Courses(string title, int course, int session, double units)
but you're calling it as Courses Class_1(title, session, units, course);
just switch around the variables and it should be fine
Courses Class_1(title,course,session,units);
Related
I'm currently working on an exercise given to me by my university professor and one of the questions is to create a program using data structures which inputs data of any topic and outputs them in a neat format. The problem is the question specifically states to not use arrays, and shorter codes grants bonus marks. How should I shorten my code below?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct musicRecord //structure name
{
//structure members
string title;
string artist;
string album;
string genre;
string releaseYear;
};
int main()
{
int numRecords;
musicRecord music1, music2, music3, music4, music5; //structure variables
//input
cout << "===================================================";
cout << "\nRecord No. 1"<< endl;
cout << "\nEnter the title : ";
getline(cin, music1.title);
cout << "\nEnter the artist's name : ";
getline(cin, music1.artist);
cout << "\nEnter the album name : ";
getline(cin, music1.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music1.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music1.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 2" << endl;
cout << "\nEnter the title : ";
getline(cin, music2.title);
cout << "\nEnter the artist's name : ";
getline(cin, music2.artist);
cout << "\nEnter the album name : ";
getline(cin, music2.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music2.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music2.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 3" << endl;
cout << "\nEnter the title : ";
getline(cin, music3.title);
cout << "\nEnter the artist's name : ";
getline(cin, music3.artist);
cout << "\nEnter the album name : ";
getline(cin, music3.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music3.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music3.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 4" << endl;
cout << "\nEnter the title : ";
getline(cin, music4.title);
cout << "\nEnter the artist's name : ";
getline(cin, music4.artist);
cout << "\nEnter the album name : ";
getline(cin, music4.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music4.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music4.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 5" << endl;
cout << "\nEnter the title : ";
getline(cin, music5.title);
cout << "\nEnter the artist's name : ";
getline(cin, music5.artist);
cout << "\nEnter the album name : ";
getline(cin, music5.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music5.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music5.releaseYear);
//output
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
cout << endl << "================================================================================================================";
cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 1";
cout << setw(27) << left << music1.title;
cout << setw(21) << left << music1.artist;
cout << setw(20) << left << music1.album;
cout << setw(20) << left << music1.genre;
cout << setw(15) << left << music1.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 2";
cout << setw(27) << left << music2.title;
cout << setw(21) << left << music2.artist;
cout << setw(20) << left << music2.album;
cout << setw(20) << left << music2.genre;
cout << setw(15) << left << music2.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 3";
cout << setw(27) << left << music3.title;
cout << setw(21) << left << music3.artist;
cout << setw(20) << left << music3.album;
cout << setw(20) << left << music3.genre;
cout << setw(15) << left << music3.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 4";
cout << setw(27) << left << music4.title;
cout << setw(21) << left << music4.artist;
cout << setw(20) << left << music4.album;
cout << setw(20) << left << music4.genre;
cout << setw(15) << left << music4.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 5";
cout << setw(27) << left << music5.title;
cout << setw(21) << left << music5.artist;
cout << setw(20) << left << music5.album;
cout << setw(20) << left << music5.genre;
cout << setw(15) << left << music5.releaseYear;
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU";
cout << endl << "================================================================================================================";
return 0;
}
As you might notice, you copy/paste code for each of your variables.
So create function to avoid to duplicate code, for example:
musicRecord inputMusicRecord(int recordNb)
{
musicRecord res;
cout << "===================================================";
cout << "\nRecord No. " << recordNb << endl;
cout << "\nEnter the title : ";
getline(cin, res.title);
cout << "\nEnter the artist's name : ";
getline(cin, res.artist);
cout << "\nEnter the album name : ";
getline(cin, res.album);
cout << "\nEnter the genre of the title : ";
getline(cin, res.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, res.releaseYear);
return res;
}
void print(const musicRecord& music)
{
cout << endl << setw(12) << left << " 1";
cout << setw(27) << left << music.title;
cout << setw(21) << left << music.artist;
cout << setw(20) << left << music.album;
cout << setw(20) << left << music.genre;
cout << setw(15) << left << music.releaseYear;
}
And then, you might do:
void print_header()
{
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
cout << endl << "================================================================================================================";
cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
}
void print_separator()
{
cout << endl << "----------------------------------------------------------------------------------------------------------------";
}
void print_footer()
{
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU";
cout << endl << "================================================================================================================";
}
int main()
{
const musicRecord music1 = inputMusicRecord(1);
const musicRecord music2 = inputMusicRecord(2);
const musicRecord music3 = inputMusicRecord(3);
const musicRecord music4 = inputMusicRecord(4);
const musicRecord music5 = inputMusicRecord(5);
print_header();
print_separator();
print(music1);
print_separator();
print(music2);
print_separator();
print(music3);
print_separator();
print(music4);
print_separator();
print(music5);
print_footer();
}
I am having trouble with my code not reading the whole string name.
I have tried using getline(cin, movieName) as well as cin.ignore(), but I can't get it to work properly.
I am taking a beginner's course to this class, but my professor does not answer their emails.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string movieName;
const float adultTicketPrice = 12;
const float childTicketPrice = 6;
const float percentTheaterKeeps = .20;
int adultTicketsSold, childTicketsSold;
float grossProfit, netProfit, amountPaidToDistributor;
getline(cin, movieName);
cout << fixed << showpoint << setprecision(2);
cout << "What movie played tonight?\t ";
cin >> movieName;
cout << "How many adult tickets were sold? ";
cin >> adultTicketsSold;
cout << "How many child tickets were sold? ";
cin >> childTicketsSold;
grossProfit = adultTicketPrice * adultTicketsSold + childTicketPrice * childTicketsSold;
netProfit = grossProfit * percentTheaterKeeps;
amountPaidToDistributor = grossProfit - netProfit;
cout << setw(10) << left << "Movie Name";
cout << setw(23) << right << movieName << endl;
cout << setw(10) << left << "Adult Tickets Sold";
cout << setw(16) << right << adultTicketsSold << endl;
cout << setw(10) << left << "Child tickets sold";
cout << setw(16) << right << childTicketsSold << endl;
cout << setw(10) << left << "Gross Box Office Profit";
cout << setw(11) << right << grossProfit << endl;
cout << setw(10) << left << "Net Box Office Profit";
cout << setw(13) << right << netProfit << endl;
cout << setw(10) << left << "Amount Paid to distributor";
cout << setw(8) << right << amountPaidToDistributor << endl << endl;
return 0;
}
You need to use std::getline(std::cin, movieName) when you want to get input for movieName.
Presumably you're seeing a blank line when you start your program, and pressing enter. This sets movieName to '\n'. Remove that line, and switch std::cin >> movieName; with std::getline(std::cin, movieName). (Remove the std::s if you're using a using declaration)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct football_game
{
string visit_team;
int home_score;
int visit_score;
};
void printMenu();
int main()
{
int i, totalValues = 0;
ifstream inputFile;
string temp = "";
inputFile.open("games.txt");
if (!inputFile)
{
cout << "Error opening Input file!" << endl;
exit(101);
}
inputFile >> totalValues;
getline(inputFile, temp);
cout << " *** Football Game Scores *** " << endl << endl;
cout << " * Total Number of teams : " << totalValues << endl << endl;
football_game* records = new football_game[totalValues];
// while (!inputFile.eof())
// {// == NULL) {
for (i = 0; i < totalValues; i++)
{
getline(inputFile, records[i].visit_team);
cout << records[i].visit_team << endl;
inputFile >> records[i].home_score >> records[i].visit_score;
cout << records[i].home_score << " " << records[i].visit_score << endl;
getline(inputFile, temp);
}
//}
cout << endl;
int choice = 0;
int avg_home_Score = 0;
int avg_visit_Score = 0;
printMenu(); // prints menu
cout << "Please Enter a choice from the Menu : ";
cin >> choice;
cout << endl << endl;
while (true)
{
switch (choice)
{
case 1:
cout << " Score Table " << endl;
cout << " ***********************" << endl << endl;
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
for (int i = 0; i < totalValues; i++)
{
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|' << setw(7)
<< right << records[i].visit_score << " " << '|' << endl;
}
cout << endl << endl << endl;
break;
case 2:
{
string team_name;
cout << "Enter the Team Name : ";
cin >> team_name;
for (int i = 0; i < totalValues; i++)
{
if (records[i].visit_team == team_name)
{
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|'
<< setw(7) << right << records[i].visit_score << " " << '|'
<< endl;
}
}
cout << endl;
break;
}
case 3:
{
for (int i = 0; i < totalValues; i++)
avg_home_Score += records[i].home_score;
cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
break;
}
case 4:
{
for (int i = 0; i < totalValues; i++)
avg_visit_Score += records[i].visit_score;
cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
break;
}
default:
{
cout << "Please enter valid input !!" << endl;
break;
}
}
printMenu();
cin >> choice;
}
return 0;
}
void printMenu()
{
cout << " Menu Options " << endl;
cout << " ================ " << endl;
cout << " 1. Print Information of all Games[Table Form] " << endl;
cout << " 2. Print Information of a Specific Game " << endl;
cout << " 3. Print Average points scored by the Home Team during season" << endl;
cout << " 4. Print Average points scored against the Home Team" << endl << endl << endl;
}
Here is the input file i am using
games.txt
5
SD Mines
21 17
Northern State
10 3
BYU
10 21
Creighton
14 7
Sam Houston State
14 24
When i am using the 2nd option (Print Information of a Specific Game) from the output screen,
it ask me to enter the team name and when i enter the team-name.
For example: SD Mines it gives me an error, but when I enter the team-name with no space like: BYU it works fine for me.
cin >> team_name;
Takes the input only upto space.
You might want to use cin.getline() for taking space separated strings as input.
A small program demonstrating the same :
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Name is : , " << name << "!\n";
return 0;
}
std::cin ignores whitespaces by default.
To include spaces in your input try :
getline(cin, team_name);
This would pick up all the characters in a line until you press enter. This is available in
#include<string>
You need to flush the std::cin buffer after reading the choice:
#include <limits>
//...
cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Refer to this question for detailed explanation.
Also, if you want to read strings with spaces from the standard input, replace this:
cin >> team_name;
with this:
getline(cin, team_name);
as already mentioned in other answers. No need to flush std::cin this time, since you have already read the full line.
Finally, remove extra newlines from your games.txt:
5
SD Mines
21 17
Northern State
...
I'm working on an assignment in my first semester of C++ and I just can't figure out working syntax for it. I need to pass a struct as a parameter to a class function using a pointer. This code I've copied is my best attempt, and it will compile but it crashes when it asks for the first name. When I try variations in the syntax, I get errors about incomplete struct, undefined variables (warrior was or invalid operators. What am I doing wrong?
#include <iostream>
using namespace std;
class StarWars
{
public:
int totalNumber;
struct data_clone
{
int ID, timeCounter;
string name;
};
data_clone *warrior;
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
}
void input(struct data_clone *pointer, int total)
{
for(int i = 1; i <= total; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> pointer[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> pointer[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> pointer[i].timeCounter;
}
}
void lifeSpan(struct data_clone *pointer, int total)
{
for(int i = 1; i <= total; i++)
{
cout << "Warrior number " << pointer[i].name << ": " << endl;
while(pointer[i].timeCounter > 0)
{
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is alive." << endl;
pointer[i].timeCounter--;
}
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
};
int main(void)
{
StarWars clones;
clones.createClone();
clones.input(clones.warrior, clones.totalNumber);
clones.lifeSpan(clones.warrior, clones.totalNumber);
}
You don't initialise the memory warrior points to, so you're working with uninitiailised memory - always a bad thing. There's two things to keep in mind here:
When working with pointers, always initialise the memory behind them first. Prefer using RAII concepts like std::unique_ptr / std::shared_ptr
void DoStuff(Widget* w);
std::unique_ptr<Widget> pw = std::make_unique<Widget>();
DoStuff(w.get());
When working with normal variables, use the dereference / reference operators to take a pointer to the variable.
void DoStuff(Widget* w);
Widget widget;
DoStuff(&w);
struct data_clone
{
int ID, timeCounter;
string name;
};
class StarWars
{
private:
data_clone *warrior;
int totalNumber;
public:
StarWars()
{
}
~StarWars()
{
delete[] warrior; // clean up
}
void createClone();
void input();
void lifeSpan();
};
void StarWars::createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
// construct structure baased on input
warrior = new data_clone[totalNumber];
}
void StarWars::input()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "For warrior number " << i+1 << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> warrior[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> warrior[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> warrior[i].timeCounter;
}
}
void StarWars::lifeSpan()
{
std::cout<<"**********Print data**********\n";
for(int i = 0; i < totalNumber; i++)
{
cout << "Warrior number " << warrior[i].name << ": " << endl;
while(warrior[i].timeCounter > 0)
{
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is alive." << endl;
warrior[i].timeCounter--;
}
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
StarWars clones;
clones.createClone();
clones.input();
clones.lifeSpan();
return 0;
}
You never created your clones, you only read how many there should be.
Allocate space for them:
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
warrior = new data_clone[totalNumber];
}
Your array indexes are also off - an array of size N is indexed from 0 to N - 1.
But the more common way of handling this is to pass the amount to the constructor and let the object handle its own members:
class StarWars
{
public:
StarWars(int clones)
: totalNumber(clones),
warrior(new data_clone[clones])
{
}
void input()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> warrior[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> warrior[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> warrior[i].timeCounter;
}
}
void lifeSpan()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "Warrior number " << i << ": " << endl;
while(warrior[i].timeCounter > 0)
{
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is alive." << endl;
warrior[i].timeCounter--;
}
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
private:
int totalNumber;
struct data_clone
{
int ID, timeCounter;
string name;
};
data_clone *warrior;
};
int main()
{
int number = 0;
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> number;
StarWars clones(number);
clones.input();
clones.lifeSpan();
}
Note that you need to handle the destructor, the copy constructor, and the assignment operator properly as well (left as an exercise).
class StarWars
{
private:
struct data_clone
{
int ID, timeCounter;
string name;
};
public:
StarWars()
{
warrior = new data_clone[4];
}
~StarWars()
{
delete[] warrior;
}
int totalNumber;
data_clone *warrior;
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
}
void input(struct data_clone *pointer, int total)
{
for(int i = 0; i < total; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> pointer[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> pointer[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> pointer[i].timeCounter;
}
}
void lifeSpan(struct data_clone *pointer, int total)
{
for(int i = 0; i < total; i++)
{
cout << "Warrior number " << pointer[i].name << ": " << endl;
while(pointer[i].timeCounter > 0)
{
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is alive." << endl;
pointer[i].timeCounter--;
}
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
};
Note: I just hardcoded it which is not the correct way, it force you to enter only 4 tyeps of dataclone
"warrior = new data_clone[4];"
you will at least get your result
I need help understanding the [Error] id return 1 exit status due to 2 undefined references: getGrades() and getAverage(). I was issued DEV C++ and knocked out the syntax errors with mild frustration but these "linking" errors are still giving me a hard time. This is the most recent update of the code, if anyone could help me understand these linking errors that would be great.
Compiler - Dev C++
Windows 7
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
// Function declarations
string getStudentName();
string getWork();
int getGrades();
double getAverage();
int main()
{
string studentName, work[3];
int grades[3];
double average;
// Get the name of the student
studentName = getStudentName();
// Get the work
work[3] = getWork();
// Get the grades
grades[3] = getGrades();
// Get the average
average = getAverage();
// Dynamic spacing for grades
ostringstream ss;
int gradesLength = ss.str().length();
ss <<setprecision(0) << fixed << showpoint;
ss << grades;
cout << "\n the average for " << studentName << " is: " << average << endl;
cout << "The grades for " << studentName << " are: " << endl;
cout << setw(30) << work[0] << ": " << setw(gradesLength) << grades[0] << endl;
cout << setw(30) << work[1] << ": " << setw(gradesLength) << grades[1] << endl;
cout << setw(30) << work[2] << ": " << setw(gradesLength) << grades[2] << "\n\n\n";
cout << "You have completed the program: \n";
return 0;
}
// Student Name
string getStudentName()
{
string name;
cout << "Enter students full name: ";
getline(cin, name);
return name;
}
// Assignments
string getWork()
{
string work[3];
cout << "\nEnter the name of each assignment \n";
cout << "First assignment: ";
getline (cin, work[0]);
cout << "Second assignment: ";
getline (cin, work[1]);
cout << "Third assignment: ";
getline (cin, work[2]);
return work[3];
}
// Grades
int getGrades(string work[3])
{
int grades[3];
cout << "\nEnter the grade for " << work[0] << ": ";
cin >> grades[0];
cout << "Enter the grade for " << work[1] << ": ";
cin >> grades[1];
cout << "Enter the grade for " << work[2] << ": ";
cin >> grades[2];
return grades[3];
}
// Math
double getAverage(int grades[3])
{
double average;
average = (grades[0] + grades[1] + grades[2]) / 3.0f;
return average;
}