Question

c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

c++, I am having trouble getting my program to compile, any help would be appreciated.

#include <iostream>

#include <string>

#include <string.h>

#include <fstream>

#include <stdlib.h>

using namespace std;

struct record

{

char artist[50];

char title[50];

char year[50];

};

class CD

{

//private members declared

private:

string artist; //asks for string

string title; // asks for string

int yearReleased; //asks for integer

//public members declared

public:

CD();

CD(string,string,int);

void setArtist(string);

void setTitle(string);

void setYearReleased(int);

string getArtist() const;

string getTitle() const;

int getYearReleased() const;

};

CD::CD() : artist(""),title(""), yearReleased(0)

{}

CD::CD(string artist, string title, int yearReleased): artist(artist),title(title),yearReleased(yearReleased)

{}

void CD::setArtist(string artist)

//implicit paramater, qualify the name of the class members and make it visible again.

{

this->artist = artist;

}

void CD::setTitle( string title)

{

this->title = title;

}

void CD::setYearReleased(int yearReleased)

{

this->yearReleased = yearReleased;

}

string CD::getArtist() const

{

return artist;

}

string CD::getTitle() const

{

return title;

}

int CD::getYearReleased() const

{

return yearReleased;

}

void printMenu();

void displayTestCD();

void mathBreak();

void evenOdd();

void primeNumber();

bool isPrime(int);

bool isEven(int);

void readCollection();

void displayCollection();

int main()

{

int choice;

choice =0;

      while(choice != 3)

      {

//The menu and the lets the user input which menu they want to access, given the option between displaying the testCD, math break or quiting. if anything else is picked, invalid choie is displayed.

printMenu();

cin>>choice; //user input

switch(choice) //switch to give the menu different option

{

case 1: // display the testCD

readCollection();

break;

case 2:

displayCollection();

break;

case 3:

displayTestCD();

break;

case 4:

break;

case 5:

break;

case 6:

mathBreak();

break;

case 7:

break;

default: // if anything other than 1-3 is chosen, display invalid choice

cout<<"Invalid choice"<

}

}

  

return 0;

}

//void function for the menu and the cout statements for what will be displayed when the printMenu() function is accessed

// Records array.....

struct record rc[18];

ifstream ptr;

      ptr.open("cd.txt");

     if(!ptr)

{

cout<<"\nFile opening error\n";

exit(1);

}

void printMenu()

{

cout<<"1.Read Collection<<endl;

cout<<"2. Display Collection"<<endl;

cout<<"3. Sort Collection"<<endl;

cout<<"4. Find a CD"<<endl;

cout<<"5. Edit CD"<<endl;

cout<<"6. Math Break"<<endl;

cout<<"7. Quit"<<endl;

}

void displayTestCD()

{

//test CD

CD cd1;

cd1.setArtist("asdfa");

cd1.setTitle("adfasdf");

cd1.setYearReleased(2019);

cout<<" Artist : "<<endl;

cout<<" Title : "<<endl;

cout<<" Year Released : "<<endl;

}

void mathBreak()

{

int subChoice;

cout << endl << "1. Even or Odd" << endl;

cout << "2. Prime Number" << endl;

cout << "3. End my Math Break" << endl;

cout << "Enter your choice: ";

cin >> subChoice;

//anything other than 1,2,3 is invalid and will prompt the user to enter a valid choice

while(subChoice < 1 || subChoice > 3)

{

cout << "Invalid choice." << endl;

cout << "Enter your choice: ";

cin >> subChoice;

}

switch(subChoice)

{

case 1:

//call the evenOdd function

evenOdd();

break;

case 2:

//call the primeNumber function

primeNumber();

break;

}

}

//Was not sure if i had to include the Math Break menu from lab 1, left it out. What I belive we are supposed to do is bring all the labs together at the end of the semester?

void evenOdd()

