Little problem :(

Hello everyone, im very new to C++ and i've been learning it for like a week now, and i was wondering if anyone on this forum could help me resolve a problem, here is the 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
#include <iostream>
#include <windows.h>
using namespace std;
string g;
float l;

void playnote (string g, float l)
{
     string n = g;
        if (n == "A"||"a"){
         Beep(2750,l*1000);
         cout << "A";
         }
         else if(n == "B"||"b"){
         Beep(3087,l*1000);
         cout << "B";
         }
         else if(n == "C"||"c"){
         Beep(1637,l*1000);
         cout << "C";
         }
         else if(n == "D"||"d"){
         Beep(1835,l*1000);
         cout << "D";
         } 
         else if(n == "E"||"e"){
         Beep(2060,l*1000);
         cout << "E";
         } 
         else if(n == "F"||"f"){
         Beep(2183,l*1000);
         cout << "F";
         } 
         else if(n == "G"||"g"){
         Beep(2450,l*1000);
         cout << "G";
         } 
           }

int main() {
    
    playnote("C", 0.4);
    playnote("C", 0.4);
    playnote("G", 0.4);
    playnote("G", 0.4);
    playnote("A", 0.4);
    playnote("A", 0.4);
    playnote("G", 0.4);
    Sleep(400);
    playnote("F", 0.4);
    playnote("F", 0.4);
    playnote("E", 0.4);
    playnote("E", 0.4);
    playnote("D", 0.4);
    playnote("D", 0.4);
    playnote("C", 0.4);
    Sleep(400);
    playnote("G", 0.4);
    playnote("G", 0.4);
    playnote("F", 0.4);
    playnote("F", 0.4);
    playnote("E", 0.4);
    playnote("D", 0.4);
    playnote("G", 0.4);
    Sleep(400);
    playnote("G", 0.4);
    playnote("F", 0.4);
    playnote("F", 0.4);
    playnote("E", 0.4);
    playnote("D", 0.4);
    playnote("C", 0.4);
    playnote("C", 0.4);
    Sleep(400);
    playnote("G", 0.4);
    playnote("G", 0.4);
    playnote("A", 0.4);
    playnote("A", 0.4);
    playnote("G", 0.4);
    playnote("F", 0.4);
    playnote("F", 0.4);
    Sleep(400);
    playnote("E", 0.4);
    playnote("E", 0.4);
    playnote("D", 0.4);
    playnote("D", 0.4);
    playnote("C", 0.4);
    cout << "thanks for listening <3";
    system("pause >nul");

    }
    

//twinkle twinkle
//CC GG AA G
//FF EE DD C
//GG FF EE D
//GG FF EE D
//CC GG AA G
//FF EE DD C     


Now, this code compiles correctly and all, but there is one problem, instead of playing twinkle twinkle little star like i programmed it to do, it just plays AAAAAAAAAA, ive tried a few methods of trying to fix it, but my knowledge of C++ just isn't good enough yet, i hope to hear from someone soon and thanks in advancced

-Shaun <3
Your logical ORs in the if statements are wrong. You need n=="A" || n=="a", not n=="A"||"a", which may as well be n=="A"||true.
Don't use "string" data types because you have used only single character there will be memory wastage so use "char" data types look the correction that I have done below or you can go to my blog "www.http://codeincodeblock.blogspot.com/2011/10/playing-sound-source-code-in-c.html" .In my blog there are few articles for beginner but helpful and effective.

#include <iostream>
#include <windows.h>
using namespace std;
//twinkle twinkle
//CC GG AA G
//FF EE DD C
//GG FF EE D
//GG FF EE D
//CC GG AA G
//FF EE DD C
void playnote (char g, float l)
{

char n = g;

if (n == 'A'||'a'){
Beep(2750,l*1000);
cout <<n;
}
else if(n == 'B'||'b'){
Beep(3087,l*1000);
cout << n;
}
else if(n == 'C'||'c'){
Beep(1637,l*1000);
cout << n;
}
else if(n == 'D'||'d'){
Beep(1835,l*1000);
cout << n;
}
else if(n == 'E'||'e'){
Beep(2060,l*1000);
cout << n;
}
else if(n == 'F'||'f'){
Beep(2183,l*1000);
cout << n;
}
else if(n == 'G'||'g'){
Beep(2450,l*1000);
cout << n;
}
}

int main() {
int a;
playnote('C', 0.4);
playnote('C', 0.4);
playnote('G', 0.4);
playnote('G', 0.4);
playnote('A', 0.4);
playnote('A', 0.4);
playnote('G', 0.4);
Sleep(400);
cout<<endl;
playnote('F', 0.4);
playnote('F', 0.4);
playnote('E', 0.4);
playnote('E', 0.4);
playnote('D', 0.4);
playnote('D', 0.4);
playnote('C', 0.4);
Sleep(400);
cout<<endl;
playnote('G', 0.4);
playnote('G', 0.4);
playnote('F', 0.4);
playnote('F', 0.4);
playnote('E', 0.4);
playnote('D', 0.4);
playnote('G', 0.4);
Sleep(400);
cout<<endl;
playnote('G', 0.4);
playnote('F', 0.4);
playnote('F', 0.4);
playnote('E', 0.4);
playnote('D', 0.4);
playnote('C', 0.4);
playnote('C', 0.4);
Sleep(400);
cout<<endl;
playnote('G', 0.4);
playnote('G', 0.4);
playnote('A', 0.4);
playnote('A', 0.4);
playnote('G', 0.4);
playnote('F', 0.4);
playnote('F', 0.4);
Sleep(400);
cout<<endl;
playnote('E', 0.4);
playnote('E', 0.4);
playnote('D', 0.4);
playnote('D', 0.4);
playnote('C', 0.4);
cout<<endl;
cout << "thanks for listening <3";
//system("pause >nul");
return 0;

}
@Dinesh stubedi

thanks a lot for the help but there still seems to be a problem, it now displays the letter its playing quite nicely, but the notes still all seem to be the same :/ is it because it just cant handle all the different frequencies like that, or is there still something else wrong, and btw why did you commment me system("pause >nul") out? :3

EDIT: i fixed the problem and the program works fine, thanks a lot! <3
Last edited on
I comment system("pause>nul") because it does not need in IDE like code::blocks.IDE itselfs hold the console windows after compile but if you tries to open exe file then you may need system("pause>nul") because after finishing program console windows will close and can't look result nicely.Anyway according to your convenient you can use system("pause>nul").
I have also notice notes still all seem to be same and I will look solution of this problem later.
Topic archived. No new replies allowed.