Class Will Not Run

For an assignment, I'm supposed to create a class called piano and then use the methods:

1-) LoadNotes. This function takes a string as a parameter, the parameter is a file name that contains notes. The notes are already shown in the code you're given. Load Notes should read the notes into an array of characters (or strings)

2-) PlayNotes. This function simply plays the notes that were loaded by LoadNotes.

However, whenever I try to run the code in visual studio it stops working at line 18. Are there any hints you could give me or any easy fixes to make my code run? Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

class Piano 
{
//Member variables
private:
string notes;
//Member functions
 public:
//default constructor
Piano() {
notes = "";
}
void LoadNotes(string mynotes.txt)
{
char note;
ifstream input_file(mynotes.txt);
return;
Last edited on
please use code tags for code. soon people will stop answering if you do not.

a class creates a new type.
where is main()?

you should have a main:
1
2
3
4
5
6
7
int main()
{
  //and it should contain your variable:
   Piano p;
   //and you can use that:
   p.LoadNotes("file");
}

Which line is 18? If you would use code tags in the post, then it would be easy to see. Read https://www.cplusplus.com/articles/jEywvCM9/

Your code in tags and with some indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

class Piano {
    //Member variables
private:
    string notes;
    //Member functions
public:
    //default constructor
    Piano() {
        notes = "";
    }
    //Load notes from file
    void LoadNotes(string mynotes.txt)
    {
        char note;
        ifstream input_file(mynotes.txt);
        return;
    }


Is the line 18 this: void LoadNotes(string mynotes.txt)?

What exactly does the visual studio give as error message?

Note:
The general rules for naming variables are:
* Names can contain letters, digits and underscores
* Names must begin with a letter or an underscore (_)
* Names are case sensitive (myVar and myvar are different variables)
* Names cannot contain whitespaces or special characters like !, #, %, etc.
* Reserved words (like C++ keywords, such as int) cannot be used as names

Your name for a variable, the mynotes.txt, violates one of them. Dot is not a letter, digit, nor underscore.


See http://www.cplusplus.com/doc/tutorial/files/ too.
Last edited on
Variable names cannot have '.'s in them. String literals should have "" around them.

1
2
3
4
5
6
7
8
9
10
11
12
13
void LoadNotes(string filename)
{
    ifstream input_file(filename);
    // ...
}

// ...

int main()
{
   Piano p; // borrowing from jonnin's post
   p.LoadNotes("mynotes.txt");
}


The return statement at the end of a void function is not necessary.
Last edited on
note that your ifstream is a local and is opened, then destroyed.
you need to read it into a container or something.
I took into account what y'all had to say and made some changes, but I still can't get it to run. Whenever I try, I get the following error messages:

Error (active) E0065 expected a ';' ConsoleApplication2 16

Error (active) E0415 no suitable constructor exists to convert from "const char [1]" to "Piano" ConsoleApplication2 17

Error (active) E0169 expected a declaration ConsoleApplication2 18

Error (active) E0070 incomplete type is not allowed ConsoleApplication2 23

Error (active) E0020 identifier "filename" is undefined ConsoleApplication2 23

Error (active) E0135 class "Piano" has no member "LoadNotes" ConsoleApplication2 28

Error C2143 syntax error: missing ';' before '}' ConsoleApplication2 16

Error C2440 'initializing': cannot convert from 'const char [1]' to 'Piano' ConsoleApplication2 17

Error C2447 '{': missing function header (old-style formal list?) ConsoleApplication2 18

Error C2079 'input_file' uses undefined class ConsoleApplication2 23

Error C2065 'filename': undeclared identifier ConsoleApplication2 23

Error C2039 'LoadNotes': is not a member of 'Piano' ConsoleApplication2 28

Error C2731 'main': function cannot be overloaded ConsoleApplication2 32

Error C4996 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details. ConsoleApplication2 36


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
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

class Piano 
{
    //Member variables
private:
    string notes;
    //Member functions
public:
    //default constructor
    Piano();
}
        notes = "";
        {
            //Load notes from file
            void LoadNotes(string filename)
        }
        char note;
        ifstream input_file(filename);

int main()
{
    Piano p;
   p.LoadNotes("filenmae");
}
Last edited on
Remove <conio.h> and <windows.h>, you do not need them.

Add #include <string>
Add #include <fstream>

Something got severely messed up with your declaration of LoadNotes.

Let's just start with a simple, minimal example:
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
// Example program
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Piano 
{
private:
    string notes;

public:
    void LoadNotes(string filename)
    {
        //Load notes from file
        ifstream input_file(filename);
        
        if (!input_file)
        {
            cout << "Error reading file\n";
            return;
        }

        //
        // ... read from input_file here ...
        //
    }
};

int main()
{
    Piano p;
    p.LoadNotes("filename.txt");
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

class Piano 
{
    //Member variables
private:
    string notes;
    //Member functions
public:
    //default constructor
    Piano();
} // E0065 expected a ';'
// The definition of class must end with ;
// This closing brace matches the opening brace on line 8 

The rest of errors are just due to the compiler having already fallen off the track.

Except ...
Error C2731 'main': function cannot be overloaded ConsoleApplication2 32

Error C4996 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details. ConsoleApplication2 36

You show only 29 lines of code and these errors come from lines 32 and 36?

Do you now have int main() twice? Why don't you show the one that you have, rather than blindly copy-pasting the second implementation of main()?
keskiverto
My code up to this point is:

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
class Piano
{
private:
    string notes;

public:
    void LoadNotes(string filename)
    {
        //Load notes from file
        ifstream input_file(filename);

        if (!input_file)
        {
            cout << "Error reading file\n";
            return;
        }

        //
        // ... read from input_file here ...
        //
    }
};

int main()
{
    Piano p;
    p.LoadNotes("filename");
}

My professor gave us code to test it with (the code is down below:)

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
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

int main(int argc, char *argv[])
{
  while(true){
              cout<<"Input note: ";
              char note = getch();
              
              
              //do re mi fa sol la si do re mi fa sol
              if(note == 'a'){
                      Beep(261,100);
                      }
              if(note == 's'){
                      Beep(293,100);
                      }
              if(note == 'd'){
                      Beep(329,100);
                      }
              if(note == 'f'){
                      Beep(349,100);
                      }
              if(note == 'g'){
                      Beep(392,100);
                      }
              if(note == 'h'){
                      Beep(440,100);
                      }
              if(note == 'j'){
                      Beep(493,100);
                      }                      
              if(note == 'k'){
                      Beep(523,100);
                      }                      
              if(note == 'l'){
                      Beep(587,100);
                      }
              if(note == ';'){
                      Beep(659,100);
                      }  
              if(note == '\''){
                      Beep(698,100);
                      } 
               if(note == '\\'){
                      Beep(784,100);
                      } 
                      
               //rebemol mibemol solbemol labemol sibemol rebemol mibemol solbemol
               if(note == 'w'){
                      Beep(277,100);
                      } 
                      if(note == 'e'){
                      Beep(311,100);
                      }
                      if(note == 't'){
                      Beep(370,100);
                      }
                      if(note == 'y'){
                      Beep(415,100);
                      }
                      if(note == 'u'){
                      Beep(466,100);
                      }
                      if(note == 'o'){
                      Beep(554,100);
                      }
                      if(note == 'p'){
                      Beep(622,100);
                      }
                      if(note == ']'){
                      Beep(740,100);
                      }     
                      
            system("cls");          
}  
    
    return EXIT_SUCCESS;
} 
Last edited on
The reason I had originally said to not use #include <conio.h> #include <windows.h> were that they were non-standard headers, and you weren't using their functionality in the code you posted. Your latest post supersedes this.

First, you should ask your professor exactly what compiler he or she is using. Ideally, you should compile with whatever compiler/toolchain your professor will use to grade you with. Because getch() is non-standard, different compilers range in strictness regarding it.

Error C4996 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch

Your compiler wants you to use _getch() instead of getch(). Ask your professor if this is OK.

______________________

If you combine your Piano class with your professor's driver code, these are the headers you need:
1
2
3
4
5
6
#include <cstdlib>   // EXIT_SUCCESS, system
#include <iostream>  // std::cout
#include <string>    // std::string
#include <fstream    // std::ifstream
#include <conio.h>   // getch or _getch
#include <windows.h> // Beep 
Last edited on
My professor is using windows visual studio, although I am not sure what version. Other than the getch error, are you saying that my could should run?
Last edited on
Other than the getch error, are you saying that my could should run?
Yes. Assuming you fix syntax issues in your second post, don't have two different mains, have proper includes, etc.
I was able to fix my code and wanted to thank everyone who helped.

Here is the running code:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include<fstream>
using namespace std;
//Create a class Piano
class Piano {
	//Member variables
private:
	string notes;
	//Member functions
public:
	//default constructor
	Piano() {
		notes = "";
	}
	//Load notes from file
	void LoadNotes(string filename) {
		char note;
		ifstream input_file(filename);
		if (!input_file.is_open()) {
			cerr << "Could not open the file - '"
				<< filename << "'" << endl;
			return;
		}
		while (input_file.get(note)) {
			notes += note;
		}
		input_file.close();
	}
	//Play notes
	void PlayNotes() {
		for (const auto& note : notes) {
			if (note == 'a') {
				Beep(261, 100);
			}
			if (note == 's') {
				Beep(293, 100);
			}
			if (note == 'd') {
				Beep(329, 100);
			}
			if (note == 'f') {
				Beep(349, 100);
			}
			if (note == 'g') {
				Beep(392, 100);
			}
			if (note == 'h') {
				Beep(440, 100);
			}
			if (note == 'j') {
				Beep(493, 100);
			}
			if (note == 'k') {
				Beep(523, 100);
			}
			if (note == 'l') {
				Beep(587, 100);
			}
			if (note == ';') {
				Beep(659, 100);
			}
			if (note == '\'') {
				Beep(698, 100);
			}
			if (note == '\\') {
				Beep(784, 100);
			}
			//rebemol mibemol solbemol labemol sibemol rebemol mibemol solbemol
			if (note == 'w') {
				Beep(277, 100);
			}
			if (note == 'e') {
				Beep(311, 100);
			}
			if (note == 't') {
				Beep(370, 100);
			}
			if (note == 'y') {
				Beep(415, 100);
			}
			if (note == 'u') {
				Beep(466, 100);
			}
			if (note == 'o') {
				Beep(554, 100);
			}
			if (note == 'p') {
				Beep(622, 100);
			}
			if (note == ']') {
				Beep(740, 100);
			}
		}
	}
};
int main(int argc, char* argv[])
{
	//Create object
	Piano mypiano;
	//Load notes from file
	mypiano.LoadNotes("mynotes.txt");
	//Play
	mypiano.PlayNotes();
	return EXIT_SUCCESS;
}
As PlayNotes() is really the professor's test code, then I'm not enthused with his code. There's much better ways of doing this than a long list of if statements...
... furthermore, that code does not "test" class Piano. It is closer to sample implementation of the "PlayNotes".

Surely a proper "test driver" can be used as is, without any modification?
Topic archived. No new replies allowed.