Hello everyone. I'm working on a project that acts as a function generator to output a sine wave. The program also outputs an excel file with all of the data points so one can plot the points. For some reason, when I add noise, the graph does not look right. Can anyone tell me why? I appreciate the help.

Here is my code:

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <math.h>
#include <conio.h> 
#include <iomanip>
#include <time.h> 
#include <stdlib.h> 
#include <dos.h>

using namespace std;

void lab_7 (void)
    // declarations
    FILE *fptr;
    char fileName[50] = "";
    double freq;
    double gain;
    double phase;
    double bias;
    double noise;
    char save;

    const long double PI = acos((long double) -1);

    // user inputs for sine wave values

    cout<<"Enter Frequency: [1-10Mhz] ";
    cout<<"Enter Gain of Sine Wave: [1-10] ";
    cout<<"Enter Phase Angle: [0-180] ";
    cout<<"Enter Bias (offset) [+/- 10] ";
    cout<<"Enter % Noise level introduced to sine (percent) [0 - 100%] ";
    cout<<"Do you want to save the data [y/n]: ";

    if (save == 'y'){
        cout<<"Enter a file name you wish to use: ";
            cin>> fileName;

    double timeint = (double)1/freq/360;
    long double interval = (2*PI)/360;
    long double sinevalue;
    if (save = 'y') {
        sprintf(fileName, "%s.csv", fileName);
        fptr = fopen(fileName, "w");
        fprintf(fptr, "%s,", "Time(1/f)");
        fprintf(fptr, "%s\n", "Sine Pattern");
    for(int i=0; i<=360; i++){
        sinevalue = (gain*sin((interval*i)+phase)+(rand()*(noise/100)*gain))+bias;
        if (save = 'y') {
            fprintf(fptr, "%f,", timeint*i);
            fprintf(fptr, "%f\n", sinevalue);


I'm wondering if precision could be your problem. I believe double is precise to only about 15 places. The apfloat library might work better.

Hmmm...also, is it because I need to cap the noise? If so, how would I go about doing that?