Hello cplusplusgs,
"using namespace std;" if for lazy instructors that do not want to have to read "std::" in front of things. As an example: someday you will need to include the header file "<algorithm>" which has a function "sort" in the standard namespace then you will write your own function called "sort". The compiler will first try to use "std::sort()" and if this does not give any errors it will work until you make changes to your "sort" function and can not figure out why the changes do not work.
Search "using namespace std" here and you will find many resuls that address this problem. This link is also worth reading:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
The sample of the output file would be written this way:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//USED FOR PRINTING THINGS THAT APPEARS IN THE OUTPUT FILE
outfile << "No\t" << "Type\t" << "Odd\t" << "Even\t" << "Sum\t" << "Digit\t\n" << std::endl;
for (int i = 0; i < size; i++)
{
outfile << r[i].No << "\t"
<< r[i].type << "\t"
<< r[i].odd << "\t"
<< r[i].even << "\t"
<< r[i].sum << "\t"
<< r[i].digit
<< endl;
}
|
Moving the first line above the for loop will print this as header once. Within the for loop I would use "setw()" before the variable and loose the "\t". The "\t" will not align the columns the way you would want.
1 2 3 4 5 6 7 8 9
|
std::string junk{ "" };
i = 0;
while (afile >> junk)
{
afile >> junk;
afile >> r[i].No;
i++;
}
|
As I said in a previous message you will need to include the header file "string". It also occurred to me that "fileName" should be defined as a std::string and if your compiles is using C++11 standard or greater "afile.open()" can use a "std::string" in stead of a C string.
Refeering tto the above code:
Line 1 defines the variable "junk". I did this here out of convenience and to keep the code short. Normally I would define this at the beginning of the function.
Line 2 not really needed here. I did this to show that "i" should start at zero and be defined out side the while loop. Later I notice that you did define and initialize "i" earlier. You could leave it here to make sure that "i" will start at zero.
Line 4 will start the while loop reading the string "Numbers" form the file
Line 6 Again reads the second piece of information from the input file that you do not need.
Line 7 Stores the actual random number into the struct as yo want.
Line 8 Increases "i" one, same as
i = i + 1;
. This allows you to use each element of the array not just the first, element zero. When finished "i" will be the number of records read, so when the value of "i" is used i a for loop condition it will have to be
for (...; iterator <= size; ...)
because "< size" would leave you one short.
The code was not to show the best way to to this, keskiverto's suggestion is the more accepted way, but to show what needed to be done.
In the end the code will read the file until end of file is reached when the read produces nothing because you are trying to read past EOF. The file stream will fail and so will the while loop.
There are better ways to do the while loop. I was thinking this would be easier to understand.
Hope that helps,
Andy