Hello osteen91,
As lastchance has already mentioned I just do a copy and paste here.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
When I loaded your code it came out like this:
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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std; // <--- Best not to use.
int main()
{
constexpr size_t MAXSIZE{ 8 };
int i = 0;
int j = 0;
double angle[MAXSIZE]{}; // <--- Changed.
double amplitude[MAXSIZE]{}; // <--- Changed.
//size of array more than number of entries in data file
ifstream infile;
infile.open("amplitude.txt");//open the text file
if (!infile)
{
cout << "Unable to open file";
return 1; //exit(1); // <--- if not in "main". // terminate with error // <--- Changed.
}
while (!infile.eof())
{
do
infile >> angle[8] >> amplitude[8];//To make three arrays for each column (a for 1st column, b for 2nd....)
while (i < 8);
{
++i;
};
while (j < 8)
{
++j;
}
cout << angle[i]; amplitude[j];
}
return 0;
}
|
With a couple of minor fixes you are good down to line 27;
Then it falls apart.
On line 27 the while condition does not work the way you are thinking. By the time it figures out that you have reached "eof" you will have entered the do while loop one extra time.
Lines 32 - 34 increment the variable "i", but this is outside the do/while loop, so during the do/while loop "i" will always be zero and it will produce an endless loop.
Lines 36 - 39 As
lastchance has asked what is this for. There is no real use. If your intent is to print out the arrays a for loop would work better.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// <--- First line in "main"
constexpr size_t MAXSIZE{ 8 };
if (!infile)
{
cout << "Unable to open file";
return 1; //exit(1); // <--- if not in "main". // terminate with error // <--- Changed.
}
while (infile >> angle[i] >> amplitude[i])
{
++i;
}
std::cout << std::fixed << std::showpoint << std::setprecision(2);
for (size_t lc = 0; lc < MAXSIZE; lc++)
{
cout << std::setprecision(2) << std::setw(5) << angle[lc] << " "
<< std::setprecision(4) << std::setw(8) << amplitude[lc] << std::endl;
}
|
For line 7 it is best to use "return" when the if statement is in "main" and "exit()" when the if statement is in a function or a different file. Zero is considered a normal exit and any number greater than zero means there is a problem.
Hint: By using (1, 2, 3, etc.) the number can help track down where the problem occurred. This is useful when there are multiple "return" or "exit()" statements in a program. The "return" at the end of "main" should always return zero.
The while loop is the most often way to read a file of unknown length. When the stream fails to read anything then the while condition will fail and the program will move on.
Notice the use of "i" in the while condition. This will change the element of the array instead of what you did trying to put everything in element "8" of the array that dos not exist.
Line 15 is the way that it is added through My IDE. This line need done only once before it is used to output floating point numbers. Line 15 will manipulate the output until it is changed. Line 19 is a good demonstration of this.
The for loop is the usual method to print an array. If you need to a while loop can work.
I did not put the time into the output. Headings and spaceing are some additions that you could make later. Anyway the output looks like this:
0.00 0.2200
0.80 0.0142
1.60 -0.0180
2.40 0.0375
3.20 0.2186
4.00 -0.0091
4.80 -0.1769
5.60 0.0606
|
Hint: in the "setw"s that might have a minus sign be sure to include an extra place for the minus sign so that the decimal points will line up properly.
Hope that helps,
Andy