Help with functions and loops Updated

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace 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");
      }
Last edited on
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,







Last edited on
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?
Last edited on
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?
 
  return 0;





ok not i am having prob on line 80-82
and dont know if the functions are going to work :/



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace 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");
      }
I think when you put arguments into a function they should be separated by a comma. int Option1(int&, SaveNewCD); for instance... i hope that helps
closed account (Dy7SLyTq)
you need to declare SaveNewCD, OpenCD, and Exit in a scope main can see
what do you mean DTSCode?
closed account (Dy7SLyTq)
its what your trying to pass to option one/two/three
this is what i am getting


http://i39.tinypic.com/5wcx9w.png
Last edited on
You're trying to pass variables named SaveNewCD, OpenCD, and Exit to Option1/2/3., but you've not declared those variables anywhere.
ok so now i named the variables but then the program opens and closes itself right away.
If the menu is not being displayed, you're probably exiting at line 66 because the input file is not found.
yea i just figured that but now on option 2 is not displaying what is on the CD file.dat
and the loop on writing to the file is not working properly
Your while loop at line 43 doesn't do anything.
The ; at the end of the line terminates the statement.

1
2
while (LoraCD.good());
//-------------------^ 


Topic archived. No new replies allowed.