Wrong display on console

I have edited the program and it seems the first Frequency value from my text file is being read and not the rest. How can I create a loop that will keep reading from the text file and display different values for Frequency and Velocity and not same values? For Time, since it is an increment of 100 a loop worked without calling the time from the text file which is not correct as it should read from the text file in a loop. Also, Frequency values are long decimal points not displayed as seen with 9 decimal point since am using double data type. my text file loops like this:
0

100

200

300

400

500

600

5.500000040

5.500000095

5.500000230

5.500001800

5.500000870

5.500000065

5.500000370

Problem:
For a radar transmitting at frequency f (t), the difference in transmitted and received frequencies due to a target moving at speed v (m/s) relative to the radar that is, directly toward or away from the radar is given by

(f (r)- f (t)) / f (t) = 2v/c

where f (r) is the received frequency and c is the speed of light (3x10^8m/s). A weather service station at a major municipal airport is using a C-band Doppler radar f (t) = 5.5GHz). During a severe thunderstorm, the following received frequencies are observed:

Time(s) Frequency (GHz)
0 5.500000040
100 5.500000095
200 5.500000230
300 5.500001800
400 5.500000870
500 5.500000065
600 5.500000370

Write a program that scans these data and displays a table showing the data along with a third column displaying the Doppler velocities of the winds relative to the radar.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int Time = 0;
	double Frequency;
	double Velocity = 0;//size of array more than number of entries in data file

	ifstream infile("C:\\My documents\\frequency.txt", ios::in);//open the text file
	infile >> Time;
	infile >> Frequency;
	while (!infile.eof())
	{

		infile >> Frequency;
		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
		Frequency++;
	}
	cout << setw(1) << "Time" << setw(15) << "Frequency" << setw(20) << "Velocity" << endl;
	for (Time = 0; Time < 700; Time)//counter loop of the first values from 0 to 600 increment
	{
		cout << setw(1) << Time << setw(15) << Frequency << setw(20) << Velocity << endl;
		Time += 100;//Print out for the table with values.
	}

	infile.close();//input file is closed.
	system("PAUSE");//will wait for the user to end the console
	return 0;//End of the main program
}
Last edited on
The way the file is being read, and the following for-loop don't make much sense. It's generally not recommended to use eof() as the condition of a while loop. in this case the problem is made worse by the fact that after each pair of values have been read, nothing is done with those values.

The heading line
 
    cout << "Time" << "    Frequency" << "    Velocity" << endl;
should be output before reading from the file.

Then read/ process something like this:
1
2
3
4
5
6
7
    while (infile >> Time >> Frequency)
    {
        double Velocity = // some formula based on Time and Frequency
        
        // Output the values of Time and Frequency which were read from the file
        // and the calculated Velocity.
    }

I have edited the program and it seems the first Frequency value from my text file is being read and not the rest. How can I create a loop that will keep reading from the text file and display different values for Frequency and Velocity and not same values? For Time, since it is an increment of 100 a loop worked without calling the time from the text file which is not correct as it should read from the text file in a loop. Also, Frequency values are long decimal points not displayed as seen with 9 decimal point since am using double data type. my text file loops like this:
0

100

200

300

400

500

600

5.500000040

5.500000095

5.500000230

5.500001800

5.500000870

5.500000065

5.500000370
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int Time = 0;
	double Frequency;
	double Velocity = 0;//size of array more than number of entries in data file

	ifstream infile("C:\\My documents\\frequency.txt", ios::in);//open the text file
	infile >> Time;
	infile >> Frequency;
	while (!infile.eof())
	{

		infile >> Frequency;
		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
		Frequency++;
	}
	cout << setw(1) << "Time" << setw(15) << "Frequency" << setw(20) << "Velocity" << endl;
	for (Time = 0; Time < 700; Time)//counter loop of the first values from 0 to 600 increment
	{
		cout << setw(1) << Time << setw(15) << Frequency << setw(20) << Velocity << endl;
		Time += 100;//Print out for the table with values.
	}

	infile.close();//input file is closed.
	system("PAUSE");//will wait for the user to end the console
	return 0;//End of the main program
}
As I tried to point out before, you need to change this:
1
2
3
4
5
6
7
8
9
	infile >> Time;
	infile >> Frequency;
	while (!infile.eof())
	{

		infile >> Frequency;
		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
		Frequency++;
	}