{

int number;

bool repeat = true;

do

{

//allow the user to input a number 0 or larger

cout << endl << "Enter an integer larger than 0:";

cout << endl <<"Enter 0 to access the previous menu. ";

cin >> number;

//repeat until a valid numebr is given

while(number < 0)

{

//if invalid numbers are input, then reprompt the user to enter a valid number

cout << "Invalid number. Negative numbers are not allowed." << endl;

cout << "Enter an integer larger than 0: ";

cin >> number;

}

//set repeat to false if 0 is entered to return to the sub menu

if(number == 0)

repeat = false;

else

{

//check and print wherther the number is even or not

if(isEven(number))

cout << "The number is even." << endl;

else

cout << "The number is odd." << endl;

}

}while(repeat);

}

bool isEven(int number)

{

//check and return whether the number is even or not

if(number % 2 == 0)

return true;

else

return false;

}

void primeNumber()

{

int number;

bool repeat = true;

do

{

cout << endl <<"Enter an integer larger than 0: ";

cout << endl <<"Enter 0 to access the previous menu. ";

cin >> number;

//if the number is less than 0 then ask for a valid number

while(number < 0)

{

cout << "Invalid number. Negative numbers are not allowed." << endl;

cout << "Enter an integer larger than 0: ";

cin >> number;

}

if(number == 0)

repeat = false;

else

{

//check and print whether the number is prime or not

if(isPrime(number))

cout << "The number is prime." << endl;

else

cout << "The number is not a prime." << endl;

}

}while(repeat);

}

  

bool isPrime(int number)

{

int i;

bool prime = true;

//equation to check if number is prime or not

for(i = 2; i <= number/2; ++i)

{

//if number is not prime

if(number % i == 0)

prime = false;

}

return prime;

}

void readCollection()

{

for(int i=1;i<18;i++)

{

ptr.clear();

ptr.getline(rc[i].artist, 50);

ptr.clear();

ptr.getline(rc[i].title, 50);

ptr.clear();

ptr.getline(rc[i].year, 50);

}

}

void displayCollection()

