Changing struct into class C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For my homework assignment, I was supposed to code a struct that collected information about music albums. I was able to do this easily. The second part of my assignment was to turn my struct into a class, and I'm having trouble getting my code to compile. Here are my two codes.
Struct code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
struct Album
{
string name;
string artist;
vector <string> songs;
Date release;
};
void initializeAlbum(Album& a);
void initializeSongs(vector <string> & songs);
void printAlbum(Album a);
int main()
{
Album my_album;
initializeAlbum(my_album);
printAlbum(my_album);
return 0;
}
void initializeAlbum(Album& a)
{
cout << "Enter album name:" << endl;
getline(cin, a.name);
cout << "Enter artist name:" << endl;
getline(cin, a.artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> a.release.month;
cout << "Enter the release day:" << endl;
cin >> a.release.day;
cout << "Enter the release year:" << endl;
cin >> a.release.year;
initializeSongs(a.songs);
}
void initializeSongs(vector <string> & songs)
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void printAlbum(Album a)
{
cout << "The album name is " << a.name << endl;
cout << "The artist name is " << a.artist << endl;
cout << "The release date is " << a.release.day << "/" << a.release.month << "/" << a.release.year << endl;
cout << "The songs are:";
for (int x = 0; x < a.songs.size(); x++ )
cout << " " << a.songs[x] << endl;
}
Class code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
class Album
{
public:
void printAlbum();
void initializeAlbum();
string name;
string artist;
vector <string> songs;
Date release;
private:
void initializeSongs();
};
int main()
{
Album a;
a.initializeAlbum();
a.printAlbum();
return 0;
}
void Album::initializeAlbum( )
{
cout << "Enter album name:" << endl;
getline(cin, name);
cout << "Enter artist name:" << endl;
getline(cin, artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> release.month;
cout << "Enter the release day:" << endl;
cin >> release.day;
cout << "Enter the release year:" << endl;
cin >> release.year;
initializeSongs(songs);
}
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void Album::printAlbum()
{
cout << "The album name is " << name << endl;
cout << "The artist name is " << artist << endl;
cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl;
cout << "The songs are:";
for (int x = 0; x < songs.size(); x++ )
cout << " " << songs[x] << endl;
}

Maybe this line is your error: initializeSongs(songs); in void Album::initializeAlbum() function?
Follow compiler messages.

This function takes zero arguments:
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
Yet you call
initializeSongs(songs);
You can access your members within your class functions so just call it like:
initializeSongs();
This compiles fine:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
class Album
{
public:
void printAlbum();
void initializeAlbum();
string name;
string artist;
vector <string> songs;
Date release;
private:
void initializeSongs();
};
int main()
{
Album a;
a.initializeAlbum();
a.printAlbum();
return 0;
}
void Album::initializeAlbum( )
{
cout << "Enter album name:" << endl;
getline(cin, name);
cout << "Enter artist name:" << endl;
getline(cin, artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> release.month;
cout << "Enter the release day:" << endl;
cin >> release.day;
cout << "Enter the release year:" << endl;
cin >> release.year;
initializeSongs();
}
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void Album::printAlbum()
{
cout << "The album name is " << name << endl;
cout << "The artist name is " << artist << endl;
cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl;
cout << "The songs are:";
for (int x = 0; x < songs.size(); x++ )
cout << " " << songs[x] << endl;
}

Related

expected primary-expression before 'int'- How can I sort out the problem?

I'm creating a database for Customer in C++. The firt void works well but the second one returns me the following error: expected primary-expression before 'int'
Where is teh mistake? How can I figure out it?
I can't understand why I receive this error. I'm a beginner and this is my firt important project.
Many thanks!!!
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <cstdio>
using namespace std;
class DataCustomer{ //initialize a class
public:
DataCustomer(){
count = 0 ;
}
void AddCustomer();
void DisplayAll(int i);
void DeleteFile();
void EditFile();
void quit();
void search();
void searchName();
void searchSurname();
void searchCell();
void searchEmail();
int Menu();
//defining New Customer's details
struct NewCustomer
{
char Name[20];
char Surname[20];
char Birthday[10];
char PhoneNum [15];
char Email[20];
};
NewCustomer entries [10000];
int count;
};
void DataCustomer::AddCustomer()
{
cout<<"Enter a Name: ";
ofstream a_file ("database.txt", ios::app);
cin >> entries[count].Name;
a_file << entries [count].Name<<"\n";
cin.clear();
cin.sync();
cout << "Enter a Surname: ";
cin >> entries[count].Surname;
a_file << entries[count].Surname << "\n";
cin.clear();
cin.sync();
cout << "Enter Date of Birth: ";
cin >> entries[count].Birthday;
a_file << entries[count].Birthday << "\n";
cin.clear();
cin.sync();
cout << "Enter Phone Number: ";
cin >> entries[count].PhoneNum;
a_file << entries[count].PhoneNum << "\n";
cin.clear();
cin.sync();
cout << "Enter Email: ";
cin >> entries[count].Email;
a_file << entries[count].Email << "\n";
a_file.close();
++count;
}
void DataCustomer::DisplayAll(int i)
{
system ("cls");
cout<<"Entire Customers' database:"<<endl;
ifstream a_file ( "database.txt" );
a_file>> entries[i].Name;
a_file>> entries[i].Surname;
a_file>> entries[i].Birthday;
a_file>> entries[i].PhoneNum;
a_file>> entries[i].Email;
cout << "Name : " << entries[i].Name << endl;
cout << "Surname: " << entries[i].Surname << endl;
cout << "Date of birth : " << entries[i].Birthday << endl;
cout << "Phone number : " << entries[i].PhoneNum << endl;
cout << "Email: " << entries[i].Email << endl;
cout << "Number of Entries : " << count << endl;
for(int i = 0;i < count;++i)
{cout << endl;
DisplayAll(i); }
}
DataCustomer db;
int main ()
{
db.DisplayAll(int i);
return 0;
}

Why won't Xcode (10.1) realize this class? What am I doing wrong

Basically trying to just run this program for extra learning, Xcode won't understand that I have written a class, and wont implement it. Really confused and need some guidance.
When I run the code only the main method is implemented, nothing else works...
#include <iostream>
using namespace std;
class students {
int id;
char name[20];
int s1;
int s2;
int s3;
public:
void getData() {
cout << "Enter the ID " << endl;
cin >> id;
cout << "Enter the name " << endl;
cin >> name;
cout << "Enter the grade in subject 1 " << endl;
cin >> s1;
cout << "Enter the grade in subject 2 " << endl;
cin >> s2;
cout << "Enter the grade in subject 3 " << endl;
cin >> s3;
}
void putData() {
cout << id << " " << name << " " << s1 << " " << s2 << " " << s3 << endl;
}
};
int main () {
students s[20];
int i, n; //i is for the for loop, n for number of students
cout << "Enter the number of students " << endl;
cin >> n;
for (i=0;i>n;i++)
{
s[i].getData();
}
for (i=0;i>n;i++)
{
s[i].putData();
}
return 0;
}

How to use struct members in a struct's member function?

So the purpose of the program is to Create an array of 3 people, allow the user to populate the data in a for loop, ensure that the results are capitalized, and output the results.
These new projects instructions were to
1. Rewrite capitalize() as a method within the structure.
2. Rewrite printPerson() as a method within the structure
The program itself works just fine, it's just not in the format that my professor wanted. He said the point of it is to not use any arguments but again, I don't know what he means. I just started programming a few months ago and even though I'm trying I don't have a strong knowledge of the terminology.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Person {
string firstName;
string middleName;
string lastName;
int age;
string gender;
void capitalize(Person &arg);
void printPerson(Person arg);
};
Pretty sure these are the methods right here, but I'm not sure if the (person &arg) and (person arg) are the arguments itself or if they are parameters. I thought it was the "arg" part but I can't find a way to get the program to run without them as I'm pretty sure I need the & of operator to modify the information.
int main(void) {
Person myPerson;
Person a[3];
const int size = 5;
for (int i = 0; i <= 2; i++) {
cout << "What is First Name #" << i + 1 << "? ";
getline(cin, a[i].firstName);
cout << "What is Middle Name #" << i + 1 << "? ";
getline(cin, a[i].middleName);
cout << "What is Last Name #" << i + 1 << "? ";
getline(cin, a[i].lastName);
cout << "Age #" << i + 1 << "? ";
cin >> a[i].age;
cin.ignore();
cout << "Male or Female #" << i + 1 << "? ";
getline(cin, a[i].gender);
cout << endl;
}
for (int i = 0; i <= 2; i++) {
myPerson.capitalize(a[i]);
cout << "PERSON #" << i + 1 << endl;
cout << "~~~~~~~~~~~~~~~" << endl;
myPerson.printPerson(a[i]);
}
system("pause");
return 0;
}
Along with that, I don't know how to manipulate the functions to work without the "parameters/arguments" (I'm not sure the difference at this point) or without the "arg"
void Person::capitalize(Person &arg) {
transform(arg.firstName.begin(), arg.firstName.end(), arg.firstName.begin(), ::toupper);
transform(arg.middleName.begin(), arg.middleName.end(), arg.middleName.begin(), ::toupper);
transform(arg.lastName.begin(), arg.lastName.end(), arg.lastName.begin(), ::toupper);
}
void Person::printPerson(Person arg) {
cout << "\nFirst Name: " << arg.firstName << endl;
cout << "\nMiddle Name: " << arg.middleName << endl;
cout << "\nLast Name: " << arg.lastName << endl;
cout << "\nAge: " << arg.age << endl;
cout << "\nGender: " << arg.gender << endl;
cout << "\n\n";
}
The capitalize and the printPerson are now members (usually called methods) of the struct Person. This means that they operate on the member variables of an Person instance. Like this, you can just access all the classes members in these methods. See the following code. I also completed it with a constructor and made it slightly more readable.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Person {
public:
Person();
void readFromUserInput();
void capitalize();
void print();
public:
string firstName;
string middleName;
string lastName;
int age;
string gender;
};
Person::Person() :
firstName(""),
middleName(""),
lastName(""),
age(0),
gender("")
{
}
void Person::readFromUserInput()
{
cout << "What is the First Name ? ";
getline(cin, firstName);
cout << "What is Middle Name ? ";
getline(cin, middleName);
cout << "What is Last Name ? ";
getline(cin, lastName);
cout << "Age ? ";
cin >> age;
cin.ignore();
cout << "Male or Female ? ";
getline(cin, gender);
}
void Person::capitalize()
{
transform(firstName.begin(), firstName.end(), firstName.begin(), ::toupper);
transform(middleName.begin(), middleName.end(), middleName.begin(), ::toupper);
transform(lastName.begin(), lastName.end(), lastName.begin(), ::toupper);
}
void Person::print()
{
cout << "\nFirst Name: " << firstName << endl;
cout << "\nMiddle Name: " << middleName << endl;
cout << "\nLast Name: " << lastName << endl;
cout << "\nAge: " << age << endl;
cout << "\nGender: " << gender << endl;
cout << "\n\n";
}
int main(void)
{
const int NUM_PERSONS = 3;
Person a[NUM_PERSONS];
for (int i = 0; i < NUM_PERSONS; i++)
{
cout << "### " << (i + 1) << ". User:" << endl;
a[i].readFromUserInput();
cout << endl;
}
for (int i = 0; i < NUM_PERSONS; i++)
{
a[i].capitalize();
cout << "PERSON #" << i + 1 << endl;
cout << "~~~~~~~~~~~~~~~" << endl;
a[i].print();
}
system("pause");
return 0;
}

How to add a name field for a shop database (OOP/C++)

Ok, so I've made a database type program and created an item number and price field. I've also tried to stick to a primary key by using the index values of the array of both the "item number" field and "price". But was wondering how to add an "Item name" field along with this to make it work. I've tried to think of many different ways of adding a char type array but that doesn't really work.
#include<iostream>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the price of the item: ";
cin >> price[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Price: " << price[i] << endl << endl;
}
}
void main()
{
class shop s;
s.input();
s.output();
}
Create a class or struct to hold the data:
struct Item
{
int id;
float price; // float may lead to rounding errors.
// I would use int and store cents
//string name;
// If you really want to use c-style string
char name[20];
// or
// char *name; // You would need to use new with this
};
Then keep an array of these:
class shop
{
private:
Item items[20];
// As mentioned, better than a static array would be a vector
//std::vector<Item> items;
public:
void input();
void output();
};
What about a vector of strings?
#include<iostream>
#include <string>
#include <vector>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
vector<string> names;
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
names.resize(n);
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the price of the item: ";
cin >> price[i];
cout << "Enter the name of the item: ";
cin >> names[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Price: " << price[i] << endl << endl;
cout << "Name: " << names[i] << endl << endl;
}
}
And it would be good to have a destructor to clean the memory taken by the vector.
I would use std::string
#include <iostream>
#include <string>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
string name[20];
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the name of the item: ";
cin >> name[i];
cout << "Enter the price of the item: ";
cin >> price[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Item Name: " << name[i] << endl;
cout << "Price: " << price[i] << endl << endl;
}
}
int main()
{
class shop s;
s.input();
s.output();
}

