Hello everyone,

I have been visiting this site often as a guest, and I've noticed that it is a very nice community. Now, I have a question myself, and I hope I will be able to get some answers =D

Anyways, I started learning C++ about... 2-3 weeks ago. Not visual C++, although I am going to start that soon. Anyways, there is this school project (not a tech project, a physics project). I wanted to write a program, so it's easier for me to present and everything. I am stuck because I want to print some numbers to notepad, but printf is too limited for this and I don't think it can be used... (I may very well be wrong, so prove me wrong here if you can =D)

Here is my code so far:
(using Devc++)

#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>

using namespace std;

#define PI=3.14159265358979323846264338327950

int main ()
{
   double n;
   string string;
   double t;
   int time;                   // in ms
   char note;
   int freq;
   double sinwave;
   double envelope1;
   double envelope2;
   double envelope;
   double result;
   int amp=20500;
   int b=65;
   int c=1.5;
   int k=1.85;
        
   cout << "Please enter the note you would like to have played. 1 character only.\n"
        << "Or, select from the list below:\n\n"
        << "U) C sharp;\n"
        << "V) E flat;\n"
        << "W) G flat;\n"
        << "Y) A flat;\n"
        << "Z) B flat;\n\n"
        << "X) Exit;\n\n";
   
   cin >> note;
   
   if (note == 'a' || note == 'A'){
        freq = 440;}
   else if (note == 'b' || note == 'B'){
        freq = 494;}   
   else if (note == 'c' || note == 'C'){
        freq = 262;}   
   else if (note == 'd' || note == 'D'){
        freq = 294;}
   else if (note == 'e' || note == 'E'){
        freq = 330;}        
   else if (note == 'f' || note == 'F'){
        freq = 349;}        
   else if (note == 'g' || note == 'G'){
        freq = 392;}        
   else if (note == 'u' || note == 'U'){
        freq = 277;}           
   else if (note == 'v' || note == 'V'){
        freq = 311;} 
   else if (note == 'w' || note == 'W'){
        freq = 370;}   
   else if (note == 'y' || note == 'Y'){
        freq = 415;}
   else if (note == 'z' || note == 'Z'){
        freq = 466;}
   else if (note == 'x' || note == 'X'){
        goto Exit;}
   
        

   cout << "/n/nPlease enter the amount of time the note lasts.\n"
        << "Please enter it in milliseconds (ms).\n";  
   
   getline (cin, string);
   stringstream(string) >> time;
   
   for (t=0, t<(44.1*time), t+(1/44100){
       
       
       
      
   sinwave = amp*SIN(2*PI*freq*t);
   envelope1 = b*t;
   envelope2 = c*EXP(-k*t);
   
        if (envelope1<envelope2){
            envelope = envelope1)}
        else{
            envelope = envelope2)}
  
   result = sinwave*envelope;
   
   
   
      


Exit:
   return 0;
}

don't worry if you don't understand any of the physics. hopefully those will work out, but I can't really test them right now.

anyways, after all of this I end up with this double number "Result". I want to be able to print this number Result, all by itself, into a notepad, start a new line in notepad, and have the for loop all over again.

How is this possible? (and if you want you can clean up some other parts of my code too :O)

Thank you guys very much!

you can open a text file and write into it. that shd work i guess.

but how do you do that using c++, writing directly into that text file? Otherwise, 1 second of that program prints 44100 numbers, and there is no way to copy that manually.... It would be stupid to.

Well as far as cleaning up the code you can change this:

cin >> note;
   
   if (note == 'a' || note == 'A'){
        freq = 440;}
   else if (note == 'b' || note == 'B'){
        freq = 494;}   
   else if (note == 'c' || note == 'C'){
        freq = 262;}   
   else if (note == 'd' || note == 'D'){
        freq = 294;}
   else if (note == 'e' || note == 'E'){
        freq = 330;}        
   else if (note == 'f' || note == 'F'){
        freq = 349;}        
   else if (note == 'g' || note == 'G'){
        freq = 392;}        
   else if (note == 'u' || note == 'U'){
        freq = 277;}           
   else if (note == 'v' || note == 'V'){
        freq = 311;} 
   else if (note == 'w' || note == 'W'){
        freq = 370;}   
   else if (note == 'y' || note == 'Y'){
        freq = 415;}
   else if (note == 'z' || note == 'Z'){
        freq = 466;}
   else if (note == 'x' || note == 'X'){
        goto Exit;}

to a big "switch" statement:

cin >> note;
   switch (toupper (note))
   {
       case 'A':
           freq = 440;
           break;
       case 'B':
           freq = 494;
           break;

// rest of the options here

    };

You'll need to include the cctype library.

By "Notepad" do you mean the "Notepad" program by Microsoft? You want the program to open that up? Or an output file called "notepad.txt"?

you have functions like fopen, fclose,gets,puts etc for file manipulations, provided by the standard libraries. look up in some documentation.

