i need help with this program ive been asked to open a file the the user has to input one of three optns, opt1 will append to the file opt2 will display or print all info inside the created file and opt3 will exit the program.
the problem is i am switching to use the functions as part of the menu but i am unable to call the functions or print what the functions are supposed to do
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
usingnamespace std;
string ArtName; //Artist Name
string CDTitle;
float Price;
int LCV; //Loop Control
int Menu;
int Option1(int SaveNewCD)
{ ofstream NameCD("NameCD.dat",ios::app);
for (LCV=0;LCV<3;LCV++)
{
cout<<"\nEnter Name of Artist: ";
getline(cin,ArtName);
cin.get();
cout<<"\nEnter CD Title: ";
getline(cin,CDTitle);
cin.get();
cout<<"\nEnter Price: ";
cin>>Price;
cin.get();
NameCD<<ArtName<<" "<<CDTitle<<" "<<Price<<endl;
}
NameCD.close();}
int Option2(int OpenCD)
{ ifstream NameCD("NameCD.dat");
NameCD>>Price;
getline(NameCD,ArtName);
getline(NameCD,CDTitle);
while (NameCD.good());
{
cout<<ArtName<<"\t"<<CDTitle<<"\t"<<Price<<endl;}
NameCD.close();
}
int Option3(int Exit)
{ cout<<"\nProgram Will Exit."<<endl;}
main()
{
string ArtName; //Artist Name
string CDTitle;
float Price;
int LCV; //Loop Control
int Menu;
ifstream NameCD("NameCD.dat");
if (NameCD.fail()){
cout<<"\nFile does not exist."<<endl;
exit(1);
}
else { cout<<"\nFile Exists."<<endl; }
NameCD>>Price;
getline(NameCD,ArtName);
getline(NameCD,CDTitle);
cout<<"\nEnter 1 to add more artist and album";
cout<<"\nEnter 2 to see artists and albums";
cout<<"\nEnter 3 to exit."<<endl;
cin>>Menu;
switch(Menu)
{ case 1: int Option1(int SaveNewCD);break;
case 2: int Option2(int OpenCD);break;
case 3: int Option3(int Exit);break;
default: cout<<"\nYou did not entered a valid option";break;
}
system("pause");
}
What exactly is your question? Have you tried to compile this and work though the compiler error messages?
Posted before you edited your code.
What is the purpose of lines 61-63?
Why arg you passing arguments on lines 71-73?
Don't precede function names in a function call with int.
Don't put int before an argument in a function call.
Lines 31-37, don't you want to loop through the entire file (until you hit eof)?
Line 15, why are you looping exactly 3 times? Don't you want to loop until the user has no more albums to enter?
Lines 7-11, avoid the use of globals.
edit:
Line 38 doesn't match the name of the ifstream object on line 31.
Line 61 doesn't match the the ifstream object on line 52.
Line 44, main must have type int.
Lines 13,30,31 - functions must return a value,
i need help understanding the functions and how do i make this one to run properly
What is the purpose of lines 61-63? read from file.
Why arg you passing arguments on lines 71-73? i dont know
Don't precede function names in a function call with int. what?
Don't put int before an argument in a function call. i dont get this.
Lines 31-37, don't you want to loop through the entire file (until you hit eof)?
EOF? how should i loop?
Line 15, why are you looping exactly 3 times? Don't you want to loop until the user has no more albums to enter? yes but dont know how
Lines 7-11, avoid the use of globals. where should i place it.
edit:
Line 38 doesn't match the name of the ifstream object on line 31. i edited some variables.
Line 61 doesn't match the the ifstream object on line 52.
Line 44, main must have type int. solved
Lines 13,30,31 - functions must return a value, how do i return?
What is the purpose of lines 61-63? read from file.
Yes, I realize they read from the file.
Why are you reading from the file here?
You're reading only the first set of values, but don't do anything with them.
What if the file is empty?
Why are you passing arguments on lines 71-73? i dont know
If you don't know why you're doing it, then you probably shouldn't be doing it.
None of your option functions do anything with the value you pass, so why pass it?
Lines 71-73: Don't precede function names in a function call with int. what?
Don't put int before an argument in a function call. i dont get this.
case 1: int Option1(int SaveNewCD);break;
This is wrong. It should be:
case 1: Option1(SaveNewCD);break;
Lines 31-37, don't you want to loop through the entire file (until you hit eof)?
EOF? how should i loop?
Use a while loop.
Line 15, why are you looping exactly 3 times? Don't you want to loop until the user has no more albums to enter? yes but dont know how
Ask the user. Loop until the user says no.
Lines 7-11, avoid the use of globals. where should i place it.
Place variables as locally as possible. Pass as arguments if you need them in another function.
Lines 13,30,31 - functions must return a value, how do i return?
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
usingnamespace std;
int Option1(int& SaveNewCD)
{ string ArtName; //Artist Name
string CDTitle;
float Price;
int LCV; //Loop Control
ofstream LoraCD("LoraCD.dat",ios::app);
for (LCV=0;LCV<3;LCV++)
{
cout<<"\nEnter Name of Artist: ";
getline(cin,ArtName);
cin.get();
cout<<"\nEnter CD Title: ";
getline(cin,CDTitle);
cin.get();
cout<<"\nEnter Price: ";
cin>>Price;
cin.get();
LoraCD<<ArtName<<" "<<CDTitle<<" "<<Price<<endl;
}
return(0);
LoraCD.close();}
int Option2(int& OpenCD)
{ string ArtName; //Artist Name
string CDTitle;
float Price;
int LCV; //Loop Control
ifstream LoraCD("LoraCD.dat");
LoraCD>>Price;
getline(LoraCD,ArtName);
getline(LoraCD,CDTitle);
while (LoraCD.good());
{
cout<<ArtName<<"\t"<<CDTitle<<"\t"<<Price<<endl;}
LoraCD.close();
return(0);}
int Option3(int& Exit)
{ cout<<"\nProgram Will Exit."<<endl;
exit(1);}
int main()
{
string ArtName; //Artist Name
string CDTitle;
float Price;
int LCV; //Loop Control
int Menu;
ifstream LoraCD("LoraCD.dat");
if (LoraCD.fail()){
cout<<"\nFile does not exist."<<endl;
exit(1);
}
else { cout<<"\nFile Exists."<<endl; }
LoraCD>>Price;
getline(LoraCD,ArtName);
getline(LoraCD,CDTitle);
cout<<"\nEnter 1 to add more artist and album";
cout<<"\nEnter 2 to see artists and albums";
cout<<"\nEnter 3 to exit."<<endl;
cin>>Menu;
switch(Menu)
{ case 1: Option1 (SaveNewCD);break;
case 2: Option2 (OpenCD);break;
case 3: Option3 (Exit);break;
default: cout<<"\nYou did not entered a valid option";break;
}
system("pause");
}