How to keep entries of class to sort and display them?c++

I wrote some codes for house manager.
I want to keep user's entries to sort by one of objects and display them.
I know how to sort data but first I need to keep data in class to display before and after the sorting .
What is the easiest way to keep data?
#include "stdafx.h"
#include <iostream>
using namespace std;
class House
{
private :
int NumRooms;
int Area;
int FloorNumber;
public :
void Getdata()
{
cout << "Enter number of rooms in house: ";
cin >> NumRooms;
cout << "Enter area of house: ";
cin >> Area;
cout << "Enter floor number of house: ";
cin >> FloorNumber;
}
void Putdata()
{
cout << "Number of rooms: " << NumRooms << endl;
cout << "Area of house: " << Area << endl;
cout << "Floor number: " << FloorNumber << endl;
}
};
int menu ();
int _tmain(int argc, _TCHAR* argv[])
{
int n;
House s;
switch(menu())
{
case 1 :
cout << "How many houses:";
cin >> n;
if(n > 20)
cout << "Number of houses is more than max.20!!!" << endl;
else
{
for(int i=1; i<=n; i++)
{
cout << "Details of " << i << " house" << endl;
s.Getdata();
}
}
menu();
case 2 :
menu();
case 3 :
s.Putdata();
menu();
default :
cout << "End of program!!!" << endl;
}
system("pause");
return 0;
}
int menu()
{
int c;
cout << "Wlcome to house manager! Choose one of actions" << endl;
cout << "1. Enter data (max.20 houses)" << endl;
cout << "2. Sort houses" << endl;
cout << "3. Display list" << endl;
cout << "4. Exit" << endl;
cout << "Choice: ";
cin >> c;
return c;
}
Easiest way just put them in std::vector<House>
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class House
{
private :
int NumRooms;
int Area;
int FloorNumber;
public :
bool operator<(const House& other)
{
return (this->Area < other.Area);
}
void Getdata()
{
cout << "Enter number of rooms in house: ";
//cin >> NumRooms;
cout << "Enter area of house: ";
//cin >> Area;
cout << "Enter floor number of house: ";
//cin >> FloorNumber;
NumRooms= rand() * 10;
Area = NumRooms * rand();
FloorNumber=rand();
}
void Putdata()
{
cout << "Number of rooms: " << NumRooms << endl;
cout << "Area of house: " << Area << endl;
cout << "Floor number: " << FloorNumber << endl;
}
};
int menu ();
typedef std::vector<House> houseCollection;
int _tmain(int argc, _TCHAR* argv[])
{
int n;
House s;
houseCollection houses;
switch(menu())
{
case 1 :
cout << "How many houses:";
//cin >> n;
n=5;
if(n > 20)
cout << "Number of houses is more than max.20!!!" << endl;
else
{
for(int i=1; i<=n; i++)
{
cout << "Details of " << i << " house" << endl;
s.Getdata();
houses.push_back(s);
}
}
menu();
case 2 :
std::sort(houses.begin(), houses.end());
menu();
case 3 :
for( houseCollection::iterator it = houses.begin(); it != houses.end(); it++)
{
s = (*it);
s.Putdata();
}
menu();
default :
cout << "End of program!!!" << endl;
}
system("pause");
return 0;
}
int menu()
{
int c;
cout << "Wlcome to house manager! Choose one of actions" << endl;
cout << "1. Enter data (max.20 houses)" << endl;
cout << "2. Sort houses" << endl;
cout << "3. Display list" << endl;
cout << "4. Exit" << endl;
cout << "Choice: ";
cin >> c;
return c;
}