{

cout<<"Artist.........Title......Year!\n\n";

for(int i=1;i<18;i++)

{

cout<

}

  

ptr.close();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>


#include <string.h>

#include <fstream>

#include <stdlib.h>

using namespace std;
ifstream ptr;

struct record

{

char artist[50];

char title[50];

char year[50];

};

class CD

{

//private members declared

private:

string artist; //asks for string

string title; // asks for string

int yearReleased; //asks for integer

//public members declared

public:

CD();

CD(string,string,int);

void setArtist(string);

void setTitle(string);

void setYearReleased(int);

string getArtist() const;

string getTitle() const;

int getYearReleased() const;

};

CD::CD() : artist(""),title(""), yearReleased(0)

{}

CD::CD(string artist, string title, int yearReleased): artist(artist),title(title),yearReleased(yearReleased)

{}

void CD::setArtist(string artist)

//implicit paramater, qualify the name of the class members and make it visible again.

{

this->artist = artist;

}

void CD::setTitle( string title)

{

this->title = title;

}

void CD::setYearReleased(int yearReleased)

{

this->yearReleased = yearReleased;

}

string CD::getArtist() const

{

return artist;

}

string CD::getTitle() const

{

return title;

}

int CD::getYearReleased() const

{

return yearReleased;

}

void printMenu();

void displayTestCD();

void mathBreak();

void evenOdd();

void primeNumber();

bool isPrime(int);

bool isEven(int);

void readCollection();

void displayCollection();

int main()

{

int choice;

ptr.open("cd.txt");

if(!ptr)

{

cout<<"\nFile opening error\n";

exit(1);

}
choice =0;

while(choice != 3)

{

//The menu and the lets the user input which menu they want to access, given the option between displaying the testCD, math break or quiting. if anything else is picked, invalid choie is displayed.

printMenu();

cin>>choice; //user input

switch(choice) //switch to give the menu different option

{

case 1: // display the testCD

readCollection();

break;

case 2:

displayCollection();

break;

case 3:

displayTestCD();

break;

case 4:

break;

case 5:

break;

case 6:

mathBreak();

break;

case 7:

break;

default: // if anything other than 1-3 is chosen, display invalid choice

cout<<"Invalid choice";

}

}

  

return 0;

}

//void function for the menu and the cout statements for what will be displayed when the printMenu() function is accessed

// Records array.....

struct record rc[18];

void printMenu()

{

cout<<"1.Read Collection"<<endl;

cout<<"2. Display Collection"<<endl;

cout<<"3. Sort Collection"<<endl;

cout<<"4. Find a CD"<<endl;

cout<<"5. Edit CD"<<endl;

cout<<"6. Math Break"<<endl;

cout<<"7. Quit"<<endl;

}

void displayTestCD()

{

//test CD

CD cd1;

cd1.setArtist("asdfa");

cd1.setTitle("adfasdf");

cd1.setYearReleased(2019);

cout<<" Artist : "<<endl;

cout<<" Title : "<<endl;

cout<<" Year Released : "<<endl;

}

void mathBreak()

{

int subChoice;

cout << endl << "1. Even or Odd" << endl;

cout << "2. Prime Number" << endl;

cout << "3. End my Math Break" << endl;

cout << "Enter your choice: ";

cin >> subChoice;

//anything other than 1,2,3 is invalid and will prompt the user to enter a valid choice

while(subChoice < 1 || subChoice > 3)

{

cout << "Invalid choice." << endl;

cout << "Enter your choice: ";

cin >> subChoice;

}

switch(subChoice)

{

case 1:

//call the evenOdd function

evenOdd();

break;

case 2:

//call the primeNumber function

primeNumber();

break;

}

}

//Was not sure if i had to include the Math Break menu from lab 1, left it out. What I belive we are supposed to do is bring all the labs together at the end of the semester?

void evenOdd()

{

int number;

bool repeat = true;

do

{

//allow the user to input a number 0 or larger

cout << endl << "Enter an integer larger than 0:";

cout << endl <<"Enter 0 to access the previous menu. ";

cin >> number;

//repeat until a valid numebr is given

while(number < 0)

{

//if invalid numbers are input, then reprompt the user to enter a valid number

cout << "Invalid number. Negative numbers are not allowed." << endl;

cout << "Enter an integer larger than 0: ";

cin >> number;

}

//set repeat to false if 0 is entered to return to the sub menu

if(number == 0)

repeat = false;

else

{

//check and print wherther the number is even or not

if(isEven(number))

cout << "The number is even." << endl;

else

cout << "The number is odd." << endl;

}

}while(repeat);

}

bool isEven(int number)

{

//check and return whether the number is even or not

if(number % 2 == 0)

return true;

else

return false;

}

void primeNumber()

{

int number;

bool repeat = true;

do

{

cout << endl <<"Enter an integer larger than 0: ";

cout << endl <<"Enter 0 to access the previous menu. ";

cin >> number;

//if the number is less than 0 then ask for a valid number

while(number < 0)

{

cout << "Invalid number. Negative numbers are not allowed." << endl;

cout << "Enter an integer larger than 0: ";

cin >> number;

}

if(number == 0)

repeat = false;

else

{

//check and print whether the number is prime or not

if(isPrime(number))

cout << "The number is prime." << endl;

else

cout << "The number is not a prime." << endl;

}

}while(repeat);

}

  

bool isPrime(int number)

{

int i;

bool prime = true;

//equation to check if number is prime or not

for(i = 2; i <= number/2; ++i)

{

//if number is not prime

if(number % i == 0)

prime = false;

}

return prime;

}

void readCollection()

{

for(int i=1;i<18;i++)

{

ptr.clear();

ptr.getline(rc[i].artist, 50);

ptr.clear();

ptr.getline(rc[i].title, 50);

ptr.clear();

ptr.getline(rc[i].year, 50);

}

}

void displayCollection()

{

cout<<"Artist.........Title......Year!\n\n";

for(int i=1;i<18;i++)

{

cout<<"";

}

  

ptr.close();

}

The compile time errors are removed the code compiles successfully now test with your files.

Add a comment
Know the answer?
Add Answer to:
c++, I am having trouble getting my program to compile, any help would be appreciated. #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

  • I am working on this switch program everything seems to be ok but the compiler is...

    I am working on this switch program everything seems to be ok but the compiler is giving me an error on the final closing brace and wanted to know where I am making an error #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int choice; char repeat; double MidTerm = 0; double FinalExam = 0; double Quiz1 = 0; double Quiz2 = 0; double MidTermGrade = 1, FinalExamGrade = 2, Quiz1Grade = 3, Quiz2Grade = 4,...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT