I need some help on my C++ program,thx
how do I add this part into my program? thx!<>a) Add a void function called Contemporary that prints the name of the artists that created work between 1995 and 2007 (non inclusive). Place your function call within the body of the loop.
b) Add an enumerated type called Style that lists the types of music - jazz, pop, rap
c) Change the structure so that it uses this enumerated type. Now add a function with a switch statement that will assign the correct enumerated type based on the input value
here we got so far..
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
enum Style {jazz,pop,rap};
//put enumerated type description and function prototypes here
int main()
{
ifstream dataIn;
ofstream dataOut;
struct Musictype
{
string style;
string name;
string albumName;
int year;
int profit;
};
//Declare a datatype structure below called Music that will hold the data elements of the input file:
//style name albumName year profit
//Declare a variable of your structured type below
dataIn.open("musicians.dat");
if (!dataIn){
cout << "error opening input file";
return(1);
}
//priming read goes here
string style;
string name;
string albumName;
int year;
int profit;
int total=0;
while (dataIn){
dataIn>>style>>name>>albumName>>year>>profit;
total=total+profit;
dataIn.get();
//Enter code to read the data into your struct variable from the input file
//after you read the data in, print it to the screen
//Write a code segment that sums the profits of all the musicians and prints it to the screen
//remember to use formatting for the dollar amount
}
cout<<"The total profits for all musicians: $"<<total<<endl;
//print out the total profit here, please use formatting
return 0;
}//end program
[2161 byte] By [
leoyous] at [2007-11-11 10:11:12]

# 1 Re: I need some help on my C++ program,thx
an enum is of the syntax
enum Colors
{
red,
blue,
green
};
and a switch statement looks like this
switch (int_variable_type)
{
case one: do something
case two: case one will do this stuff as well
break; //exit the cases so we do not proceed into case 3
case three: do something else
break; // as before. Switch statements are aggravating because of this feature...
default: in case the variable is out of bounds handled above...
}
}
and of course a void function is just
void fubar(int x)
{
x = 3;
}
etc...
hopefully these hints will get you started.
jonnin at 2007-11-11 20:59:24 >

# 2 Re: I need some help on my C++ program,thx
Note that enums are a nice way to organize integer constants, switches are a poor way to handle a series of nested "if" statements, and void functions are just a standard subroutine except they use the keyword void which is an odd concept, it just means the function cannot be used like this int x = fubar(); //error, fubar does not return a value. If it did, it would have been int fubar instead of void funar, see?
jonnin at 2007-11-11 21:00:25 >

# 3 Re: I need some help on my C++ program,thx
here is what I have so far , and I need some help on //Enter code to read the data into your struct variable from the input file
//after you read the data in, print it to the screen
how do I create it.
thx...
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
enum Style {jazz,pop,rap};
//put enumerated type description and function prototypes here
void contemporary(string,string,string,int,int);
int main()
{
ifstream dataIn;
ofstream dataOut;
struct Music
{
string style;
string name;
string albumName;
int year;
int profit;
};
Music musictypes;
//Declare a datatype structure below called Music that will hold the data elements of the input file:
//style name albumName year profit
//Declare a variable of your structured type below
dataIn.open("musicians.dat");
if (!dataIn){
cout << "error opening input file";
return(1);
}
//priming read goes here
string style;
string name;
string albumName;
int year;
int profit;
int total=0;
cout<<"The musicians who created albums between 1995 and 2007:"<<endl<<endl;
while (dataIn){
dataIn>>style>>name>>albumName>>year>>profit;
contemporary(style,name,albumName,year,profit);
total=total+profit;
dataIn.get();
//Enter code to read the data into your struct variable from the input file
//after you read the data in, print it to the screen
//Write a code segment that sums the profits of all the musicians and prints it to the screen
//remember to use formatting for the dollar amount
}
cout<<"The total profits for all musicians: $"<<total<<endl;
//print out the total profit here, please use formatting
return 0;
}//end program
void contemporary(string style,string name, string albumName,int year,int profit)
{
if (year>1995 && 2007>year)
cout<<name<<endl;
}
# 4 Re: I need some help on my C++ program,thx
struct sample
{
int x;
double d;
};
ifstream ifs; //input file
ifs.open("filedata.txt");
sample sam;
ifs >> sam.x; //read from the file into struct
ifs >> sam.d;
...
cout << sam.x <<" "<< sam.d << endl; //write to the console.
you probably have an array of structs that you will loop over to read from the file -- what I gave you here is just for one and its just an example of the syntax youll need.
jonnin at 2007-11-11 21:02:19 >

# 5 Re: I need some help on my C++ program,thx
here is what i got so far...and i still having trouble with this part c) Change the structure so that it uses this enumerated type. Now add a function with a switch statement that will assign the correct enumerated type based on the input value :WAVE:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
enum Style {jazz,pop,rap};
//put enumerated type description and function prototypes here
void contemporary(string,string,string,int,int);
int main()
{
ifstream dataIn;
ofstream dataOut;
struct Music
{
string style;
string name;
string albumName;
int year;
int profit;
};
Music musictypes;
//Declare a datatype structure below called Music that will hold the data elements of the input file:
//style name albumName year profit
//Declare a variable of your structured type below
dataIn.open("musicians.dat");
if (!dataIn){
cout << "error opening input file";
return(1);
}
//priming read goes here
int total=0;
cout<<"The musicians who created albums between 1995 and 2007:"<<endl<<endl;
while (dataIn){
dataIn>>musictypes.style>>musictypes.name>>musictypes.albumName>>musictypes.year>>musictypes.profit;
contemporary(musictypes.style,musictypes.name,musictypes.albumName,musictypes.year,musicty pes.profit);
total=total+musictypes.profit;
dataIn.get();
//Enter code to read the data into your struct variable from the input file
//after you read the data in, print it to the screen
//Write a code segment that sums the profits of all the musicians and prints it to the screen
//remember to use formatting for the dollar amount
}
cout<<"The total profits for all musicians: $"<<total<<endl;
//print out the total profit here, please use formatting
return 0;
}//end program
void contemporary(string style,string name,string albumName,int year,int profit)
{
if (year>1995 && 2007>year)
cout<<name<<endl;
}