to this:
1
2
3
4
5
6
7
   while (infile >> Time >> Frequency)
    {
        double Velocity = Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
        
        // Output the values of Time and Frequency which were read from the file
        // and the calculated Velocity.
    }


Well, you could adapt your code, but please don't use eof() here, it is almost always wrong to use eof() in a while loop. The result, adapting your current version would look lile this, but notice it is longer and more complicated. I tried to show you the simple way.
1
2
3
4
5
6
7
8
9
10
	infile >> Time;
	infile >> Frequency;
	while (infile)
	{
		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
                // Output the values of Time and Frequency which were read from the file
                // and the calculated Velocity.
	        infile >> Time;
	        infile >> Frequency;
	}


Also, get rid of the for loop, it is nonsense. And also as i mentioned, output the heading line before the while loop which reads the file. Why? Because that loop will be where you print out the results, so it's no use printing out the headings afterwards. Also don't do this: Frequency++;, it doesn't make any sense to do that.
I did what you suggested and the print out has nothing!
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int Time = 0;
	double Frequency;
	double Velocity = 0;//size of array more than number of entries in data file

	ifstream infile("C:\\My documents\\frequency.txt", ios::in);//open the text file

	while (infile >> Time >> Frequency)
	{
		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
	}
	cout << setw(1) << "Time" << setw(15) << "Frequency" << setw(20) << "Velocity" << endl;

	infile.close();//input file is closed.
	system("PAUSE");//will wait for the user to end the console
	return 0;//End of the main program
}
I'd read Chervil's comments in the code to mean that you need to add in the output statements to the while loop after the velocity equation.

1
2
3
4
5
6
7
 while (infile >> Time >> Frequency)
    {
        double Velocity = Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
        
        // Output the values of Time and Frequency which were read from the file
        // and the calculated Velocity.
    }


And you'll want to move line 21 with the headers before this while loop.
I am definitely missing something, nothing is retrieved and printed out on the console other than the headings;

1
2
3
4
5
6
7
8
cout << setw(1) << "Time" << setw(15) << "Frequency" << setw(20) << "Velocity" << endl;
	while (infile >> Time >> Frequency)
	{
		double Velocity = Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
	}
	

	infile.close();//input file is closed. 
between lines 4 and 5, (i.e. after calculating Velocity and before the closing brace) you will need the cout statements.

In other words, replace this comment:
1
2
        // Output the values of Time and Frequency which were read from the file
        // and the calculated Velocity. 

with the appropriate cout <<
Getting closer. Now on the console am getting this and not last values of time (600) and not getting full values of Frequency, i.e.,(5.500000370) but 5.5 only.

Time Frequency Velocity
0 5.5 1.09091
100 5.5 2.59091
200 5.5 6.27273
300 5.5 49.0909
400 5.5 23.7273
500 5.5 1.77273
Press any key to continue . . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << setw(1) << "Time" << setw(15) << "Frequency" << setw(20) << "Velocity" << endl;
	
	while (infile >> Time >> Frequency)
	{

		Velocity = (((Frequency - 5.5) / 5.5) * 3e+8) / 2;
		
		cout << setw(1) << Time << setw(15) << Frequency << setw(20) << Velocity << endl;
		
	}
	
	
		
	infile.close();//input file is closed.
	system("PAUSE");//will wait for the user to end the console
	return 0;//End of the main program 

I was missing last Frequency numbers. Now am able to have this, except the full frequency decimals:
[code]
Time Frequency Velocity
0 5.5 1.09091
100 5.5 2.59091
200 5.5 6.27273
300 5.5 49.0909
400 5.5 23.7273
500 5.5 1.77273
600 5.5 10.0909
Press any key to continue . . .
Topic archived. No new replies allowed.