as in export it as a notepad .txt file with a certain filename (i'll choose it later).

I already said... I don't know how to use printf for this kind of scenarios, and would really appreciate if someone showed me how printf could be used here... because with printf you use get, but after you get it you have to undergo so many operations... printf doesn't seem to support operations in there...

edit: @vernon, I could do that, but since it's all typed out.... might as well leave it.

Well if you just want to put result into a text file, I don't think you want to use printf to do that. I think you want to create and open an ofstream (from the fstream library) with the name of the file you want to output to, get what you need into result, and just send result to that ofstream. So if result was, say 20.43 and you wanted to store it in "result.txt", result.txt would contain:

20.43

You're not displaying or sending to the file thousands of lines of code, just result. Just one little line.

hmm. no. you are not getting what my program does.

there is a for loop. Within the loop, every single time it loops it generates a 'result', which is different every single time.

I want to print that result to the text file, every single instance of that result until the loop is over.

So, like simpler, what I want is this:

int result;
int i;

for (i=0;i<5;i++){
print result to a file called result.txt;
result++;}

Just create file before the start of loop. inside the loop, keep writing to it. after the loop close it. have u tried reading the documentation for ofstream or fopen ? i guess you shd do that now

Okay.

int result;
int i;


// open up an ofstream called "outstreamToFile"   (or any other name)
// Specify the output file name to be "result.txt".

for (i=0;i<5;i++){


outstreamToFile << result << endl;


result++;}


// close outstreamToFile

thanks you guys. Time to test this out (and read the documentation)

Hmm... It seems to work.... but now there is just 1 more problem:

It is either one of my formulae (going to look over them),

or with stringstream.

time:
   cout << "\nPlease enter the length of time the note lasts.\n"
        << "Please enter it in milliseconds (ms).\n";  
   
  getline (cin,mystr);
  stringstream(mystr) >> time;
  
  if (time<0 || time>30000){
  cout << "Invalid time,\n\n";
  goto time;}

At this time, after I type in a number for time, it lags completely and the entire program is stopped.

Can you post your updated program?

soon. Tomorrow morning. I have to sleep now (PST timezone). I'm making it pause and print the values at the end of every loop to see where its going wrong.


Hahaha! no wonder! I put for (t=0; t<time; t+(1/44100);), when I had to put t=t+(1/44100);

What a stupid mistake. It's solved now! thanks everyone for their hard work!

Ah. Another problem that popped up. The program runs perfectly, thanks guys, and my equations were correct =D. But, still some problems.

Here is the complete code:

#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <fstream>


using namespace std;


int main ()
{
   long double n;
   long double t=0.0;
   int time;                   // in ms
   char note;
   int freq;
   long double wave;
   long double envelope1;
   long double envelope2;
   long double envelope;
   long double result;
   int amp=20500;
   int b=65;
   long double c=1.5;
   long double k=1.85;
   char confirmation;
   string mystr;
   
   ofstream outfile;
   outfile.open("file.txt");

   
   start:
      
   cout << "Please enter the note you would like to have played. 1 character only.\n"
        << "Or, select from the list below:\n\n"
        << "U) C sharp;\n"
        << "V) E flat;\n"
        << "W) G flat;\n"
        << "Y) A flat;\n"
        << "Z) B flat;\n\n"
        << "X) Exit.\n\n";
   
   cin >> note;
   cin.get();
   
   if (note == 'a' || note == 'A'){
        freq = 440;}
   else if (note == 'b' || note == 'B'){
        freq = 494;}   
   else if (note == 'c' || note == 'C'){
        freq = 262;}   
   else if (note == 'd' || note == 'D'){
        freq = 294;}
   else if (note == 'e' || note == 'E'){
        freq = 330;}        
   else if (note == 'f' || note == 'F'){
        freq = 349;}        
   else if (note == 'g' || note == 'G'){
        freq = 392;}        
   else if (note == 'u' || note == 'U'){
        freq = 277;}           
   else if (note == 'v' || note == 'V'){
        freq = 311;} 
   else if (note == 'w' || note == 'W'){
        freq = 370;}   
   else if (note == 'y' || note == 'Y'){
        freq = 415;}
   else if (note == 'z' || note == 'Z'){
        freq = 466;}
   else if (note == 'x' || note == 'X'){
        goto finish;}
   else {
        cout << "The note that you entered is not valid. Please enter another one.\n";
        system("pause");
        system("cls");
        goto start;}
        
   time:
   cout << "\nPlease enter the length of time the note lasts.\n"
        << "Please enter it in milliseconds (ms).\n";  
   
  getline (cin,mystr);
  stringstream(mystr) >> time;
  
  if (time<0 || time>3000){
  cout << "Invalid time,\n\n";
  goto time;}
  
   
  calculation: 
  if(t<time){
  envelope1 = b*t/1000;
   envelope2 = c*exp(-k*t/1000);
   
        if (envelope1<envelope2){
            envelope = envelope1;}
        else{
            envelope = envelope2;}
            
   wave = amp*sin(2*M_PI*freq*t/1000);
   result = wave*envelope;
   
  outfile << result << endl; 
  t=t+(1/44.1);

   goto calculation;
   }
  
 system("cls");

loop:
cout << "\nDo you wish to play another note? (Y/N)\n";
cin >> confirmation;
if (confirmation == 'y' || confirmation == 'Y'){
     system("cls");
     goto start;}
else if (confirmation == 'n' || confirmation == 'N'){ 
     goto finish;}
else 
     goto loop;
      

finish:
   outfile.close();    
   return 0;
}

Hehe. I don't trust for loops. If loops are much better.

anyhow, this is the problem: The outfile is supposed to open, and every time a result is generated it is written in a NEW LINE in the outfile. After the loop is finished, the outfile closes. However, I added a way to specify two different notes, one directly after another. But, when you use this feature, you put another note in, the second note OVERWRITES the values of result in the outfile of the first note.

Ie.

First note:

gives numbers in outfile:


1
2
3
4
5
6
7
8

Second note: not quite as long: gives

8
7
6
5.

Supposed to produce:

1
2
3
4
5
6
7
8
8
7
6
5.

but, produces,

8
7
6
5
5
6
7
8.

How to fix this?

may b u can try opening the file in the append mode. in that case it would not overwrite

Ah. Thanks. It works now with append and flush.

I think this is solved. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.